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

RE: [xsl] Scope of variable defined in


Subject: RE: [xsl] Scope of variable defined in <xsl:choose>
From: Joshua.Kuswadi@xxxxxxxxxxxxxxxxxxxx
Date: Mon, 21 May 2001 12:54:07 +1000

Hi Tim,

For your first example you have declared the variable 'numberdisplayed' within the when and within the otherwise. Therefore, it is out of scope when you try to select its value after the choose.

One solution I've seen around (Check out http://www.dpawson.co.uk/xsl/xslfaq.html for heaps of great XSLT examples) is to use:

<xsl:variable name="numberdisplayed">
	<xsl:choose>
		<xsl:when test="data/merchant/customer">
			<xsl:value-of select="count(data/merchant/customer)" />
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="count(data/merchant/orders)" />
		</xsl:otherwise>
	</xsl:choose>
</xsl:variable>

This will set the variable based on the choose, ready for later use.

Cheers,
Joshua

> I can't understand this error message
> 
> > Error [code:222] [node:
> > element '<xsl:value-of>']
> >   variable not found
> > search/search - text/html
> 
> for the following code:
> 
> > <xsl:choose>
> >	<xsl:when test="data/merchant/customer">
> >		<xsl:variable name="numberdisplayed"><xsl:value-of
> select="count(data/merchant/customer)" /></xsl:variable>
> >	</xsl:when>
> >	<xsl:otherwise>
> >		<xsl:variable name="numberdisplayed"><xsl:value-of
> select="count(data/merchant/orders)" /></xsl:variable>
> >	</xsl:otherwise>
> > </xsl:choose>
> > <xsl:value-of select="$numberdisplayed" />
> 
> The variable has just been defined, but it can't be used 
> outside of the
> <xsl:choose>
> 
> Yet, if I have
> 
> > <xsl:choose>
> >	<xsl:when test="/document/data/merchant/customer">
> >		<xsl:variable name="numberdisplayed"><xsl:value-of
> select="count(/document/data/merchant/customer)" />
> > </xsl:variable>
> >		<xsl:value-of select="$numberdisplayed" />
> >	</xsl:when>
> >	<xsl:otherwise>
> >		<xsl:variable name="numberdisplayed"><xsl:value-of
> select="count(/document/data/merchant/orders)" />
> > </xsl:variable>
> >		<xsl:value-of select="$numberdisplayed" />
> >	</xsl:otherwise>
> > </xsl:choose>
> 
> the output is displayed.
> 
> How can the variable be used throughout the scope of the 
> template it has
> been defined in?
> 
> Thanks,
> 
> Tim Watts
> 
> PS. Am using Sablotron 0.51.
> 
> 
> 
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 
> 

------------------------------------------------------------------------------
This message and any attachment is confidential and may be privileged or otherwise protected from disclosure.  If you have received it by mistake please let us know by reply and then delete it from your system; you should not copy the message or disclose its contents to anyone.





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



Current Thread
Keywords