2

我想使用下面的一些 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>

我需要总结每个托盘编号和产品的数量(请参见下面的第一个节点)。根据 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>
4

1 回答 1

3

不是 100% 确定新的逻辑PalletNumber是正确的(这里是凌晨 3:00),但是下面的 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>
于 2012-10-26T09:09:30.717 回答