我正在XSL 1.0中进行分组,我希望对所有分组的“Amount”元素进行总结。
我的数据看起来像这样。
<CustInvoiceTable class="entity">
<McsCmBilCalcInvoiceLine class="entity">
<Amount>0.05</Amount>
<ItemId>ITM-0000088</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>0.07</Amount>
<ItemId>ITM-0000088</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>0.16</Amount>
<ItemId>ITM-0000091</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>0.22</Amount>
<ItemId>ITM-0000091</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>1.33</Amount>
<ItemId>ITM-0000098</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>1.82</Amount>
<ItemId>ITM-0000098</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>0.01</Amount>
<ItemId>ITM-0000086</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>0.01</Amount>
<ItemId>ITM-0000086</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>0.05</Amount>
<ItemId>ITM-0000062</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>2</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>0.06</Amount>
<ItemId>ITM-0000062</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>2</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>0.02</Amount>
<ItemId>ITM-0000111</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>0.02</Amount>
<ItemId>ITM-0000111</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>0.06</Amount>
<ItemId>ITM-0000089</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>0.08</Amount>
<ItemId>ITM-0000089</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>1.96</Amount>
<ItemId>ITM-0000092</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>2.69</Amount>
<ItemId>ITM-0000092</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>0.07</Amount>
<ItemId>ITM-0000101</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>0.10</Amount>
<ItemId>ITM-0000101</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>0.07</Amount>
<ItemId>ITM-0000102</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>0.10</Amount>
<ItemId>ITM-0000102</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>6.69</Amount>
<ItemId>ITM-0000083</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>7.96</Amount>
<ItemId>ITM-0000083</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>0.15</Amount>
<ItemId>ITM-0000067</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>0.21</Amount>
<ItemId>ITM-0000067</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>1</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>96.00</Amount>
<ItemId>ITM-0000125</ItemId>
<McsCmBilProductItem class="entity">
<CgiBundleLines>0</CgiBundleLines>
</McsCmBilProductItem>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>-88.00</Amount>
<ItemId>ITM-0000069</ItemId>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>-0.66</Amount>
<ItemId>ITM-0000083</ItemId>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>-0.22</Amount>
<ItemId>ITM-0000092</ItemId>
</McsCmBilCalcInvoiceLine>
<McsCmBilCalcInvoiceLine class="entity">
<Amount>-0.55</Amount>
<ItemId>ITM-0000098</ItemId>
</McsCmBilCalcInvoiceLine>
</CustInvoiceTable>
和XSL:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="Lines-by-ItemId" match="CustInvoiceTable/McsCmBilCalcInvoiceLine" use="CustInvoiceTable/McsCmBilCalcInvoiceLine/ItemId" />
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<root>
<xsl:for-each select="CustInvoiceTable/McsCmBilCalcInvoiceLine[count(. | key('Lines-by-ItemId', ItemId)[1]) = 1][McsCmBilProductItem/CgiBundleLines = 1][not(ItemId = preceding-sibling::McsCmBilCalcInvoiceLine/ItemId)]">
<xsl:sort select="ItemId" />
<GroupInvoiceLine>
<ItemId><xsl:value-of select="ItemId" /></ItemId>
<SumAmount><xsl:value-of select="sum(Amount)"/></SumAmount> <!-- This does not work -->
</GroupInvoiceLine>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
结果: 假设我们对“ITM-0000088”进行分组,我希望结果为0.12。目前它只是抓住了第一张唱片。
如果我们对“ITM-0000083”进行分组,我预计总和为6.69 + 7.96 = 14.65而不是6.69 + 7.96 + -0.66 = 13.99,因为最后一个'具有'ItemId = ITM-0000083'的'McsCmBilCalcInvoiceLine'没有在'McsCmBilProductItem'中有'CgiBundleLines = 1'。
答案 0 :(得分:0)
首先,您的密钥应定义为:
<xsl:key name="Lines-by-ItemId" match="McsCmBilCalcInvoiceLine" use="ItemId" />
然后改变:
<SumAmount><xsl:value-of select="sum(Amount)"/></SumAmount>
为:
<SumAmount><xsl:value-of select="sum(key('Lines-by-ItemId',ItemId)/Amount)"/></SumAmount>
答案 1 :(得分:0)
将密钥定义为<xsl:key name="Lines-by-ItemId" match="CustInvoiceTable/McsCmBilCalcInvoiceLine" use="ItemId" />
,因为use
属性值是相对于匹配节点计算的。
然后首先看看如何使用密钥和Muenchian分组,使用
<xsl:for-each select="CustInvoiceTable/McsCmBilCalcInvoiceLine[count(. | key('Lines-by-ItemId', ItemId)[1]) = 1]">
<xsl:sort select="ItemId" />
<GroupInvoiceLine>
<ItemId><xsl:value-of select="ItemId" /></ItemId>
<SumAmount><xsl:value-of select="sum(key('Lines-by-ItemId', ItemId)/Amount)"/></SumAmount>
</GroupInvoiceLine>
</xsl:for-each>
至于其他条件,请考虑将其包含在关键模式中:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="Lines-by-ItemId" match="CustInvoiceTable/McsCmBilCalcInvoiceLine[McsCmBilProductItem/CgiBundleLines = 1]" use="ItemId" />
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<root>
<xsl:for-each select="CustInvoiceTable/McsCmBilCalcInvoiceLine[McsCmBilProductItem/CgiBundleLines = 1][count(. | key('Lines-by-ItemId', ItemId)[1]) = 1]">
<xsl:sort select="ItemId" />
<GroupInvoiceLine>
<ItemId><xsl:value-of select="ItemId" /></ItemId>
<SumAmount><xsl:value-of select="sum(key('Lines-by-ItemId', ItemId)/Amount)"/></SumAmount>
</GroupInvoiceLine>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>