xsl-list
[Top] [All Lists]

RE: increment variable with xsl:for-each

2004-03-26 11:33:03
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="http://www.w3.org/1999/XSL/Transform";
version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
  <xsl:call-template name="incrementValue">
    <xsl:with-param name="value">1</xsl:with-param>
  </xsl:call-template>
</xsl:template>
<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"/>
      </xsl:call-template>
    </xsl:if>
</xsl:template>
</xsl:stylesheet>

Will output:

12345678910

Best of luck!

<M:D/>

-----Original Message-----
From: Tuan Luu [mailto:tuanluu(_at_)gmx(_dot_)ch] 
Sent: Friday, March 26, 2004 10:59 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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}"/>

</xsl:for-each>


thanks for any answer


-- 
+++ NEU bei GMX und erstmalig in Deutschland: TÜV-geprüfter Virenschutz
+++
100% Virenerkennung nach Wildlist. Infos: http://www.gmx.net/virenschutz


--+------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
You are subscribed as: m(_dot_)david(_at_)mdptws(_dot_)com
To unsubscribe, go to:
http://lists.mulberrytech.com/unsub.php/xsl-list/m(_dot_)david(_at_)mdptws(_dot_)com
or e-mail:
<mailto:xsl-list-unsubscribe-m(_dot_)david=mdptws(_dot_)com(_at_)lists(_dot_)mulberrytech(_dot_)com>
--+--