Re: [xsl] How to define a global parameter and change its value

Subject: Re: [xsl] How to define a global parameter and change its value
From: "Thomas B. Passin" <tpassin@xxxxxxxxxxxx>
Date: Wed, 13 Feb 2002 12:56:47 -0500
[Chi Lin]

> I would like to use a value from one of my template to pass to a
> different template.
> I tried to declare a global parameter and assign the value in the
> template, so I can use it in a different template.
> I can assign the value for the parameter in the template, but the
> following template can not pick the changed value.  It still contains
> the original value that I have initialized.  It seems to me that the
> "global parameter" does not really work, the local one is always
> "shadow" the global one.
> Is there anyway that I can change the global parameter's value after it
> has been declared, and used in different templates?

Once a variable or parameter has been assigned within its scope, it cannot
be changed. Once created, it is read-only, as you have found.  But that does
not mean you cannot achieve what you want.  You say that you want to assign
the value of the parameter in a template and pass it to another template.
You can do that without using a global template.

A parameter can be declared again each time a template is instantiated
because it is in a different scope each time (that is, the scope of the
instantiated template).  A typical way to do this is like so:

<xsl:template match='...'>
    <xsl:apply-templates select='xxx'><!--or xsl:call-template-->
        <xsl:with-param name='yyy' select='....'/>

<!-- In the called template, pick up the parameter like this:-->
<xsl:template match='xxx'>
    <xsl:param name='yyy'/>
    <!--rest of template goes here-->

The point to to always create and pass a parameter each time you need it.
This even works when you call a template recursively (i.e., a template calls

This approach does what you said you want to do.


Tom P

 XSL-List info and archive:

Current Thread