Re: [xsl] Variable scope when using imported stylesheets

Subject: Re: [xsl] Variable scope when using imported stylesheets
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 24 Aug 2004 16:48:42 -0400
Note that what David says is even true of local variables, which are bound within their context (and thus may be different every time a template is matched), e.g.

<xsl:template match="list">
  <xsl:variable name="linecount" select="count(child::line)"/>
  <xsl:text>&#xA;Line count: </xsl:text>
  <xsl:value-of select="$linecount"/>
</xsl:template>

$linecount will be different for every list, but for any given match of the template (that is, for any given list), it's always the same. The variables "vary" because the context varies.

As David has had to point out many times, this is just like algebra:

statement: x + y = 5
context: y = 4

So x=1. But if the context gave us y=3, x would be different. (It's always 5 - y but we need to know what y is to know x; or if the context gives us x, we know y.)

In XSLT, global variables are assigned within the processing context given by the document. Local variables are assigned within the processing context of the template (which could be the body of a for-each instruction).

Cheers,
Wendell

At 09:56 AM 8/24/2004, David wrote:
It's not totally clear what your requirements are but I think that you
are making the mistake (common for some reason) of thinking of variables
as holding Xpath expressions, they don't; they hold _values_ (node sets
in your case) so if you have a variable binding such as

<xsl:variable
   name="ImpVehData"
   select="FileRefEval/FileRefVehData"
/>


Then the value of $ImpVehData is whatever node set results in evaluating that xpath __at the point of the declaration__. In common with most programming languages, XSLT can not store an arbitrary expression in a variable that you may re-evaluate, getting different results in different contexts.

the smallest unit that you can re-evaluate in that way is a named
template. Calling a named template will cause the body of the template
to be evaluate in a context determined by the call.

___&&__&_&___&_&__&&&__&_&__&__&&____&&_&___&__&_&&_____&__&__&&_____&_&&_
"Thus I make my own use of the telegraph, without consulting
the directors, like the sparrows, which I perceive use it
extensively for a perch." -- Thoreau


Current Thread