I need to convert the XML data to XHTML for which
I'm using XSL to filter some of the data. Now the issue is that i need to add values in some elements and get the total
sum. I'm not getting the solution fr it. Since i've just started with XSL I don't know the intricacies.
The data is suppose,
<DATA>Rs. 100/-</DATA>
<DATA>Rs. 250.45/-</DATA>
Now i need to add these values and get 350.45 as the result.
Can this be done without using Java extensions or Javascript
in between the XSL file. Any help/suggestions...
-
Issues with data-arithmetic in XSL (4 messages)
- Posted by: Manish Gupta
- Posted on: April 13 2005 01:42 EDT
Threaded Messages (4)
- u can use pure xmlt for sum: by razvan redcom on April 13 2005 02:04 EDT
- Issues with data-arithmetic using XSL by Manish Gupta on April 13 2005 10:17 EDT
- One way to do this by Neil Laurance on April 13 2005 11:02 EDT
- Problem Solved ! by Manish Gupta on April 14 2005 04:54 EDT
-
u can use pure xmlt for sum:[ Go to top ]
- Posted by: razvan redcom
- Posted on: April 13 2005 02:04 EDT
- in response to Manish Gupta
try the sum function in xslt
search google, i do not remember , i read that last night in a book
there is a simple sum function: sum (xsl-value-of ".") i do not remember exactly u 1th have to math for data
xsl-math "DATA or something -
Issues with data-arithmetic using XSL[ Go to top ]
- Posted by: Manish Gupta
- Posted on: April 13 2005 10:17 EDT
- in response to razvan redcom
Hi Razvan,
Thanks fr ur concern. Pls make the solution clear.
Sum function is a XPath function and it adds only the values
i.e, if the data is
<DATA>100</DATA>
<DATA>500</DATA>
<DATA>400</DATA>
then sum() will give the result 1000, but as i already mentioned my data is alphanumeric,ie., it contains strings like "Rs." and some special characters too.
I hope now the problem is clear to you.
Pls do respond to this if u're aware of the solution.
Thanks once again. -
One way to do this[ Go to top ]
- Posted by: Neil Laurance
- Posted on: April 13 2005 11:02 EDT
- in response to Manish Gupta
Here is one way, based on the computational stylesheet XSLT patter from the Wrox XSLT book.
If you have an XML file like:
[code]
<root>
<DATA>Rs. 100/-</DATA>
<DATA>Rs. 250.45/-</DATA>
</root>
[/code]
Then try the following:
[code]
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="text" indent="no"/>
<xsl:template match="root">
<xsl:call-template name="total-numbers">
<xsl:with-param name="list">
<xsl:for-each select="DATA">
<xsl:text> </xsl:text><xsl:value-of select="substring-before(substring-after(., 'Rs. '), '/-')"/>
</xsl:for-each>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
<xsl:template name="total-numbers">
<xsl:param name="list"/>
<xsl:variable name="wlist" select="concat(normalize-space($list), ' ')"/>
<xsl:choose>
<xsl:when test="$wlist!=' '">
<xsl:variable name="first" select="substring-before($wlist, ' ')"/>
<xsl:variable name="rest" select="substring-after($wlist, ' ')"/>
<xsl:variable name="total">
<xsl:call-template name="total-numbers">
<xsl:with-param name="list" select="$rest"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="number($first) + number($total)"/>
</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
[/code]
Hope this helps. Cheers, Neil -
Problem Solved ![ Go to top ]
- Posted by: Manish Gupta
- Posted on: April 14 2005 04:54 EDT
- in response to Neil Laurance
Hey Neil,
Thanks for the superb logic !
The problem is finally solved.
Regards.
Manish