Reputation: 459
I want to dynamically change the apply-templates mode based on the source XML's attribute, like this:
<xsl:choose>
<xsl:when test="@myAttribute">
<xsl:apply-templates select="." mode="@myAttribute"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="." mode="someOtherMode"/>
</xsl:otherwise>
</xsl:choose>
Is it possible to evaluate the XPath in the mode attribute? Is there some other approach?
Thanks!
Upvotes: 2
Views: 323
Reputation: 101680
No, there isn't a way to use a dynamic value for the mode
attribute. It has to be static. In your case, I would suggest doing something like this (using the name myNode as the context node for your example above):
<xsl:template match="myNode[@myAttribute = 'someValue']" mode="specialHandling">
<!-- template contents -->
</xsl:template>
<xsl:template match="myNode[@myAttribute = 'someOtherValue']" mode="specialHandling">
<!-- template contents -->
</xsl:template>
<xsl:template match="myNode[@myAttribute = 'aThirdValue']" mode="specialHandling">
<!-- template contents -->
</xsl:template>
<xsl:template match="myNode[not(@myAttribute)]" mode="specialHandling">
<!-- template contents -->
</xsl:template>
Then you don't even need that xsl:choose
. You can just do:
<xsl:apply-templates select="." mode="specialHandling" />
Upvotes: 3