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

Re: xsl:variable

Subject: Re: xsl:variable
From: RSuiter@xxxxxxxxx
Date: Tue, 12 Jun 2001 15:36:51 -0500
xslt variable scope for each
Jeff:

Where to begin?

The first thing you need to know is that in XSLT, variables are not
variable.
Once you set them with your select statement, that is the value until the
variable goes out of scope.
The reason why this must be so is that XSLT is not a procedural language.

So why do you get the results you get?
Because what you really have is four variables (speaking slightly loosely).
The first has scope of the whole template.  The others have scope only for
each iteration of the "for-each".
Think of it as if the inner variables (in the "for-each") hide the higher
level variable, so you can't see it within the for-each.
<aside>I thought that it wasn't even legal in XSLT to have multiple
variables of the same name and overlapping scope, but that some processors
still allowed it</aside>..

What you need instead is to reformulate your problem so that you're not
relying on stepping through the XML.
Typical would be something like
<xsl:choose>
  <xsl:when test="...">
    <!-- put out the right number of "Size is present"  text nodes -->
  </xsl:when>
  <xsl:otherwise>
    <!-- put out one "Size is not present" text node -->
  </xsl:otherwise>
</xsl:choose>

I hope that helps.

Rick Suiter

>Can someone tell me why the following doesn't work?

><xsl:template name="myTemplate">
> <xsl:variable name="sizePresent" select="'no'"/>
> <xsl:for-each select="attribute">
>  <xsl:if test="@type='Size'">
>   <xsl:variable name="sizePresent" select="'yes'"/>
>   Size is present.
>  </xsl:if>
> </xsl:for-each>

> <xsl:if test="$sizePresent='no'">
>  Size was never found.
> </xsl:if>
></xsl:template>




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


Current Thread
  • xsl:variable
    • Jeff Fletcher - Tue, 12 Jun 2001 16:01:27 -0400 (EDT)
      • <Possible follow-ups>
      • RSuiter - Tue, 12 Jun 2001 16:39:37 -0400 (EDT) <=

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.