[xslt] position() and apply-templates creating a copy



[please CC replies to cactus@cactus.rulez.org]

I'm having problems using position() in my style sheets, so I created some
test cases to better understand what's going on. Both of them gave
surprising results so I'm asking for some explanation.

#1, position()
Given the following XML file:

<?xml version="1.0"?>
<root>
  <foo/>
  <foo/>
  <foo/>
</root>

and the following style sheet:

  <xsl:template match="foo">
    <xsl:message>
      <xsl:text>This is foo #</xsl:text>
      <xsl:value-of select="position()"/>
    </xsl:message>   
  </xsl:template>

I expect the result to be 
This is foo #1
This is foo #2
This is foo #3

instead, the following is shown:
This is foo #2
This is foo #4
This is foo #6

#2, apply-templates operates on a copy?
Using the same XML file and the following style sheet:

  <xsl:template match="foo">
    <xsl:message>
      <xsl:text>This is foo #</xsl:text>
      <xsl:value-of select="position()"/>
    </xsl:message>   
    <xsl:apply-templates select="." mode="nest"/>
  </xsl:template>

  <xsl:template match="foo" mode="nest">
    <xsl:message>
      <xsl:text>(nested tmpl) This is foo #</xsl:text>
      <xsl:value-of select="position()"/>
    </xsl:message>
  </xsl:template>

I get the following output:

This is foo #2
(nested tmpl) This is foo #1
This is foo #4
(nested tmpl) This is foo #1
This is foo #6
(nested tmpl) This is foo #1


Thanks in advance for explanations,
	Gergo

-- 
   .--= ULLA! =---------------------.   `We are not here to give users what
   \     http://cactus.rulez.org     \   they want'  -- RMS, at GUADEC 2001
    `---= cactus@cactus.rulez.org =---'
A szinoníma olyan szó, amit akkor használsz, ha nem tudsz valamit helyesen leírni.




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]