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

Re: [xsl] Variables


Subject: Re: [xsl] Variables
From: Mukul Gandhi <mukulw3@xxxxxxxxx>
Date: Fri, 1 Aug 2003 04:57:39 -0700 (PDT)

Hi Jose,
  Others have correctly pointed that variable scoping
is limited to the context in question. If you declare
a variable in xsl:for-each loop, you cannot access it
outside the xsl:for-each loop. Unfortunately,
variables don't propagate between contexts in XSLT..
For e.g. if you declare a variable x before
xsl:for-each loop , and again declare the same
variable x in xsl:for , then when control goes outside
xsl:for , you don't get the value that was assigned in
xsl:for-each .. Outside xsl:for-each, you will refer
to x that was declared before xsl:for-each .. *This
behaviour for variables sis imposed in XSLT to give a
side effect free programming*..

>From the description of your problem, you may do some
workarounds to access value generated in xsl:for-each
,outside the for-each..

for e.g. you may use xsl:call-template as below

<xsl:for-each select="something">
   <xsl:variable name="x" select="."/>
      <xsl:call-template name="printX">
	<xsl:with-param name="temp" select="$x"/>
      </xsl:call-template>
</xsl:for-each>

<xsl:template name="printX">
  <xsl:param name="x"/>
  <xsl:value-of select="$x"/>
</xsl:template>

There can be other workarounds also as others have
pointed.. If you tell us your exact problem, somebody
here can tell you the best way to solve in XSLT..

Regards,
Mukul


--- AROSO Jose Antonio <jose.santos@xxxxxxxxxxx>
wrote:
> Hi..
> 
> 
> When I save something in a variable in a loop
> for-each i dont have access to
> this variable out of the loop for-each.
> How can i resolve this situation.
> In the following example:
> 
> <xsl:for-each select="Attribute_Groups/Attribute">
> 
> 	<xsl:variable name="aname" select="@Name" />
> 	........
> </xsl:for-each>
> 
> How can i access the variable aname out of the loop
> for-each?
> 
> 
> Best Regards
> 
> Jose
> 
> 
> 
> 
> 
>  XSL-List info and archive: 
> http://www.mulberrytech.com/xsl/xsl-list
> 


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

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



Current Thread
Keywords