RE: [xsl] position() within for-each usage

Subject: RE: [xsl] position() within for-each usage
From: "Michael Kay" <michael.h.kay@xxxxxxxxxxxx>
Date: Thu, 6 Jun 2002 19:19:27 +0100
The only "position" that's meaningful in XSLT is the position of the
context node within the context node list (the list of nodes currently
being processed). So you can't talk about the position of a node other
than the context node.

What you probably want is the value of count(../preceding-sibling::*)+1
that is, the number of elements that are preceding siblings of the
parent, plus one. 

Michael Kay
Software AG
home: Michael.H.Kay@xxxxxxxxxxxx
work: Michael.Kay@xxxxxxxxxxxxxx 

> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx 
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of Nuri Besen
> Sent: 06 June 2002 18:04
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] position() within for-each usage
> 
> 
> Hello,
> 
> I am trying to use the position function to indicate the line 
> I am currently 
> processing but I want the position of the parent node.  If 
> the data looks 
> like:
> 
> <data>
>   <item>
>     <name>xyz</name>
>     <lineitem>
>       <cost>24</cost>
>     <lineitem>
>       <cost>30</cost>
>   <item>
>     <name>abc</name>
>     <lineitem>
>       <cost>24</cost>
>     <lineitem>
>       <cost>30</cost>
> 
> and I use a for loop as to output HTML:
> 
> <xsl:template match="item">
>   <xsl:for-each select="lineitem">
>     For item #<xsl:value-of select="position()">
>     <xsl:value-of select="../name">
>     the cost for lineitem is <xsl:value-of select="cost">
>   </xsl:for-each>
> </xsl:template>
> 
> Here, the output I want should have the item position rather than the 
> lineitem position but because of the location it gives me 
> lineitem position, 
> which is correct within the loop construct.  So how can I get 
> the parent 
> position?
> I tried ../self::node()[position()] but no good.
> 
> Sample output should be:
> For item #1 xyz the cost for lineitem is 24
> For item #1 xyz the cost for lineitem is 30
> For item #2 abc the cost for lineitem is 24
> For item #2 abc the cost for lineitem is 30
> 
> BUT it gives:
> For item #1 xyz the cost for lineitem is 24
> For item #2 xyz the cost for lineitem is 30
> For item #1 abc the cost for lineitem is 24
> For item #2 abc the cost for lineitem is 24
> 
> I know I could have used two templates to match rather than a 
> loop but in 
> the real scenario I need to use the loop.
> 
> Any help is appreciated....
> 
> Thanks,
> Nuri
> 
> P.S.:  If there is a syntax error, i just typed it in fast, 
> it is the logic 
> that i am trying to portray.
> 
> _________________________________________________________________
> Join the world?s largest e-mail service with MSN Hotmail. 
> http://www.hotmail.com
> 
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 


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


Current Thread