The xsl:iterate instruction is new in XSLT 3.0. It is similar to xsl:for-each, except that the items in the input sequence are processed sequentially, and after processing each item in the input sequence it is possible to set parameters for use in the next iteration. It can therefore be used to solve problems that in XSLT 2.0 require recursive functions or templates.

The xsl:iterate instruction is motivated by use-cases for streaming, but it can also be used profitably in non-streaming situations.

Here is an example that computes the running balance of a sequence of financial transactions:

<xsl:iterate select="transactions/transaction"> <xsl:param name="balance" select="0.00" as="xs:decimal"/> <xsl:variable name="newBalance" select="$balance + xs:decimal(@value)"/> <balance date="{@date}" value="{$newBalance}"/> <xsl:next-iteration> <xsl:with-param name="balance" select="$newBalance"/> </xsl:next-iteration> </xsl:iterate>

As well as xsl:next-iteration, the instruction allows a child element xsl:break which causes premature completion before the entire input sequence has been processed, and a child element xsl:on-completion which defines processing to be carried out when the input sequence is exhausted. The instructions within xsl:on-completion have access to the final values of the parameters declared in the xsl:next-iteration instruction set while processing the last item in the sequence.

Here is an example that copies the input sequence up to the first br element:

<xsl:iterate select="*"> <xsl:choose> <xsl:when test="self::br"> <xsl:break/> </xsl:when> <xsl:otherwise> <xsl:copy-of select="."/> </xsl:otherwise> </xsl:choose> </xsl:iterate>

Earlier Saxon releases implemented a prototype of xsl:iterate as an extension in the Saxon namespace (saxon:iterate). This is dropped in Saxon 9.5.