我有包含多个产品节点的XML文件,其中包含Amount元素。如果找到重复的元素,那么我需要跳过该元素并将Amount与已存在的元素相加。
这是我想根据产品名称和身份合并Amount的XML代码。
<List>
<ProductCatList>
<ProductCatListListID identity="new"/>
<ProductCategory name="abc">
<Product name="plastic">
<ProductID identity="prod"/>
<Amount value="3"/>
</Product>
</ProductCategory>
</ProductCatList>
<ProductCatList>
<ProductCatListListID identity="new"/>
<ProductCategory name="pqrs">
<Product name="other">
<ProductID identity="test"/>
<Amount value="58"/>
</Product>
</ProductCategory>
</ProductCatList>
<ProductCatList>
<ProductCatListListID identity="new"/>
<ProductCategory name="xyz">
<Product name="plastic">
<ProductID identity="prod"/>
<Amount value="6"/>
</Product>
</ProductCategory>
</ProductCatList>
</List>
我期待输出如下。
<List>
<ProductCatList>
<ProductCatListListID identity="new"/>
<ProductCategory name="abc">
<Product name="plastic">
<ProductID identity="prod"/>
<Amount value="9"/>
</Product>
</ProductCategory>
</ProductCatList>
<ProductCatList>
<ProductCatListListID identity="new"/>
<ProductCategory name="pqrs">
<Product name="other">
<ProductID identity="test"/>
<Amount value="58"/>
</Product>
</ProductCategory>
</ProductCatList>
</List>
如果您使用的是1.0版,那么实现它的方法之一是:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:key name="productKey" match="/List/ProductCatList/ProductCategory/Product" use="ProductID/@identity" />
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
<xsl:template match="/List/ProductCatList">
<xsl:for-each select="ProductCategory/Product[generate-id() = generate-id(key('productKey', ProductID/@identity)[1])]">
<xsl:variable name="pkeyGroup" select="key('productKey', ProductID/@identity)" />
<ProductCatList>
<ProductCatListListID><xsl:attribute name="identity"><xsl:value-of select="../../ProductCatListListID/@identity"/></xsl:attribute>
<ProductCategory><xsl:attribute name="name"><xsl:value-of select="../@name"/></xsl:attribute>
<Product><xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
<ProductID><xsl:attribute name="identity"><xsl:value-of select="ProductID/@identity"/></xsl:attribute></ProductID>
<Amount><xsl:attribute name="value"><xsl:value-of select="sum($pkeyGroup/Amount/@value)"/></xsl:attribute></Amount>
</Product>
</ProductCategory>
</ProductCatListListID>
</ProductCatList>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
在XSLT 2.0中
<!-- Identical Template -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<!-- Grouping ProductCatList -->
<xsl:template match="List">
<xsl:copy>
<xsl:for-each-group select="ProductCatList" group-by="descendant::Product/@name">
<xsl:apply-templates/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<!-- Sum for Amount/@value -->
<xsl:template match="Amount/@value">
<xsl:attribute name="value" select="sum(//Amount[../@name = current()/../../@name]/@value)"/>
</xsl:template>