Shablul
Shablul

Reputation: 5

How to use sum function in XSL

i need some help with sum function in xsl.

first of all i split to 3 tables for 3 countrys i have in XML.

now i add a row in each table and for each table i need use "sum function" and calculate the sum of all prices, here is my code for now, what im doing wrong? I would be happy for help from you, thx.

XML:

<?xml version="1.0" encoding="ISO8859-1"?>
<?xml-stylesheet href="style.xsl" type="text/xsl" ?>
<Albums>
    <Album>
        <Name>Empire Burlesque</Name>
        <Artist>Bob Dylan</Artist>
        <Country>USA</Country>
        <Company>Columbia</Company>
        <Price>10.20</Price>
        <Date>19880610</Date>
    </Album>
    <Album>
        <Name>Hide your heart</Name>
        <Artist>Bonnie Tylor</Artist>
        <Country>UK</Country>
        <Company>CBS Records</Company>
        <Price>9.90</Price>
        <Date>19880509</Date>
    </Album>
    <Album>
        <Name>Greatest Hits</Name>
        <Artist>Dolly Parton</Artist>
        <Country>USA</Country>
        <Company>RCA</Company>
        <Price>9.90</Price>
        <Date>1982</Date>
    </Album>
    <Album>
        <Name>Still got the blues</Name>
        <Artist>Gary More</Artist>
        <Country>UK</Country>
        <Company>Virgin redords</Company>
        <Price>10.20</Price>
        <Date>1990</Date>
    </Album>
    <Album>
        <Name>Eros</Name>
        <Artist>Eros Ramazzotti</Artist>
        <Country>EU</Country>
        <Company>BMG</Company>
        <Price>9.90</Price>
        <Date>1997</Date>
    </Album>
    <Album>
        <Name>25</Name>
        <Artist>Adele</Artist>
        <Country>UK</Country>
        <Company>XL Recordings</Company>
        <Price>9.90</Price>
        <Date>20151120</Date>
    </Album>
    <Album>
        <Name>1000 Forms of Fear</Name>
        <Artist>Sia</Artist>
        <Country>USA</Country>
        <Company>RCA Records</Company>
        <Price>9.90</Price>
        <Date>20140704</Date>
    </Album>
    <Album>
        <Name>Rattle and Hum</Name>
        <Artist>U2</Artist>
        <Country>EU</Country>
        <Company>Island</Company>
        <Price>9.90</Price>
        <Date>19881010</Date>
    </Album>
</Albums>

XSL:

<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:key name="country" match="Album" use="Country" />
    <xsl:template match="/Albums">
        <html>
        <body>
            <center>
            <xsl:for-each select="Album[generate-id() = generate-id(key('country',Country)[1])]">
            <u style="color:#39c639" ><h2 id="{generate-id(Country)}">
                <xsl:value-of select="Country" /></h2></u>
                <table border="2" bgcolor="transparent">
                    <tr bgcolor="grey">
                        <th>Artist</th>
                        <th>Album</th>
                        <th>Company</th>
                        <th>Link</th>
                        <th>Price</th>
                    </tr>
                    <xsl:for-each select="key('country',Country)">
                        <xsl:sort select="Date" order="ascending"/>                          
                    <tr>
                        <td style="color:#ff0000"><xsl:value-of select="Artist" /></td>
                        <td><xsl:value-of select="Name" /></td>
                        <td><xsl:value-of select="Company" /></td>
                        <td><xsl:value-of select="Company" /></td>
                        <td><xsl:value-of select="Price" /></td>
                    </tr>
                    </xsl:for-each>

                    <tr >
                        <td bgcolor="yellow">Total Price: </td>
                        <td ></td><td ></td><td ></td>
                        <td bgcolor="yellow"><xsl:value-of select="sum(Album/Price)" /></td>
                    </tr>
                </table>
            </xsl:for-each>
            </center>
        </body>    
        </html>
    </xsl:template>
    </xsl:stylesheet>

And here is the result:

enter image description here

Upvotes: 0

Views: 1099

Answers (2)

Douglas Martins
Douglas Martins

Reputation: 86

Since you want to do a sum over a group, you need to specify it. The below code works for you:

<xsl:value-of select="format-number(sum(key('country', Country)/Price), '#.##')"/>

Upvotes: 1

michael.hor257k
michael.hor257k

Reputation: 116959

If you want to sum the Price of the current group, you need to use the key to select the group:

<xsl:value-of select="sum(key('country', Country)/Price)" />

Upvotes: 0

Related Questions