I understand that XSLT doesn't allow one to directly break out of a xsl:for-each
loop, but there must be an easy way to add more conditions to achieve it. I've seen much online on the same context but unable to proceed further.
I want to iterate through a set of XML tags and come out of the iteration on finding a value in one XML tag and use that value in another xsl:template
. For example,
<xsl:template name="categoryCheckTemplate">
<xsl:for-each select="TEST/ABC/DEF/GHI/Row">
<xsl:variable name="category">
<xsl:choose>
<xsl:when test="JKL/Category">
<xsl:value-of select="JKL/Category"/>
</xsl:when>
<xsl:when test="MNO/Category">
<xsl:value-of select="MNO/Category"/>
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="(contains($category,'123'))">
<xsl:value-of select="'true'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'false'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
I want to use the 'true'
value of this template in another template but don't want to iterate it all the way. Anytime it is true, I need to come out of the iteration.
XML Input
<xml>
<TEST>
<ABC>
<DEF>
<GHI>
<Row>
<JKL>
<Category>123</Category>
</JKL>
</Row>
<Row>
<JKL>
<Category>456</Category>
</JKL>
</Row>
<Row>
<JKL>
<Category>789</Category>
</JKL>
</Row>
<Row>
<JKL>
<Category>012</Category>
</JKL>
</Row>
</GHI>
</DEF>
</ABC>
</TEST>
</xml>
Desired Output
<item>
<cat>123</cat>
</item>
Can anyone help on this XSLT 1.0?
See Question&Answers more detail:os