Given
An XSLT stylesheet with a global variable:
<xsl:variable name="lang" select="/response/str[@name='lang']"/>
Question
Where from comes the limitation that using variables in predicates is incorrect in the xsl:template
matching pattern, but is acceptable in xsl:apply-templates
selecting pattern?
<!-- throws compilation error, at least in libxslt -->
<xsl:template match="list[@name='item_list'][$lang]"/>
<!-- works as expected -->
<xsl:template match="list[@name='item_list'][/response/str[@name='lang']]"/>
<!-- works as expected -->
<xsl:template match="response">
<xsl:apply-templates select="list[@name='item_list'][$lang]">
</xsl:template>
Variables are not allowed to be used in match expressions in XSLT 1.0.
From the XSLT 1.0 specification: Defining Template Rules
It is an error for the value of the match attribute to contain a VariableReference.
Variables are allowed in match expressions in XSLT 2.0.
From the XSLT 2.0 specification: Syntax of Patterns
Patterns may start with an id FO or key function call, provided that the value to be matched is supplied as either a literal or a reference to a variable or parameter, and the key name (in the case of the key function) is supplied as a string literal. These patterns will never match a node in a tree whose root is not a document node.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With