Re: [xsl] increment variable with xsl:for-each

Subject: Re: [xsl] increment variable with xsl:for-each
From: Tuan Luu <tuanluu@xxxxxx>
Date: Fri, 26 Mar 2004 23:18:49 +0100
Thanks David.

M. David Peterson wrote:

Using a recursive call-template does the trick..  Basically you call a
template from within one template with a parameter that is set to the
beginning of the increment.  From within that template you test to see
if the value of the param meets whatever your criteria is and if it does
you output whatever it is you want and then call the same template, this
time incrementing the value of the included parameter by whatever you
want by adding the necessary arithmetic within the select statement e.g.
"$value + 1"

So this XSLT:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="";
<xsl:output method="html"/>
<xsl:template match="/">
 <xsl:call-template name="incrementValue">
   <xsl:with-param name="value">1</xsl:with-param>
<xsl:template name="incrementValue">
 <xsl:param name="value"/>
   <xsl:if test="$value &lt;= 10">
     <xsl:value-of select="$value"/>
     <xsl:call-template name="incrementValue">
       <xsl:with-param name="value" select="$value + 1"/>

Will output:


Best of luck!


-----Original Message-----
From: Tuan Luu [mailto:tuanluu@xxxxxx] Sent: Friday, March 26, 2004 10:59 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] increment variable with xsl:for-each

hello I'm looking for a code to increment a variable in xsl:for-each loop

I ended up here:

<xsl:variable name="a">1</xsl:variable>

<xsl:for-each select="name">

<xsl:value of select="{$a+1}"/>
<xsl:value of select="{$a}"/>


thanks for any answer

Current Thread