XSLT新手。难以合计某些值

时间:2016-01-14 00:39:43

标签: xml xslt

我是XSLT的新手,我发现很难弄清楚如何计算从两个元素(价格和数量)生成的所有小计。

任何帮助表示感谢(包括指出我迄今为止所做的比特效率低下的代码,我是新手,还在学习,所以我需要帮助)

XML

<Items>
<Item ItemNumber="1251469">
    <ProductName>Cherub Baby 240ml Single - Light Blue</ProductName>
    <ProviderName>Cherub Baby</ProviderName>
    <Quantity>25</Quantity>
    <Price>7.99</Price>
</Item>
<Item ItemNumber="1148087">
    <ProductName>Dolby Metal-Black-Matte</ProductName>
    <ProviderName>Vestal Watches</ProviderName>
    <Quantity>4</Quantity>
    <Price>67.99</Price>
</Item>
<Item ItemNumber="1351465">
    <ProductName>M OR PERFED PLAINTOE BLACK</ProductName>
    <ProviderName>Rockport</ProviderName>
    <Quantity>2</Quantity>
    <Price>96.99</Price>
</Item>
<Item ItemNumber="1150197">
    <ProductName>Vercilli Blk-Tan</ProductName>
    <ProviderName>Boston Babes</ProviderName>
    <Quantity>1</Quantity>
    <Price>23.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="1148087">
    <ProductName>Dolby Metal-Black-Matte</ProductName>
    <ProviderName>Vestal Watches</ProviderName>
    <Quantity>2</Quantity>
    <Price>67.99</Price>
  </Item>
  <Item ItemNumber="1150197">
    <ProductName>Vercilli Blk-Tan</ProductName>
    <ProviderName>Boston Babes</ProviderName>
    <Quantity>2</Quantity>
    <Price>23.99</Price>
  </Item>
  <Item ItemNumber="1150173">
    <ProductName>Lucille Tan</ProductName>
    <ProviderName>Boston Babes</ProviderName>
    <Quantity>1</Quantity>
    <Price>24.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="1148089">
    <ProductName>Plexi Leather-Silver-Black</ProductName>
    <ProviderName>Vestal Watches</ProviderName>
    <Quantity>1</Quantity>
    <Price>189.99</Price>
  </Item>
  <Item ItemNumber="1148096">
    <ProductName>Observer-Black-Silver-White</ProductName>
    <ProviderName>Vestal Watches</ProviderName>
    <Quantity>1</Quantity>
    <Price>82.99</Price>
  </Item>
  <Item ItemNumber="1151464">
    <ProductName>Spritz Grape Seat and Extra Seat</ProductName>
    <ProviderName>Bambeano</ProviderName>
    <Quantity>20</Quantity>
    <Price>56.99</Price>
  </Item>
  <Item ItemNumber="1151470">
    <ProductName>Elegant Chandelier Pearl Drop Earrings - Champaign</ProductName>
    <ProviderName>Bella Krystal</ProviderName>
    <Quantity>1</Quantity>
    <Price>39.99</Price>
  </Item>
  <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

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

<xsl:template match="/">

<html>
<head>
    <title>Sales amount by provider</title>
</head>
<body>
<h2>Sales amount by provider</h2>
<table border="1">
    <xsl:for-each-group select="/Items/Item" group-by="ProviderName">
    <xsl:sort select="ProviderName"/>
        <tr bgcolor="#99cc00">
            <td colspan="4">Provider: <xsl:value-of select="current-grouping-key()"/></td>
        </tr>
        <tr>
            <td>Item Number</td>
            <td>Quantity</td>
            <td>Unit Price</td>
            <td>Total</td>
        </tr>
        <xsl:for-each-group select="current-group()" group-by="@ItemNumber">
        <xsl:sort select="@ItemNumber"/>
        <tr>
            <td><xsl:value-of select="@ItemNumber"/></td>
            <td><xsl:value-of select="sum(current-group()/Quantity)"/></td>
            <td><xsl:value-of select="Price"/></td>
            <td><xsl:value-of select="round(Price * (sum(current-group()/Quantity )))"/></td>
            </tr>
        </xsl:for-each-group>
        <xsl:variable name="SubTotals">
           <xsl:for-each-group select="current-group()" group-by="@ItemNumber">
              <subTotal><xsl:value-of select="round(Price * (sum(current-group()/Quantity )))"/></subTotal>
           </xsl:for-each-group>
        </xsl:variable>
        <tr>
            <td colspan="3" align="right"><b>Sub-total</b></td>
            <td>
            <TotalValue>
                <xsl:value-of select="sum($SubTotals/subTotal)"/>
            </TotalValue>
            </td>
        </tr>
    </xsl:for-each-group>
    <tr>
        <td colspan="3" align="right"><b>Grand-total</b></td>
        <td>
            <!-- The last bit I cant figure out -->
        </td>
    </tr>
</table>
</body>
</html>

</xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

请试试这个:

<tr>
    <td colspan="3" align="right"><b>Grand-total</b></td>
    <td>
        <xsl:value-of select="sum(for $item in (/Items/Item) return $item/Quantity * $item/Price)" />
    </td>
</tr>

它使用for expression计算每个小计,并使用sum函数来计算总计。