Re: [xsl] xsl:variable

Subject: Re: [xsl] xsl:variable
From: RSuiter@xxxxxxxxx
Date: Tue, 12 Jun 2001 15:36:51 -0500

Where to begin?

The first thing you need to know is that in XSLT, variables are not
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:when test="...">
    <!-- put out the right number of "Size is present"  text nodes -->
    <!-- put out one "Size is not present" text node -->

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-List info and archive:

Current Thread