This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: xsl:variable



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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]