Page 1 of 1

position() counts two at a time

Posted: Mon May 15, 2006 4:51 pm
by muewi

I try to use position() to give special treatment to the first element of a sequence, the first <realComplement>

Code: Select all

<realComplement id="v11c1" status="empty">
<ref target="v10c1"/>
<realComplement id="v11c2">
<realComplement id="v11c3" status="empty">
<ref target="V0">part/possession of speaker</ref>
This is the template I use:

Code: Select all

<xsl:template match="realComplement">
<xsl:variable name="compNr" select="position()"/>
<xsl:value-of select="$compNr"></xsl:value-of>
But the output for $compNr is 2,4,6, whereas the output for xsl:number is 1,2,3.

Any ideas what is going wrong here?

Any hints greatly appreciated,

Posted: Mon May 15, 2006 7:53 pm
by george
Hi Frank,

The position() function will return the possition of that node in the list of nodes the templates are applied on, that is it depends on how you called apply-templates. You should be aware that although they are not visible when you look at the XML document there are some whitespace only text nodes between your elements.

By default if you have only a template as in your example then it will be activated by an apply-templates from a built-in rule (see for details) and that means that the default select value is used and that is node() and there are selected not only the elements inside realFrame but also other nodes, in this case the text whitespace only nodes I mentione above. If you add a template like

Code: Select all

  <xsl:template match="realFrame">
<xsl:apply-templates select="*"/>
to select only elements in apply-templates then you will get the same results for position as you get with xsl:number.

The built-in code that is executed in your case is

Code: Select all

  <xsl:template match="realFrame">
<xsl:apply-templates select="node()"/>
and you can see that you get with this the same results as you posted.

Best Regards,

Posted: Tue May 16, 2006 10:54 am
by muewi
Thanks, a lot it works!

best regards,