ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> xsl-list
xsl-list
RE: [xsl] Setting values for variable
by other posts by this author
Sep 9 2004 9:52PM messages near this date
Re: AW: [xsl] xhtml output formating problems / passing through pre defined static xhtml | Re: AW: AW: AW: [xsl] xhtml output formating problems / passing through pre defined static xhtml
XSL variables cause a lot of confusion to those comming from other, more usual programming b
ackgrounds. 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 s
ame 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@[...].com - email



-----Original Message-----
From:     john lee <excel_man@[...].com> 
Sent:     Thu, 09 Sep 2004 21:03:40 +0000
To:       xsl-list@[...].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@[...].com> 
--+--




--+------------------------------------------------------------------
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@[...].com> 
--+--

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved