Re: recursive passing parameter

Subject: Re: recursive passing parameter
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 07 Sep 1999 13:05:35 -0400
At 99/09/07 11:50 -0400, Honglin Su wrote:
I want the following stylesheet to produce an output like this:

 The variable value before passing is:: init value ...
 The variable value after passing is :: A1
 The variable value before passing is:: A1
 The variable value after passing is :: B1
 The variable value before passing is:: B1
...
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0";>
<xsl:variable name="var" select="'init value ...'" />
<!-- process the children recursively -->
<xsl:template match="*">
 The variable value before passing is:: <xsl:value-of select="$var"/>
 <xsl:param name="var" select="name(.)"/>
 The variable value after passing is :: <xsl:value-of select="$var"/>
 <xsl:apply-templates/>
</xsl:template>
...
But the actually output is that every before passing value is 'init
value ...". What's wrong with my stylesheet?

The stylesheet is giving you what you are asking for because the scope of "$var" at the beginning of <xsl:template> is the globally scoped variable, not the locally scoped variable.


Once you declare the $var within the template, subsequent references in that template are to that locally scoped variable.

Perhaps your misunderstanding is the expectation that the value of the variable $var actually varies ... it does not vary. The <xsl:param> is declaring a variable and is supplying a default value to bind to that variable if the <xsl:apply-templates> or <xsl:call-templates> doesn't provide a <xsl:with-param> value as an overriding value to bind.

Once a value is bound to the variable, it does not change.

What you are seeing is a question of the scope of your variable reference, not an issue of variable values.

I hope this helps.

............ Ken



--
G. Ken Holman                    mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.             http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999   (Fax:-0995)
Website:  XSL/XML/DSSSL/SGML services, training, libraries, products.
Practical Transformation Using XSLT and XPath      ISBN 1-894049-01-2
Next instructor-led training:  1999-09-24, 1999-11-08, 1999-12-05/06,
                             1999-12-07, 2000-02-27/28, 2000-05-11/12



XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list


Current Thread