XSLT3.0への道(5) xsl:iterateの続きの続き

次のスタイルシートは、$prmLimitで指定された金額(10,000)を越えない限度まで入力データを出力します.xsl:breakでそのときの計を出力します.xsl:next-iterationとxsl:breakはxsl:iterateの最後の位置になければなりません.xsl:chooseでいずれかを選択するのが必須のパターンのようです.(xsl:on-completionは別扱い.)
 
<xsl:param name="prmLimit" as="xs:decimal" select="10000"/>
<xsl:template match="data">
    <result>
        <xsl:iterate select="product">
            <xsl:param name="previousTotalAmount" select="0.00" as="xs:decimal"/>
            <xsl:variable name="amount" select="xs:decimal(@price * @count)" as="xs:decimal"/>
            <xsl:variable name="currentTotalAmount"
                          select="$previousTotalAmount + $amount" as="xs:decimal"/>
            <xsl:choose>
                <xsl:when test="$currentTotalAmount lt $prmLimit">
                    <xsl:copy>
                        <xsl:copy-of select="@type"/>
                        <xsl:copy-of select="@maker"/>
                        <xsl:attribute name="total" select="$amount"/>
                    </xsl:copy>
                    <xsl:next-iteration>
                        <xsl:with-param name="previousTotalAmount" select="$currentTotalAmount"/>
                    </xsl:next-iteration>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:break>
                        <limit-amount value="{$previousTotalAmount}"/>
                    </xsl:break>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:on-completion>
                <total-amount value="{$previousTotalAmount}"/>
            </xsl:on-completion>
        </xsl:iterate>
    </result>
</xsl:template>
 
[出力XML]
<?xml version="1.0" encoding="UTF-8"?>
<result>
   <product type="pencil" maker="三菱" total="500"/>
   <product type="pencil" maker="コーリン" total="900"/>
   <product type="pencil" maker="トンボ" total="2200"/>
   <product type="pencil" maker="北星" total="3150"/>
   <product type="note" maker="コクヨ" total="2000"/>
   <limit-amount value="8750"/>
</result>