我正试图从两个设施获得总计的物品。我使用xsl:template match=""
和xsl:for-each select=""
尝试了几种变体,但没有运气,此时可能会更加困惑。
XML结构:
<Facility>
<Id>92</Id>
<Item>
<Sku>10100200-99</Sku>
<OnHand>623</OnHand>
</Item>
<Item>
<Sku>10201400-00</Sku>
<OnHand>509</OnHand>
</Item>
<Facility>
<Id>99</Id>
<Item>
<Sku>10100100-99</Sku>
<OnHand>0</OnHand>
</Item>
<Item>
<Sku>10100200-99</Sku>
<OnHand>725</OnHand>
</Item>
想要输出:
<Item>
<Sku>10100200-99</Sku>
<OnHand>1348</OnHand>
</Item>
<Item>
<Sku>10201400-00</Sku>
<OnHand>509</OnHand>
</Item>
<Item>
<Sku>10100100-99</Sku>
<OnHand>0</OnHand>
</Item>
在输出中,我会从<Sku>
和<OnHand>
获得<Facility[1]>
<Facility[2]>
<Sku>
,但如果同时<OnHand>
,则添加{{1}总计。任何帮助表示赞赏。
答案 0 :(得分:1)
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<!-- define a set of Items identified by Sku -->
<xsl:key name="groups" match="Item" use="Sku" />
<xsl:template match="/">
<root>
<!-- select always only the first Item with the same Sku -->
<xsl:apply-templates select=".//Item[generate-id() = generate-id(key('groups', Sku)[1])]"/>
</root>
</xsl:template>
<xsl:template match="Item">
<Item>
<xsl:copy-of select="Sku"/>
<OnHand>
<!-- sum OnHand of all Items with the same Sku -->
<xsl:value-of select="sum(key('groups', Sku)/OnHand)"/>
</OnHand>
</Item>
</xsl:template>
</xsl:stylesheet>