Saturday, 15 May 2010

xslt - xsl:variable scope in xsl:for-each -



xslt - xsl:variable scope in xsl:for-each -

this question in context of xsl 1.0; because shadowing of local variables not allowed.

given xsl variables lexically scoped, don't understand why example, taken michael kay's book (p. 338 in 2/e , p. 509 in 4/e) works:

xml:

<program> <opera> <title>the magic flute</title> <composer>mozart</composer> <date>1791</date> </opera> <opera> <title>ernani</title> <composer>verdi</composer> <date>1843</date> </opera> ... <composer name="mozart"> <fullname>wolfgang amadeus mozart</fullname> <born>1756</born> <died>1791</died> </composer> ... </program>

xsl:

... <xsl:for-each select="program/composer"> <h2><xsl:value-of select="fullname"/></h2> <xsl:variable name="c" select="."/> <xsl:for-each select="program/opera[composer=$c/@name]"> <p><xsl:value-of select="title"/></p> </xsl:for-each> </xsl:for-each> ...

why wouldn't case of declaring variable c multiple times in same lexical context? i'm missing something; xsl:for-each creates multiple virtual lexical contexts? starts sense more dynamic scoping, though. must not thinking right way.

i find michael kay has answers, (or @ to the lowest degree i) must re-read things before you/i it. in case, explanation of effect of :

the effect of <xsl:for-each> instruction instantiate template body contains 1 time each node in selected node-set.

so, that's answer: local variable lexically scope inside each virtual template generated <xsl:for-each>.

this still feels little weird me; it's not lexical or dynamic scoping. figured out prior posting question, went ahead posted anyway in case comes along same confusion had.

xslt xslt-1.0

No comments:

Post a Comment