Xml XSLT分组和求和
我是XSLT新手,需要根据每个订单的ID计算项目的总价(数量*单价),并使用XSLT 1.0将其打印在每个项目组的末尾。 下面是我的示例XMLXml XSLT分组和求和,xml,xslt,xslt-grouping,Xml,Xslt,Xslt Grouping,我是XSLT新手,需要根据每个订单的ID计算项目的总价(数量*单价),并使用XSLT 1.0将其打印在每个项目组的末尾。 下面是我的示例XML <Orders> <Order> <Reference>234</Reference> <Item> <ID>10</ID> <Quantity>1</Quantity> <Un
<Orders>
<Order>
<Reference>234</Reference>
<Item>
<ID>10</ID>
<Quantity>1</Quantity>
<UnitPrice>2</UnitPrice>
</Item>
<Item>
<ID>10</ID>
<Quantity>2</Quantity>
<UnitPrice>3</UnitPrice>
</Item>
<Item>
<ID>10</ID>
<Quantity>2</Quantity>
<UnitPrice>2</UnitPrice>
</Item>
<Item>
<ID>20</ID>
<Quantity>2</Quantity>
<UnitPrice>4</UnitPrice>
</Item>
</Order>
<Order>
<Reference>456</Reference>
<Item>
<ID>10</ID>
<Quantity>2</Quantity>
<UnitPrice>2</UnitPrice>
</Item>
<Item>
<ID>20</ID>
<Quantity>2</Quantity>
<UnitPrice>2</UnitPrice>
</Item>
</Order>
</Orders>
234
10
1.
2.
10
2.
3.
10
2.
2.
20
2.
4.
456
10
2.
2.
20
2.
2.
下面是所需的输出XML
<SAPOrders>
<Order>
<Reference>234</Reference>
<Item>
<Quantity>1</Quantity>
<UnitPrice>2</UnitPrice>
</Item>
<Item>
<Quantity>2</Quantity>
<UnitPrice>3</UnitPrice>
</Item>
<Item>
<Quantity>2</Quantity>
<UnitPrice>2</UnitPrice>
<Total>12</Notes>
</Item>
<Item>
<Quantity>2</Quantity>
<UnitPrice>4</UnitPrice>
<Total>8</Notes>
</Item>
</Order>
<Order>
<Reference>456</Reference>
<Item>
<Quantity>2</Quantity>
<UnitPrice>2</UnitPrice>
<Total>4</Notes>
</Item>
<Item>
<Quantity>2</Quantity>
<UnitPrice>2</UnitPrice>
<Total>4</Total>
</Item>
</Order>
</SAPOrders>
234
1.
2.
2.
3.
2.
2.
12
2.
4.
8.
456
2.
2.
4.
2.
2.
4.
这里有两个问题:(1)根据订单和项目ID对项目进行分组,以及(2)计算每个此类组的小计
第一个问题相对简单,可以使用该方法解决。第二个问题更难,因为XSLT1.0不允许对计算结果求和
下面的样式表首先预计算每个项目的扩展价格,并将结果存储在一个变量中。然后对变量进行操作,对项目进行分组并对每组进行小计
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="item" match="Item" use="@key" />
<xsl:template match="/Orders">
<!-- first pass -->
<xsl:variable name="first-pass">
<xsl:for-each select="Order">
<Order>
<xsl:copy-of select="Reference"/>
<xsl:for-each select="Item">
<Item key="{concat(../Reference, '|', ID)}" extPrice="{Quantity * UnitPrice}">
<xsl:copy-of select="*"/>
</Item>
</xsl:for-each>
</Order>
</xsl:for-each>
</xsl:variable>
<!-- output -->
<SAPOrders>
<xsl:for-each select="exsl:node-set($first-pass)/Order">
<xsl:copy>
<xsl:copy-of select="Reference"/>
<!-- for each unique item in this order -->
<xsl:for-each select="Item[count(. | key('item', @key)[1]) = 1]">
<!-- list the items in this group -->
<xsl:for-each select="key('item', @key)">
<Item>
<xsl:copy-of select="Quantity | UnitPrice"/>
<!-- add the subtotal of this group -->
<xsl:if test="position()=last()">
<Total>
<xsl:value-of select="sum(key('item', @key)/@extPrice)" />
</Total>
</xsl:if>
</Item>
</xsl:for-each>
</xsl:for-each>
</xsl:copy>
</xsl:for-each>
</SAPOrders>
</xsl:template>
</xsl:stylesheet>
演示:看看哪一个是近距离的duplicate@MichaelKay您指向的是XSLT 2.0解决方案。@tojira为什么输出中没有项ID?@michael.hor257k输出xml有逻辑,不会只是复制。我不能说我理解您的答复,但如果您希望这样做。。。