RE: [xsl] Setting values for variable

Subject: RE: [xsl] Setting values for variable
From: cknell@xxxxxxxxxx
Date: Thu, 09 Sep 2004 17:52:32 -0400
XSL variables cause a lot of confusion to those comming from other, more usual programming backgrounds. Variables get a value assigned exactly once, and that's it. You cannot re-assign the value as in "x = x + 1". To add to the confusion, you can have two variables with the same name so long as they exist in different contexts. Your "pterm" looks like it has global scope, so you will get a problem if you try to re-delcare it in a context where the original one has scope. Well, never mind, it will only lead to more confusion until you get the hang of it.

Rewrite your code as follows:

<xsl:variable name="pterm" select="0"/>
<xsl:for-each select="$lstAccount[Account_Type=$grpRecord]">
    <xsll:choose>
      <xsl:when test="string-length(normalize-space(Payment))!=0">
        <xsl:variable name="pterm1" select="$pterm + Payment"/>
      </xsl:when>
    <xsl:otherwise>
       <xsl:variable name="pterm1" select="$pterm + 0.02*Balance"/>
    </xsl:otherwise>
   </xsl:choose>
</xsl:for-each>
<xsl:value-of select="$pterm1"/>

-- 
Charles Knell
cknell@xxxxxxxxxx - email



-----Original Message-----
From:     john lee <excel_man@xxxxxxxxxxx>
Sent:     Thu, 09 Sep 2004 21:03:40 +0000
To:       xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  [xsl] Setting values for variable

Dear All,

How do you set a value for a variable without declaring the name of the 
variable twice.
This is what I intend to do.

<xsl:variable name="pterm" select="0"/>
<xsl:for-each select="$lstAccount[Account_Type=$grpRecord]">
    <xsll:choose>
      <xsl:when test="string-length(normalize-space(Payment))!=0">
        <xsl:variable name="pterm" select="$pterm + Payment"/>
      </xsl:when>
    <xsl:otherwise>
       <xsl:variable name="pterm" select="$pterm + 0.02*Balance"/>
    </xsl:otherwise>
   </xsl:choose>
</xsl:for-each>
<xsl:value-of select="$pterm"/>

But this will generate error since pterm is declared more than once.
Anybody know what's the other way around this ?

Thanks

_________________________________________________________________
The new MSN 8: smart spam protection and 2 months FREE*  
http://join.msn.com/?page=features/junkmail

Current Thread