Streaming with saxon:iterate
In the examples given above, streaming is used to select a sequence of element nodes
from the source document, and each of these nodes is then processed independently. In cases where the processing
of one node depends in some way on previous nodes, it is possible to use burst-mode streaming in
conjunction with the xsl:iterate
extension element in XSLT 3.0.
The following example takes a sequence of <transaction>
elements in an input document, each one containing
the value of a debit or credit from an account. As output it copies the transaction elements, adding a current balance.
The following example is similar: this time it copies the account number (contained in a separate element at the start of the file) into each transaction element:
<xsl:stream href="transactions.xml"> <xsl:iterate select="account/(account-number|transaction)"> <xsl:param name="accountNr"/> <xsl:choose> <xsl:when test="self::account-number"> <xsl:next-iteration> <xsl:with-param name="accountNr" select="string(.)"/> </xsl:next-iteration> </xsl:when> <xsl:otherwise> <transaction account-number="{$accountNr}"> <xsl:copy-of select="@*"/> </transaction> </xsl:otherwise> </xsl:choose> </xsl:iterate> </xsl:stream>Here is a more complex example, one that groups adjacent transaction elements having the same date attribute. The two loop parameters are the current grouping key and the current date. The contents of a group are accumulated in a variable until the date changes.
<xsl:stream href="transactions.xml"> <xsl:iterate select="account/transaction"> <xsl:param name="group" as="element(transaction)*" select="()"/> <xsl:param name="currentDate" as="xs:date?" select="()"/> <xsl:choose> <xsl:when test="xs:date(@date) eq $currentDate or empty($group)"> <xsl:next-iteration> <xsl:with-param name="currentDate" select="@date"/> <xsl:with-param name="group" select="($group, .)"/> </xsl:next-iteration> </xsl:when> <xsl:otherwise> <daily-transactions date="{$currentDate}"> <xsl:copy-of select="$group"/> </daily-transactions> <xsl:next-iteration> <xsl:with-param name="group" select="."/> <xsl:with-param name="currentDate" select="@date"/> </xsl:next-iteration> </xsl:otherwise> </xsl:choose> <xsl:on-completion> <final-daily-transactions date="{$currentDate}"> <xsl:copy-of select="$group"/> </final-daily-transactions> </xsl:on-completion> </xsl:iterate> </xsl:stream>Note that when a xsl:iterate
loop is terminated using xsl:break
,
parsing of the source document will be abandoned. This provides a convenient way to read data near the start
of a large file without incurring the cost of reading the entire file.