[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Scope of variables

Subject: RE: Scope of variables
From: "Passin, Tom" <tpassin@xxxxxxxxxxxx>
Date: Thu, 8 May 2003 15:29:49 -0400
xslt variable scope
(Sorry, accidentally sent my reply too soon, so it was incomplete )

[Karl J. Stubsjoen]

> 1)  If I define a variable at the top of a template rule and 
> then later try
> to update the value of this variable later down in the 
> template, I always
> get the error "Can't define variable twice...".  So how do 
> you reassign a
> value to a variable.
> 

Karl, **Google is your friend** - the first hit for "xslt faq variable
scope" returned a good link : 

http://www.xml.com/pub/a/2002/06/26/q-a.html

You cannot change the value of an xslt variable once it has been
assigned.  You have to get the results you want some other way, and
there are other ways.  One way is to pass parameters to templates.  Each
time the template gets invoked, it gets passed a new value in the
parameter.

> 2) About scope:  Within a for-each loop I have a choose.  In 
> the choose I
> have a single test and an otherwise.  The result of the 
> choose is setting
> the value of a variable.  Just below this choose logic I am 
> referencing the
> value but am getting the error "A reference to variable or parameter
> 'currentVAL' cannot be resolved".  Where did I lose scopE on 
> this variable
> (currentVAL)?

The scope of the variable is the enclosing block, which in this case are
"when" or "otherwise" blocks.  This question comes up all the time on
the list.  Just check the FAQs and pay attention to some of the other
threads.  Outside its scope the variable is not available.

In this case, where you do not actually need a node set but only a
string, you can just put the choose inside the variable definition -

<xsl:variable name='x'>
<xsl:choose>
   <xsl:when>
...
</xsl:choose>
</xsl:variable>

Cheers,

Tom P


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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.