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

僤鯓⒐⒋嵵緔 提交于 2019-12-11 05:29:30

问题


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

回答1:


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.




回答2:


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.)



来源:https://stackoverflow.com/questions/8041838/in-xalan-xslt-1-0-how-to-pass-a-variable-to-a-template-match

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!