xsl-list
[Top] [All Lists]

RE: Setting values for variable

2004-09-09 14:52:32
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(_at_)onebox(_dot_)com - email



-----Original Message-----
From:     john lee <excel_man(_at_)hotmail(_dot_)com>
Sent:     Thu, 09 Sep 2004 21:03:40 +0000
To:       xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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


--+------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--+--