I want to apply a template with a mode that depends on a variable value.
<xsl:variable name="mode" select="@attribute"/>
<xsl:apply-templates mode="{$mode}"/>
I get the error that the stylesheet cannot be compiled. The value of mode should be a QName, but it is "{$mode}".
Is there a possibilty to use modes dependent on variables?
mode. The mode attribute allows an element as specified by its Qualified Names to be processed multiple times, each time producing a different result. If <xsl:template> does not have a match attribute, it cannot have a mode attribute.
The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes. If we add a "select" attribute to the <xsl:apply-templates> element, it will process only the child elements that matches the value of the attribute.
The only option you have to use a certain mode based on an expression is to use
<xsl:choose>
<xsl:when test="@attribute = 'foo'">
<xsl:apply-templates mode="bar"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
or the same with an xsl:if
. The mode
attribute value itself needs to be a QName
in XSLT 1.0 respectively in XSLT 2.0 allows a QName
or special tokens like #current
or #default
'. But you can't compute a mode
value at run-time.
mode is not a valid candidate for Attribute Value Templates (AVT). You simply can't do this.
From the XSLT 2.0 spec:
[Definition: In an attribute that is designated as an attribute value template, such as an attribute of a literal result element, an expression can be used by surrounding the expression with curly brackets ({})].
mode is not designated as AVT in the spec, so ou can't do this.
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