首页 > 解决方案 > 如何在 XSL 中使用 sum 函数

问题描述

我需要一些关于 xsl 中 sum 函数的帮助。

首先,我在 XML 中拆分为 3 个国家/地区的 3 个表。

现在我在每个表中添加一行,对于每个表我需要使用“求和函数”并计算所有价格的总和,这是我现在的代码,我做错了什么?我很乐意得到你的帮助,谢谢。

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>

结果如下:

在此处输入图像描述

标签: xmlxslt

解决方案


由于您想对一个组进行求和,因此您需要指定它。以下代码适用于您:

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

推荐阅读