0
votes

I was using XSLT1.0 using inbuilt processors from jre1.6
When I changed my processor to XSLT2.0 using SAXONHE jars suddenly most of template matching are not working.

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

<xsl:template match="@source[. = 'SG']">
    <xsl:attribute name="source">MIG</xsl:attribute>  
</xsl:template>

<xsl:template match="customer/@homeAddress"/>

<xsl:param name="removeInvAttr" select="'indexDefinition|services|paymentMethod'"/>
<xsl:template match="invoice/@*">
    <xsl:for-each select="@*">
        <xsl:if test="not(name() = $removeInvAttr)">
            <xsl:call-template name="identity"/>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

<xsl:variable name="vLowercaseChars_CONST" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="vUppercaseChars_CONST" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:template match="@country">
     <xsl:attribute name="country"><xsl:value-of select="translate(. , $vLowercaseChars_CONST , $vUppercaseChars_CONST)"/></xsl:attribute>
</xsl:template>

<xsl:template match="@claimingSystem">
    <xsl:if test="string-length(.) > 5">
        <xsl:attribute name="claimingSystem"><xsl:value-of select="substring(.,1,5)"/></xsl:attribute>
    </xsl:if>
</xsl:template>

All above templates not working except first template "identity".
How to make them work in both XSLT 2.0 and 1.0?

Input xml is:

<dynamicData source="SG">
    <customer name="Cyrus S" homeAddress="NY" custType="P" country="us">
    <invoice amount="250" invType="C" indexDefinition="SECR" services="TYRE_REP" paymentMethod="CC" claimingSystem="EX001-S1"/>
    </customer>
</dynamicData>

Expected output is:

<dynamicData source="MIG">
    <customer name="Cyrus S" custType="P" country="US">
    <invoice amount="250" invType="C" claimingSystem="EX001"/>
    </customer>
</dynamicData>

This works in XSLT 1.0.

1
If you include a sample XML input, we can test this ourselves.Mathias Müller
<xsl:if test="not(name() = $removeInvAttr)"> with a delimited string in $removeInvAttr would absolutely never have worked in any version of XSLT. Also <xsl:template match="/@claimingSystem"> cannot match anything, independent of the XSLT version.Tomalak
In addition to providing sample XML input, you should describe clearly what "not working" means. A specific error message would be ideal.kjhughes
Suspect classpath/packaging issue.kjhughes
Corrected my post for input/output and template of claimingSystem.user2025527

1 Answers

0
votes

I don't see how

<xsl:param name="removeInvAttr" select="'indexDefinition|services|paymentMethod'"/>
<xsl:template match="invoice/@*">
    <xsl:for-each select="@*">
        <xsl:if test="not(name() = $removeInvAttr)">
            <xsl:call-template name="identity"/>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

could do anything as the parameter is a string with several names separated by a bar | and, as important, with match="invoice/@*" the xsl:for-each select="@*" will not select anything (as an attribute node as the context node does not have any attribute nodes itself).

So I suspect you want

<xsl:param name="removeInvAttr" select="'indexDefinition|services|paymentMethod'"/>
<xsl:variable name="removeAttrNames" select="tokenize($removeInvAttr)"/>

and

<xsl:template match="invoice/@*[name() = $removeAttrNames]"/>