[xsl] position() in sequences in XSLT 2.0

Subject: [xsl] position() in sequences in XSLT 2.0
From: ben.pickering@xxxxxxxxxxxxxxxx
Date: Thu, 15 Jul 2004 10:00:53 +0100
Hi

I have a document looking like the following, which I'm using to 
generate/populate a web form. 

<entries>
        <e task="1000">
            <text>Task One Thousand</text>
        </e>

        <e task="2000">
            <text>Task Two Thousand</text>
        </e>

        <e task="3000">
            <text>Task Three Thousand</text>
        </e>
</entries>

I also want to generate a few empty ones at the end to be filled in by the 
user.  So I'm doing the following with a temporary tree variable which 
matches the structure of the input:

    <xsl:variable name="empty">
          <e task="">
              <text></text>
          </e>
    </xsl:variable>

    <xsl:template match="/">
        <xsl:apply-templates select="entries/e, for $d in (1 to 3) return 
$empty"/>
    </xsl:template>

    <xsl:template match="e">
       entry <xsl:value-of select="position()"/> of <xsl:value-of 
select="last()"/><br />
       <!-- ...plus the form output, not important -->
    </xsl:template>

And the result I'm getting is

entry 1 of 6
entry 2 of 6
entry 3 of 6
entry 1 of 1
entry 1 of 1
entry 1 of 1

Whereas I was expecting "1 of 6" up to "6 of 6".  So it looks right for 
the first ones, but then seems to go wrong...  I was under the impression 
that position() is the offset into the sequence and that last() is the 
overall size.  Is this wrong?

I notice that if I swap the template for / with

    <xsl:template match="/">
        <xsl:variable name="temp">
           <xsl:copy-of select="entries/e, for $d in (1 to 3) return 
$empty"/>
        </xsl:variable>
        <xsl:apply-templates select="$temp"/>
    </xsl:template>

Then it works fine.   I'm sort of satisfied to do it this way, but I'd 
like to check I have my facts straight as regards sequences in XSLT 2.0.

Cheers,
Ben.

Current Thread