[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Newbie wonders why the variable can change value?


Subject: Re: [xsl] Newbie wonders why the variable can change value?
From: Brandon Ibach <brandon.ibach@xxxxxxxxxxxxxxxxxxx>
Date: Mon, 21 Feb 2011 17:54:15 -0500

On Mon, Feb 21, 2011 at 4:53 PM,  <thehulk@xxxxxxxxxxx> wrote:
> The template that processes the sibling ACCT uses a "choose" to determine
the level of that ACCT and does much the same to nest BRANs within SUBS:
>
> <xsl:variable name="currentSU" select="HIERARCHY_Info/@Acct_ID"/>
> <xsl:apply-templates  mode="RE"
select="../ACCT[HIERARCHY_Info/@Prnt_Acct_ID=$currentSU]"/>
>
> All that works, but, it seems to me that it is resetting the variable
$currentSU for each of the ACCTs that are Subsidiary-level. But you cannot
reset a variable, it says on all the tutorials. Why does it work?

Every variable exists within a particular "scope".  For a "global"
variable, defined outside of any template, the scope is the entire
transform, and it will only ever have one value during the entire run.
 For other variables, the scope is the parent element, such as an
xsl:template, xsl:for-each, or even an xsl:if.  The variable only
exists until the end of that scope and only has one value during that
time.

However, every time that scope is executed, the variable gets a
different value, generally because the scope is executing with a
different context node, etc.  Think of a template as a function.  The
value you get out of it will depend on the parameters you pass in
(including the context) and the local variables within that function
will take on different values during the course of its execution, as
well.  The only difference (compared to many programming languages) is
that the variable's value is fixed until the end of the function.

Hope this makes things clearer...

-Brandon :)


Current Thread