This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: position() within for-each usage


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]