Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT Error produced "Ambiguous rule match"

Tags:

match

xslt

I am trying to do a match for two scenarios:

  1. String contains character other than just a number
  2. String has more than 8 characters.

So the XSLT is:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="record[translate(employeeNumber, 'abcdefghijklmnopqrstuvwxyzABCDEFGHILKLMNOIPQRSTUVWXYZ!£$%^', '')]"/>
    <xsl:template match="record[string-length(employeeNumber) &lt; 9]"/>
</xsl:stylesheet>

Test Data is:

<?xml version="1.0" encoding="UTF-8"?>
<request>
    <records>
        <record>
            <employeeNumber>12345678</employeeNumber>
        </record>
        <record>
            <employeeNumber>1234567A</employeeNumber>
        </record>
        <record>
            <employeeNumber>12345678A</employeeNumber>
        </record>
        <record>
            <employeeNumber>123456789</employeeNumber>
        </record>
    </records>
</request>

This is what should be returned:

<?xml version="1.0" encoding="UTF-8"?>
<request>
    <records>
        <record>
            <employeeNumber>1234567A</employeeNumber>
        </record>
        <record>
            <employeeNumber>12345678A</employeeNumber>
        </record>
        <record>
            <employeeNumber>123456789</employeeNumber>
        </record>
    </records>
</request>

However as i said in Oxygen i am getting the error:

Severity: warning Description: Ambiguous rule match for /request[1]/records[1]/record[1] Matches both "record[string-length(employeeNumber) < 9]" on line 13 of file:/C:/Users/mdown/Desktop/Untitled21.xsl and "record[translate(employeeNumber, 'abcdefghijklmnopqrstuvwxyzABCDEFGHILKLMNOIPQRSTUVWXYZ!£$%^', '')]" on line 12 of file:/C:/Users/mdown/Desktop/Untitled21.xsl

The reason for this is because it is matching both rules, however this should not be a problem. How would i alter the XSLT to support these options.

like image 899
mmkd Avatar asked May 10 '12 15:05

mmkd


3 Answers

It looks to me as if you are getting a warning, not an error. If you want to avoid the warning then decide which template should have higher priority and set that e.g. <xsl:template match="foo" priority="5"/> as needed.

like image 73
Martin Honnen Avatar answered Oct 24 '22 09:10

Martin Honnen


The XSLT specification says that it is a "recoverable error" if a node matches more than one template rule with the same priority. Implementations are allowed to ignore the error and choose whichever template comes last, or they are allowed to treat it as a hard error. Saxon allows either of these options to be selected by a command line parameter, but by default it chooses a middle options which is to report a warning and carry on.

I would recommend that when you get this warning, you examine the two rules and decide which of them you want to take priority. Then give both rules explicit priority attributes so there is no ambiguity.

like image 45
Michael Kay Avatar answered Oct 24 '22 09:10

Michael Kay


I am not sure if this will work, but try adding the opposite check on the length to the first template match

<xsl:template match="record
       [translate(employeeNumber, 'abcdefghijklmnopqrstuvwxyzABCDEFGHILKLMNOIPQRSTUVWXYZ!£$%^', '')]
       [string-length(employeeNumber) &gt;= 9]"/"/> 

<xsl:template match="record[string-length(employeeNumber) &lt; 9]"/> 

It is a bit of duplicate coding, but they should now not match the same record.

like image 1
Tim C Avatar answered Oct 24 '22 08:10

Tim C