cancel
Showing results for 
Search instead for 
Did you mean: 

Sum of variables using XSLT

Former Member
0 Kudos

Hi All,

I am stuck in finding sum of variables using XSLT.

I have a field D_Txt which is series of numbers separated by spaces under S_Txt ( S_Txt can repeat).

I need to get the sum of first numbers(Numbers before first space) in all D_Txt that can repeat whenever S_Txt repeat.

<xsl:variable name="tot" /> (global declaration)

<xsl:template match="/">

<xsl:for-each select="Root/S_Txt>

.........

..........

<xsl:variable name="x" select="substring-before(D_Txt,' ')"/>

<xsl:variable name="tot" select="$tot + $x"/>

</xsl:for-each>

Total is: <xsl:value-of select="$tot"/>

</xsl:template>

With the above code I get the 'tot' value as NaN. When I am assigning "0" to 'tot' variable, each time when loop repeats "0" is getting assigned to 'tot' variable. But I need the sum of all values that comes in 'x' variable.

Can you resolve this problem.

With Regards,

Lakshmi.

Edited by: Venkata Lakshmi Kante on Jan 23, 2008 10:02 AM

Accepted Solutions (0)

Answers (2)

Answers (2)

aashish_sinha
Active Contributor
0 Kudos

Hi

refer these two links :

/people/aashish.sinha/blog/2008/01/17/step-150-by-150-step-simple-approach-for-xslt-mapping

/people/aashish.sinha/blog/2008/01/21/xslt-mapping-for-multiple-segments-of-xml

regards

Aashish Sinha

PS : reward points if helpful

Former Member
0 Kudos

Hi Lakshmi,

a variable in XSLT is NOT a variable in programmatic sense.

In XSLT you can only apply a value to a variable ONCE!

If a value is applied you cannot change it!

But I am sure you could solve your problem using a recursion

http://www.ibm.com/developerworks/xml/library/x-xslrecur/

Regards Mario

Edited by: Mario Müller on Jan 23, 2008 6:09 AM