Franco
Franco

Reputation: 21

How to sum variables in xslt

I'm new in XSLT, and I'm having problems to solve this. I have some values that aren't in the XML document, so I need to create them, like this:

   <xsl:for-each select="msxsl:node-set($obsData)/Round">
    <xsl:with-param name="Save1">
       <xsl:variable name="xx">
        <xsl:element name="lasuma">
         <xsl:value-of select="(some calculation)"/>
        </xsl:element>
       </xsl:variable>  
       <xsl:value-of select="msxsl:node-set($xx)/lasuma"/> 
    </xsl:with-param>
   </xsl:for-each>

So now that I have the values that I need, I save them into the variable. Now I need to sum it, but the only way that the function SUM seems to works is when you have nodes, and I can't sum the values in a variable. I tried this, but it doesn't sum the values that I specified in that path:

..."sum(msxsl:node-set($xx)/lasuma)"/>

Is there a way to sum the values in a variable? A simple example that I might follow, would be much appreciated. Thanks.

Upvotes: 0

Views: 7210

Answers (2)

John Ernst
John Ernst

Reputation: 1235

Try this. Just make sure your calculation creates a valid number. If not, you will get NaN, "not a number", returned from your sum.

<xsl:variable name="xx">
  <xsl:for-each select="msxsl:node-set($obsData)/Round">
    <xsl:element name="lasuma">
     <xsl:value-of select="(some calculation)"/>
    </xsl:element>
  </xsl:for-each>
</xsl:variable>

<xsl:value-of select="sum(msxsl:node-set($xx)/lasuma)"/> 

Upvotes: 0

Sojimanatsu
Sojimanatsu

Reputation: 601

if you want to sum any variable you have to say that variable is a number.

So in XSLT you can use number(value)+number(otherValue)

Upvotes: 3

Related Questions