RE: [xsl] Retaining value of a Global variable

Subject: RE: [xsl] Retaining value of a Global variable
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Fri, 16 Jul 2004 22:17:29 +0100
You've got the wrong end of the stick, I'm afraid. You have a global
variable called whead whose value is NO, and a local variable called whead
whose value is YES. The two variables are quite unrelated to each other.
Declaring a local variable cannot change the value of a global variable.
Variables in XSLT, as in other functional languages, are immutable, and you
cannot use them for procedural programming in the way you are attempting.

If you're trying to output the value NO for the first article, and YES for
subsequent articles, then the way to do that is

<xsl:when test="position()=1">NO</xsl:when>

You seem to be confused about disable-output-escaping as well. Not only is
it a good thing that XSLT processors escape special characters on output,
it's also architecturally really bad news to try and prevent it. But YES and
NO don't contain any characters that need escaping, so using this
sledgehammer to prevent the processor behaving normally is not only
disrupting the design of your application, it's also in this case totally

Michael Kay 

> -----Original Message-----
> From: Vivek Shinde [mailto:Vivek.Shinde@xxxxxxx] 
> Sent: 16 July 2004 22:04
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Retaining value of a Global variable
> Hi,
> I have a global variable defined at the top of my stylesheet. 
> When I try to use it subsequently it gets reinitialized. 
> Here's the code.
>  <xsl:variable name="whead" select="'No'" />
> <xsl:template match="/newsletter">
> .
> .
>       <xsl:apply-templates select="content/article" 
> mode="proc_webcastheader" />.
> </xsl:template>
> <xsl:template match="article" mode="proc_webcastheader">
> <xsl:variable name="artclass" 
> select="normalize-space(metadata/article-classification/.)" />
> <xsl:if test="contains($artclass,'Biz-Webcast')">
>           <p><xsl:value-of disable-output-escaping="yes" 
> select="$whead"/></p>
>           <IMG 
> src="";></IMG>
>           <xsl:variable name="whead" select="'Yes'" />
>           <p><xsl:value-of disable-output-escaping="yes" 
> select="$whead"/></p>
> </xsl:if>
> </xsl:template>
> In the second template when I print whead before the <img> it 
> shows 'No' for all articles even if I set it to 'Yes' after 
> the <img>. Any clues on how to retain the value of the variable?
> Thanks
> Vivek

Current Thread