我被困在尝试计算xslt 2.0中的总和(数量)*价格

时间:2017-02-14 10:49:25

标签: xml xslt

首先,我的任务是按ProviderName对元素进行分组,按@ItemNumber对数量求和,并计算总数=数量*价格。我无法进行此计算,我收到错误。这是我到目前为止所做的。任何帮助将不胜感激。

    <?xml version="1.0"?>
    <Items>
          <Item ItemNumber="1148089">
            <ProductName>Plexi Leather-Silver-Black</ProductName>
            <ProviderName>Vestal Watches</ProviderName>
            <Quantity>1</Quantity>
            <Price>189.99</Price>
          </Item>
          <Item ItemNumber="1251465">
            <ProductName>Cherub Baby Bottle Kit - Berry</ProductName>
            <ProviderName>Cherub Baby</ProviderName>
            <Quantity>3</Quantity>
            <Price>42.99</Price>
          </Item>
          <Item ItemNumber="1351468">
            <ProductName>M ES MOC OX BITTER CHOCOLATE</ProductName>
            <ProviderName>Rockport</ProviderName>
            <Quantity>7</Quantity>
            <Price>72.99</Price>
          </Item>
          <Item ItemNumber="1151464">
            <ProductName>Spritz Grape Seat and Extra Seat</ProductName>
            <ProviderName>Bambeano</ProviderName>
            <Quantity>1</Quantity>
            <Price>56.99</Price>
          </Item>
          <Item ItemNumber="1251464">
            <ProductName>Apple Pattern T-Shirt-Blue</ProductName>
            <ProviderName>Avahna</ProviderName>
            <Quantity>1</Quantity>
            <Price>14.99</Price>
          </Item>
          <Item ItemNumber="1351464">
            <ProductName>W ZANA BIKE FRONT OXFORD SATIN NICKEL</ProductName>
            <ProviderName>Rockport</ProviderName>
            <Quantity>1</Quantity>
            <Price>59.99</Price>
          </Item>
          <Item ItemNumber="1251464">
            <ProductName>Cherub Baby Bottle Kit - Citrus</ProductName>
            <ProviderName>Cherub Baby</ProviderName>
            <Quantity>1</Quantity>
            <Price>42.99</Price>
          </Item>
    </Items>

这是我的XSLT

<xsl:stylesheet version="2.0" 
        xmlns:ex="http://exslt.org/common"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="Items">
        <!-- TODO: Auto-generated template -->
        <html>
            <head>
                <link rel="stylesheet" type="text/css" href="Item.css" />
                <title>Sales Amount by Provider</title>
            </head>
            <body>
                <h1>Sales Amount by Provider</h1>

                <table>
                <xsl:for-each-group select="Item" group-by="ProviderName">
                    <xsl:sort select="ProviderName"/>
                    <tr>
                        <th colspan="4">Provider: <xsl:value-of select="ProviderName"/></th>
                    </tr>

                    <tr id="title">
                        <th>Item Number</th> 
                        <th>Quantity</th>
                        <th>Unit Price </th>
                        <th>Total</th>
                    </tr>
                    <xsl:for-each-group select="current-group()" group-adjacent="@ItemNumber">
                    <xsl:sort select="@ItemNumber"/>
                    <tr>

                        <td> 
                            <xsl:value-of select="distinct-values(current-group()/@ItemNumber)"/>
                        </td>

                         <td>  
                            <xsl:value-of select="sum(current-group()/Quantity)"/>
                        </td>

                        <td>  
                            <xsl:value-of select="distinct-values(current-group()/Price)"/>
                        </td>
                        <td>
                            <xsl:variable name="total">
                                <xsl:for-each select="current-group()/Item">
                                    <product>
                                        <xsl:value-of select="sum(current-group()/Quantity * current-group()/Price)" />     
                                    </product>
                                </xsl:for-each>
                            </xsl:variable>
                            <xsl:value-of select="sum(ex:node-set($total)/product)"/>
                        </td>

                    </tr>
                    </xsl:for-each-group>
                    <tr>
                        <th id="SubGrandtotal" colspan="3"><b>Sub-total</b></th>
                        <!-- <td>  
                            <xsl:value-of select="sum(current-group()/Price)"/> -->
                            <!-- <xsl:variable name="quantity" select="sum(current-group()/Quantity)" as="xs:double" />
                            <xsl:variable name="price" select="current-group()/Price/text()" />
                            <xsl:value-of select="format-number($price*$quantity, '#0.00')"/> -->
                        <!-- </td> -->

                    </tr>

                </xsl:for-each-group>
                    <tr>
                        <th id="SubGrandtotal" colspan="3"><b>Grand-total</b></th>
                    </tr>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

如果你正在使用XSLT 2.0,那么你可以简单地使用:

<xsl:value-of select="sum(current-group()/(Quantity * Price))" /> 

对于两个分组级别,并且:

<xsl:value-of select="sum(Item/(Quantity * Price))" /> 

总计。