Reputation: 49
I've created an XSLT using an identity template and several templates that match to a potential XPath in the source. However, the matching paths do not always exist. Is there a way to "insert" the path before the matching template applies? Since I know XSLT does not execute procedurally, I wasn't sure how to do this. Examples below.
Let's say this is the XSLT:
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match='pathA'>
do stuff
</xsl:template>
<xsl:template match='pathB'>
do something
</xsl:template>
<xsl:template match='pathC'>
do other stuff
</xsl:template>
And let's say this in the input:
<root>
<Child>
<pathA>I have Data!</pathA>
<pathC>We skipped B!</pathC>
</Child>
</root>
Is there a way to "create" pathB
so that the template that matches the XPath can execute?
Thanks again for any assistance!
Upvotes: 1
Views: 265
Reputation: 4806
Good one. How about...
<xsl:template name="pathB">
<xsl:param name="nodes"/>
do something
</xsl:template>
<xsl:template match="Child">
<xsl:copy>
<xsl:apply-templates select="@* | pathA[not(../pathB)] | pathB/preceding-sibling::node()"/>
</xsl:copy>
<xsl:call-template name="pathB">
<!-- pass the set of elements of type "pathB", possibly an empty nodeset -->
<xsl:with-param name="nodes" select="pathB"/>
</xsl:call-template>
<xsl:copy>
<xsl:apply-templates select="node()[not(self::pathA) and not(../pathB)] | pathB/following-sibling::node()"/>
</xsl:copy>
<xsl:template>
Upvotes: 1