RE: [xsl] counter in nested for-eachs

Subject: RE: [xsl] counter in nested for-eachs
From: David Neary <David@xxxxxxxxx>
Date: Thu, 15 May 2003 16:25:06 +0200

This is a FAQ? the answer is that there is no way to modify a variable.
However...

> i have two or three nested for-each elements, is it possible to
> have a global counter for all of them?
> 
> i would like that with every loop in every for-each a number
> is incremented by one..

... you can do it like this.

<xsl:for-each select="stuff">
  <xsl:variable name="stuffcount" select="count()"/>
  <xsl:variable name="stuffpos" select="position() - 1"/>
   <xsl:for-each select="more">
    <xsl:variable name="morecount" select="count()"/>
    <xsl:variable name="morepos" select="position() - 1 + $stuffpos *
$stuffcount"/>
     <xsl:for-each select="again">
      <xsl:variable name="againpos" select="position() - 1 + $morepos *
$morecount"/>
      <xsl:text>Current position: </xsl:text>
      <xsl:value-of select="$againpos" />
    </xsl:for-each>
  </xsl:for-each>
</xsl:for-each>

... but this is, as you can see, very messy.

Cheers,
Dave.

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


Current Thread