Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use the value of a variable in the mode of apply-templates

Tags:

xslt

xslt-1.0

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?

like image 952
maria90 Avatar asked Sep 06 '12 11:09

maria90


People also ask

What is mode in xsl apply templates?

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.

What does apply template mean?

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.


2 Answers

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.

like image 154
Martin Honnen Avatar answered Sep 28 '22 09:09

Martin Honnen


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.

like image 44
Sean B. Durkin Avatar answered Sep 28 '22 10:09

Sean B. Durkin