Discussions

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
    <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.
  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:
    [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
  5. Problem Solved ![ Go to top ]

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