Issues with data-arithmetic in XSL


XML & Web services: Issues with data-arithmetic in XSL

  1. Issues with data-arithmetic in XSL (4 messages)

    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...

    Threaded Messages (4)

  2. u can use pure xmlt for sum:[ Go to top ]

    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
  3. 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
    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.
  4. One way to do this[ Go to top ]

    Here is one way, based on the computational stylesheet XSLT patter from the Wrox XSLT book.

    If you have an XML file like:
        <DATA>Rs. 100/-</DATA>
        <DATA>Rs. 250.45/-</DATA>

    Then try the following:

    <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:template name="total-numbers">
        <xsl:param name="list"/>
        <xsl:variable name="wlist" select="concat(normalize-space($list), ' ')"/>
            <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:value-of select="number($first) + number($total)"/>


    Hope this helps. Cheers, Neil
  5. Problem Solved ![ Go to top ]

    Hey Neil,
    Thanks for the superb logic !
    The problem is finally solved.