[xsl] Using xsl:iterate to get a count

Subject: [xsl] Using xsl:iterate to get a count
From: "rick@xxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 1 Jul 2026 18:29:00 -0000
Hi All,



I am have a series of bookmark elements and I am trying to find the absolute
number of gaps between the @number elements. In my example, there are 3
gaps: between 21-2 and 21-5; between 21-7 and 21-11; and between 21-11 and
21-15.



I set up a named template and am expecting an integer of 3, but instead I am
getting 011. I am missing something fundamental. Any help would be
appreciated. Thank you.



Rick Quatro



<?xml version="1.0" encoding="UTF-8"?>

<map>

  <chapter name="Chapter21TOC.fm" number="21" page="1523">

    <title>Chapter 21 Common Procedural Language and Notice of Court
Proceeding</title>

    <bookmark name="Form21-1.fm" type="form" number="21-1" page="1525">

      <title>Form 21-1 Notice of Hearing</title>

    </bookmark>

    <bookmark name="Form21-2.fm" type="form" number="21-2" page="1528">

      <title>Form 21-2 Notice of Hearing</title>

    </bookmark>

    <bookmark name="Form21-5.fm" type="form" number="21-5" page="1531">

      <title>Form 21-5 Objection to Notice of Hearing</title>

    </bookmark>

    <bookmark name="Form21-6.fm" type="form" number="21-6" page="1535">

      <title>Form 21-6 Order on Objection to Notice of Hearing</title>

    </bookmark>

    <bookmark name="Form21-7.fm" type="form" number="21-7" page="1537">

      <title>Form 21-7 Objection to Notice of Trial</title>

    </bookmark>

    <bookmark name="Form21-11.fm" type="form" number="21-11" page="1541">

      <title>Form 21-11 Certificate of Service</title>

    </bookmark>

    <bookmark name="Form21-15.fm" type="form" number="21-15" page="1542">

      <title>Form 21-15 Suggested Language for Appearances before the
Court</title>

    </bookmark>

  </chapter>

</map>



<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";

    xmlns:xs="http://www.w3.org/2001/XMLSchema";

    exclude-result-prefixes="xs"

    version="3.0"

    expand-text="yes">



   <xsl:output method="text" indent="no"/>



    <xsl:template match="/map">

        <xsl:apply-templates/>

    </xsl:template>



    <xsl:template match="chapter">

        <xsl:variable name="reserved-count">

            <xsl:choose>

                <xsl:when test="exists(bookmark[@type='form'])">

                    <xsl:call-template name="get-reserved-count"/>

                </xsl:when>

                <xsl:otherwise>0</xsl:otherwise>

            </xsl:choose>

        </xsl:variable>

        <xsl:text>{@name} {$reserved-count}</xsl:text>

    </xsl:template>



    <xsl:template name="get-reserved-count">

        <xsl:sequence><xsl:iterate select="bookmark[@type = 'form']">

            <xsl:param name="reserved-count" select="0" as="xs:integer"/>

            <xsl:if test="preceding-sibling::bookmark[1][@type = 'form']">

                <xsl:variable name="prev-form-num"
select="tokenize(preceding-sibling::bookmark[1]/@number,'\-')"/>

                <xsl:variable name="curr-form-num"
select="tokenize(@number,'\-')"/>

                <xsl:variable name="gap" select="(number($curr-form-num[2])
- number($prev-form-num[2]))"/>

                <xsl:choose>

                    <xsl:when test="$gap > 1">

                        <xsl:next-iteration>

                            <xsl:with-param name="reserved-count"
select="$reserved-count + 1" as="xs:integer"/>

                        </xsl:next-iteration>

                    </xsl:when>

                    <xsl:when test=".[last()]">{$reserved-count}</xsl:when>

                    <xsl:otherwise>

                        <xsl:next-iteration>

                            <xsl:with-param name="reserved-count"
select="$reserved-count" as="xs:integer"/>

                        </xsl:next-iteration>

                    </xsl:otherwise>

                </xsl:choose>

            </xsl:if>

        </xsl:iterate></xsl:sequence>

    </xsl:template>



    <xsl:mode on-no-match="shallow-skip"/>



</xsl:stylesheet>

Current Thread