Reputation: 27516
I have a stylesheet with a number of templates that match certain elements, including an identity template:
<xsl:stylesheet>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="someElement/*>
...
</xsl:template>
<!-- a bunch of other matching templates -->
</xsl:stylesheet>
A new requirement has come up that if a certain element in the input document has a specific value, most of the transformation should simply be skipped.
Of course I can't simply do this:
<xsl:stylesheet>
<xsl:choose>
<xsl:when test="/someElement/somethingElse < 0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="someElement/*>
...
</xsl:template>
<!-- a bunch of other matching templates -->
</xsl:when>
<xsl:otherwise>
<!-- do very simple transform -->
</xsl:otherwise>
<xsl:choose>
</xsl:stylesheet>
because template
is not allowed as a child of when
. It looks like the only way to deal with this might be to rewrite all of the templates with actual names and parameters, but there are quite a few and I was wondering if there was an easier way.
Upvotes: 0
Views: 32
Reputation: 338316
<xsl:stylesheet>
<xsl:template match="@*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="/*[/someElement/somethingElse < 0]">
<xsl:apply-templates select="." name="identity" />
</xsl:template>
<xsl:template match="/*">
<!-- do very simple transform -->
</xsl:template>
<!-- a bunch of other matching templates, no change necessary -->
</xsl:stylesheet>
The second template has a more specific match expression, so that it will preferred over the third template when your condition is met.
Of course you can reverse the match expressions and do the "simple transform" when the condition is (not) met, keeping match="/*"
the "default".
Upvotes: 3