• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to add two variables in XSLT

 
Anil Vupputuri
Ranch Hand
Posts: 527
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 527
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul.

But the following piece of code doesn't work.



How to get total from var1 and var2?
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you provide some useful information other than "doesn't work"?
 
Anil Vupputuri
Ranch Hand
Posts: 527
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic