| Author |
How to add two variables in XSLT
|
Anil Vupputuri
Ranch Hand
Joined: Oct 31, 2000
Posts: 527
|
|
Hi All, I've two variables var1 and var2, how can I add them up. For eg., var1 = 5 var2 = 5 var3 = var1 + var2 = 10 <xsl:value-of name="var3" select="$var1+$var2"/> doesn't work. Thanks.
|
SCJP 1.5, SCEA, ICED (287,484,486)
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
Quote from the XPath Recommendation:
The + operator performs addition.
I think your example doesn't work because the xsl:value-of element can't have a "name" attribute. Later: Oh, I see, you want to know how to assign the sum to a variable named "var3". You use the <xsl:variable> element to declare a variable and assign a value to it. Remember that these two must always be done at the same time in XSLT, you can't declare a variable and later assign it a value. So: [ May 24, 2006: Message edited by: Paul Clapham ]
|
 |
Anil Vupputuri
Ranch Hand
Joined: Oct 31, 2000
Posts: 527
|
|
Thanks Paul. But the following piece of code doesn't work. How to get total from var1 and var2?
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
|
Could you provide some useful information other than "doesn't work"?
|
 |
Anil Vupputuri
Ranch Hand
Joined: Oct 31, 2000
Posts: 527
|
|
Here is the more descriptive code, When we tried to print the value of "total-ytd-max-installed", its giving nothing. Following is the code to display the value, Thanks.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
Ah well, finally we have the real question. You didn't notice what I said before, did you?
"Remember that these two must always be done at the same time in XSLT, you can't declare a variable and later assign it a value."
Let's look at your code...The standard way to assign the sum of some things to a variable in XSLT is via a recursive method. Here's an article that explains that sort of thing: http://www-128.ibm.com/developerworks/xml/library/x-xslrecur/ "Recursion Example 1" is quite similar to what you want to do.
|
 |
Clifton Craig
Ranch Hand
Joined: May 26, 2006
Posts: 103
|
|
Originally posted by Anil Vupputuri: [QB]Here is the more descriptive code,
Also I didn't see an alternative condition in your code. What do you do when $ytd-max-installed is equal to 'NA'? I would use xsl:choose to ensure $total-ytd-max-installed had a value. Something like this: Also it looks like maybe you would want to increment $total-ytd-max-installed. You probably want to declare $total-ytd-max-installed outside the loop and set it with something like: That's from the top of my head and untested but I hope you get the idea.
|
Holla at me...<br /><a href="http://codeforfun.wordpress.com" target="_blank" rel="nofollow">http://codeforfun.wordpress.com</a>
|
 |
 |
|
|
subject: How to add two variables in XSLT
|
|
|