How-to break a for-each loop in XSLT?
Put the condition for stopping the "loop" in the select
attribute of the for-each
element. For instance, to "break" after four elements:
<xsl:for-each select="nodes[position()<=4]">
To iterate up to but not including a node that satisfied some particular condition:
<xsl:for-each select="preceding-sibling::node[condition]">
XSLT is written in a very functional style, and in this style there is no equivalent of a break
statement. What you can do is something like this:
<xsl:for-each select="...nodes...">
<xsl:if test="...some condition...">
...body of loop...
</xsl:if>
</xsl:for-each>
That way the for-each
will still iterate through all the nodes, but the body of the loop will only be executed if the condition is true.