[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: position() within for-each usage

Subject: Re: position() within for-each usage
From: Mike Brown <mike@xxxxxxxx>
Date: Thu, 6 Jun 2002 11:40:00 -0600 (MDT)
xslt position position function
Nuri Besen wrote:
> 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.

position() returns the position of the current node in the current node list
(the set of nodes that were selected for processing by xsl:apply-templates 
or xsl:for-each).

The 'position' of the parent node is something altogether different because it 
is relative to some other list of nodes that only you know about. For example, 
maybe you want the position of the parent relative to its element siblings, or 
relative to all its siblings (regardless of type), or relative to its 
ancestors...

> 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?

Stick it in a variable before you change the context:

<xsl:template match="item">
  <xsl:variable name="itempos" select="position()"/>
  <xsl:for-each select="lineitem">
    <xsl:value-of select="concat('For item #',$itempos,' ',../name,
     ' the cost for lineitem is ',cost,'&#10;')"/>
  </xsl:for-each>
</xsl:template>

   - Mike
____________________________________________________________________________
  mike j. brown                   |  xml/xslt: http://skew.org/xml/
  denver/boulder, colorado, usa   |  resume: http://skew.org/~mike/resume/

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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.