[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

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

Subject: Re: variable going out of scope in a for-each loop?
From: Mike Brown <mike@xxxxxxxx>
Date: Sun, 9 Dec 2001 16:22:36 -0700 (MST)
xsl variables 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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.