Re: [xsl] Problems incrementing a variable in a <xsl:for-each>

Subject: Re: [xsl] Problems incrementing a variable in a <xsl:for-each>
From: "Vasu Chakkera" <vasucv@xxxxxxxxxxx>
Date: Wed, 23 Oct 2002 15:15:38 +0100
Hi Carlos,
It wont work the way you thought it will work.. you can not incriment the
variables in XSLT.
it's not possible in XSLT, because you can't reassign the value of a
variable..
You may use other techniques like recursive templates to solve your
problem..
>
> In the second XSL file, the counter procedes as planned. But in the first
> XSL the counter mantains
The following is what is happening
1st XSL.
<xsl:variable name="counter" select="0"/>
> <xsl:template match='/CLIENTS'>
>   <xsl:for-each select="./CLIENT">
>     <xsl:variable name="counter" select="1 + $counter"/>
>     <xsl:value-of select="$counter"/>
>   </xsl:for-each>
> </xsl:template>
> </xsl:stylesheet>

The local counter Variable allways is set  to a value which is
1+$counter(global)..

In your second XSL .. It is an error to define twice , variables of same
name within same template...
Vasu


----- Original Message -----
From: "Carlos Barroso" <est-c-barroso@xxxxxxxxxxxxx>
To: <XSL-List-Digest@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Wednesday, October 23, 2002 2:42 PM
Subject: [xsl] Problems incrementing a variable in a <xsl:for-each>


> Hy there.
> I'm having problems incrementing a variable in a <xsl:for-each> section.
> Below is the XML document, the stylesheet I used and the output I got.
>
> -----------------------
> XML file
> -----------------------
>
> <?xml version='1.0' encoding='ISO-8859-1'?>
> <!DOCTYPE CLIENTS SYSTEM 'counter.dtd'>
>
> <CLIENTS>
>   <CLIENT>
>     <NAME value='xpto'/>
>     <AGE value='10'/>
>   </CLIENT>
>   <CLIENT>
>     <NAME valor='xxx'/>
>     <AGE valor='20'/>
>   </CLIENT>
> </CLIENTS>
>
> ----------------------
> First XSL file
> ----------------------
>
> <?xml version='1.0' encoding='ISO-8859-1'?>
>
> <xsl:stylesheet version='1.0'
>   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
> <xsl:output method='text'/>
>
> <xsl:variable name="counter" select="0"/>
> <xsl:template match='/CLIENTS'>
>   <xsl:for-each select="./CLIENT">
>     <xsl:variable name="counter" select="1 + $counter"/>
>     <xsl:value-of select="$counter"/>
>   </xsl:for-each>
> </xsl:template>
> </xsl:stylesheet>
>
> -------------------------
> Output in TXT file
> -------------------------
>
> 11
>
> -------------- // --------------
>
> ------------------------
> Second XSL test
> ------------------------
>
> <?xml version='1.0' encoding='ISO-8859-1'?>
>
> <xsl:stylesheet version='1.0'
>   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
> <xsl:output method='text'/>
>
> <xsl:variable name="counter" select="0"/>
> <xsl:template match='/CLIENTS'>
>     <xsl:variable name="counter" select="1 + $counter"/>
>     <xsl:value-of select="$counter"/>
>     <xsl:variable name="counter" select="1 + $counter"/>
>     <xsl:value-of select="$counter"/>
> </xsl:template>
> </xsl:stylesheet>
>
> -------------------------
> Output in TXT file
> -------------------------
>
> 12
>
> -------------- // --------------
>
> In the second XSL file, the counter procedes as planned. But in the first
> XSL the counter mantains
> it's value!? I tried using templates instead of "<xsl:for-each>" but it
> gives me the same results!
> I don't know what's wrong.
> Can someone help me please. This is very weird to me.
> Thanks a lot.
>
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>

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


Current Thread