Search code examples
xsltxpathxalan

In Xalan XSLT 1.0, how to pass a variable to a template match?


We are using Xalan XSLT 1.0 in Java and we want to pass a variable to a template match to avoid hard-coding element names in the XSL file. The style sheet compiles, but the date returned is wrong. Are we using the correct syntax?

Possible XML inputs...

 <books>   
    <book/>
    <book/>
 </books>

 <dvds>
     <dvd/>
     <dvd/>
 </dvds>


<xsl:variable name="matchElement" select="'book'"/>
<!-- OR -->
<xsl:variable name="matchElement" select="'dvd'"/>

<xsl:template match="/*[local-name() = $matchElement]">  

Solution

  • This xsl:template:

    <xsl:template match="/*[local-name() = $matchElement]"> 
    

    is matching from root.

    Either remove the / from /* or change it to //* (depending on how the rest of your stylesheet is designed).

    Also, if you use xsl:param instead of xsl:variable, you can set the value from the command line.