This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: variable going out of scope in a for-each loop?


Eric Smith wrote:
> xsl fragment:
> 
>      <xsl:for-each select="cell">
>          <xsl:if test="number(.)=number(.)">
>            We have a number here 

I think you meant 

           <xsl:if test="string(.) = number(.)">

>          <xsl:choose>
>            <xsl:when test="position() mod 2 = 0">
>              mod 2 = 0!
>              <xsl:variable name="aantal_uren">
>                <xsl:value-of select="."/>
>              </xsl:variable>
>              The value of the aantal_uren is: <xsl:value-of select="$aantal_uren"/>||
>              The value of the uur_tarief is: <xsl:value-of select="$uur_tarief"/>||
>            </xsl:when>
>            <xsl:otherwise>
>              mod 2 != 0!
>              <xsl:variable name="uur_tarief">
>                <xsl:value-of select="."/>
>              </xsl:variable>
>              The value of the aantal_uren is: <xsl:value-of select="$aantal_uren"/>||
>              The value of the uur_tarief is: <xsl:value-of select="$uur_tarief"/>||
>            </xsl:otherwise>
>          </xsl:choose>
>          </xsl:if>
>          ....
> 
> Now, as soon as I loop form the first number containing cell to the next, the 
> value defined in the previous-sibling seems to go out of scope viz.

That's correct; XSLT is a functional, declarative language that strives to
be free of side-effects. You are expecting it to act like a procedural
language with side-effects. Variables are immutable and have limited scope
in XSLT. They don't exist "off to the side", free to be created, mutated and
destroyed; every XSLT instruction can be thought of as a mathematical function
that always returns the same results when given the same input, so only those
variables that are in scope before the function is instantiated are avaliable
to it.

Your problem can most likely be addressed without relying upon the knowledge of
what was calculated in the previous iteration by recalculating the
value you would have gotten (perhaps based on a variable you set prior to 
entering the for-each. Or it can be addressed using a recursive named template
that calls itself repeatedly until some condition is satisfied, on each iteration
passing as paramters its results and a trimmed-down node-set to act upon.

   - Mike
____________________________________________________________________________
  mike j. brown, fourthought.com  |  xml/xslt: http://skew.org/xml/
  denver/boulder, colorado, usa   |  personal: http://hyperreal.org/~mike/

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]