How-to break a for-each loop in XSLT?
There is no such a thing as break in an XSLT for-each loop. xsl:if/@test is pretty much all you can do. The other way would be to include this condition within the for-each/@select.
Definition and Usage The <xsl:number> element is used to determine the integer position of the current node in the source. It is also used to format a number.
You can format your XSLT stylesheet to go to a specific node, and then loop through the given node set. You create an XSLT loop with the <xsl:for-each> tag. The value of the select attribute in this tag is an XPath expression that allows you to specify the data element to loop through.
Variables in XSLT are not really variables, as their values cannot be changed. They resemble constants from conventional programming languages. The only way in which a variable can be changed is by declaring it inside a for-each loop, in which case its value will be updated for every iteration.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With