Subject: Re: [xsl] 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) |
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] variable going out of sco, Eric Smith | Thread | Re: [xsl] variable going out of sco, Christopher R. Maden |
Re: [xsl] variable going out of sco, Eric Smith | Date | RE: [xsl] xsl:if question, Michael Kay |
Month |