[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] position of non actual element


Subject: Re: [xsl] position of non actual element
From: Oliver Becker <obecker@xxxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 27 Feb 2002 09:30:25 +0100 (MET)

Hi Rudolf,

> I'm having problem with position().
> I need position of node which is not tested now, eg.
> <data>
>   <value attr="first">...</value>
>   <value attr="second">...</value>
> </data>
> 
> <!--xsl:value-of select="position(data/value[@attr = 'second'])"/-->
> of course this cannot run :-))

position() returns always the position of the current node within the
context node list. There's nothing like a "global position".

For example: if you have
<xsl:template match="value[@attr='second']">
   <xsl:value-of select="position()" />
</xsl:template>
you might think to get always the same value. 

No, that's not the case. position depends, as I said, on the context
node list. I.e.
<xsl:apply-templates select="//*" />
<xsl:apply-templates select="/data/value" />
<xsl:apply-templates select="/data/value[@attr='second']" />
gives three different numbers.

So, what do you mean by "position"?
The position of the value element among its value siblings?
Then just count the preceding siblings and add 1:
<xsl:value-of
     select="count(data/value[@attr='second']/preceding-sibling::value)+1" />
     
Or alternatively you could use the power of <xsl:number />:
<xsl:for-each select="data/value[@attr='second']">  
<!-- this only changes the context node -->
   <xsl:number />
</xsl:for-each>

Cheers,
Oliver


/-------------------------------------------------------------------\
|  ob|do        Dipl.Inf. Oliver Becker                             |
|  --+--        E-Mail: obecker@xxxxxxxxxxxxxxxxxxxxxxx             |
|  op|qo        WWW:    http://www.informatik.hu-berlin.de/~obecker |
\-------------------------------------------------------------------/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



Current Thread