我想使用一些xslt和xml获得更好的xml文件 下面。请提供XSLT 1.0支持。非常感谢。
<!-- entry xml -->
<Pallets>
<Pallet>
<PalletNumber>2</PalletNumber>
<Product>1</Product>
<Quantity>2</Quantity>
</Pallet>
<Pallet>
<PalletNumber>2</PalletNumber>
<Product>1</Product>
<Quantity>4</Quantity>
</Pallet>
<Pallet>
<PalletNumber>2</PalletNumber>
<Product>2</Product>
<Quantity>1</Quantity>
</Pallet>
<Pallet>
<PalletNumber>3</PalletNumber>
<Product>1</Product>
<Quantity>2</Quantity>
</Pallet>
<Pallet>
<PalletNumber>4</PalletNumber>
<Product>2</Product>
<Quantity>2</Quantity>
</Pallet></Pallets>
我需要总结每个托盘数量和产品的数量(参见第一个节点 在右边的部分需要序列号 palletnumber值基于xml中的第一次出现。结果应该 看起来像:
<?xml version="1.0" encoding="UTF-8"?>
<!-- upgraded xml -->
<Pallets><Pallet>
<PalletNumber>2_1</PalletNumber>
<Product>1</Product>
<Quantity>6</Quantity>
</Pallet>
<Pallet>
<PalletNumber>2_2</PalletNumber>
<Product>2</Product>
<Quantity>1</Quantity>
</Pallet>
<Pallet>
<PalletNumber>3_1</PalletNumber>
<Product>1</Product>
<Quantity>2</Quantity>
</Pallet>
<Pallet>
<PalletNumber>4_1</PalletNumber>
<Product>2</Product>
<Quantity>2</Quantity>
</Pallet></Pallets>
答案 0 :(得分:3)
不是100%确定新PalletNumber
的逻辑是正确的(这是凌晨3点),但是下面的XSLT 1.0返回了给定输入所需的输出......
XML输入
<Pallets>
<Pallet>
<PalletNumber>2</PalletNumber>
<Product>1</Product>
<Quantity>2</Quantity>
</Pallet>
<Pallet>
<PalletNumber>2</PalletNumber>
<Product>1</Product>
<Quantity>4</Quantity>
</Pallet>
<Pallet>
<PalletNumber>2</PalletNumber>
<Product>2</Product>
<Quantity>1</Quantity>
</Pallet>
<Pallet>
<PalletNumber>3</PalletNumber>
<Product>1</Product>
<Quantity>2</Quantity>
</Pallet>
<Pallet>
<PalletNumber>4</PalletNumber>
<Product>2</Product>
<Quantity>2</Quantity>
</Pallet>
</Pallets>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kPalletProduct" match="Pallet" use="concat(PalletNumber,'-',Product)"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Pallets">
<xsl:copy>
<xsl:for-each select="Pallet[generate-id()=generate-id(key('kPalletProduct',concat(PalletNumber,'-',Product))[1])]">
<xsl:copy>
<PalletNumber>
<xsl:value-of select="concat(PalletNumber,'_',count(preceding::Pallet[PalletNumber=current()/PalletNumber and not(Product=current()/Product)][1])+1)"/>
</PalletNumber>
<Product>
<xsl:value-of select="Product"/>
</Product>
<Quantity>
<xsl:value-of select="sum(key('kPalletProduct',concat(PalletNumber,'-',Product))/Quantity)"/>
</Quantity>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<强>输出强>
<Pallets>
<Pallet>
<PalletNumber>2_1</PalletNumber>
<Product>1</Product>
<Quantity>6</Quantity>
</Pallet>
<Pallet>
<PalletNumber>2_2</PalletNumber>
<Product>2</Product>
<Quantity>1</Quantity>
</Pallet>
<Pallet>
<PalletNumber>3_1</PalletNumber>
<Product>1</Product>
<Quantity>2</Quantity>
</Pallet>
<Pallet>
<PalletNumber>4_1</PalletNumber>
<Product>2</Product>
<Quantity>2</Quantity>
</Pallet>
</Pallets>