In Xalan XSLT 1.0, how to pass a variable to a tem

2019-08-14 04:39发布

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]">  

标签: xslt xpath xalan
2条回答
祖国的老花朵
2楼-- · 2019-08-14 04:58

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.

查看更多
欢心
3楼-- · 2019-08-14 05:24

Your variable syntax is correct, but note that it is technically illegal to use variable or parameter references in XSLT 1.0 match patterns. It is possible, however, that Xalan has implemented this behavior outside of the standard. (@DevNull's comment about your expression also applies.)

查看更多
登录 后发表回答