[xsl] Re: xsl:variable

Subject: [xsl] Re: xsl:variable
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Thu, 6 Mar 2003 20:12:22 +0100
> i would like to write something like this :
>
> <xsl:variable name="counter" select="0"/>
> <xsl:for-each select="toto">
>     <xsl:variable name="counter" select="$counter+1"/>
> </xsl:for-each>
>
> i know that i'm wrong but how could i do that?

You cannot increment (or modify in any way) the assigned value of an
xsl:variable, because xslt is a functional language.

However you could do:

  <xsl:for-each select="toto">
      <xsl:variable name="someName" select="position()"/>
  </xsl:for-each>

Note also, that the "someName" variable goes out of scope outside the body
of the xsl:for-each element.

As per the current draft of XSLT 2.0 the code proposed by you will not raise
an error! ... but it will not do at all what you're expecting -- the value
of $counter inside the body of xsl:for-each will always be 1.


Hope this helped.


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL



"Laurence Mossuz" <mossuz@xxxxxxxxxxxxx> wrote in message
news:3E6776DF.5CE65BC8@xxxxxxxxxxxxxxxx
> hello
>
> i would like to declare a variable counter in my xsl file and to
> incremente it whenever i have the occurence toto in my XML file.
> toto is a complextype whose maxOccurs="unbounded" in the schema
> declaration.
> For instance we can have in the xml file :
> <test>
>     <toto>
>       <titi/>
>       <tata/>
>     </toto>
>     <toto>
>      <titi>hello</titi>
>       <tata>bye</tata>
>     </toto>
> </test>
>
> i would like to write something like this :
>
> <xsl:variable name="counter" select="0"/>
> <xsl:for-each select="toto">
>     <xsl:variable name="counter" select="$counter+1"/>
> </xsl:for-each>
>
> i know that i'm wrong but how could i do that?
>
> Many thanks for your help!!
>
> Lo.
> --
>
> Laurence MOSSUZ
>
> LAPP Service Informatique
> Chemin de Bellevue BP 110
> 74941 Annecy-Le-Vieux CEDEX
> Tel : 04 50 09 17 31 _ Fax : 04 50 27 94 95
>
>
>
>  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