• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to add two variables in XSLT

 
Ranch Hand
Posts: 527
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic