Page 1 of 1

position() counts two at a time

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

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

Code: Select all


<realFrame>
<realComplement id="v11c1" status="empty">
<role>eA2+</role>
<ref target="v10c1"/>
</realComplement>
<realComplement id="v11c2">
<role>SRC</role>
<case>DatLoc!</case>
</realComplement>
<realComplement id="v11c3" status="empty">
<role>P2+</role>
<ref target="V0">part/possession of speaker</ref>
</realComplement>
</realFrame>
This is the template I use:

Code: Select all


<xsl:template match="realComplement">
<xsl:variable name="compNr" select="position()"/>
<xsl:number></xsl:number>
<xsl:text>realComplement-Nummer</xsl:text>
<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,
Frank

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 http://www.w3.org/TR/xslt#built-in-rule 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="*"/>
</xsl:template>
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()"/>
</xsl:template>
and you can see that you get with this the same results as you posted.

Best Regards,
George

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

best regards,
Frank