使用XSLT删除特定于值的重复节点

使用XSLT删除特定于值的重复节点,xslt,Xslt,在给定的xml中,当ItemtypeCode值为“S”时,它应该检查重复的“原始订单号”值,如果找到,则删除相应的“订单”节点,当ItemtypeCode值为“R”时,它可以允许重复 请为此场景推荐XSLT。 提前谢谢 XML 5000003324892 s 5000003324892 R 5000003324892 s 5000003324892 R 预期产量 <orders> <order order-no="5000003324123"> <origin

在给定的xml中,当ItemtypeCode值为“S”时,它应该检查重复的“原始订单号”值,如果找到,则删除相应的“订单”节点,当ItemtypeCode值为“R”时,它可以允许重复

请为此场景推荐XSLT。 提前谢谢

XML


5000003324892
s
5000003324892
R
5000003324892
s
5000003324892
R
预期产量

<orders> 
<order order-no="5000003324123">
<original-order-no>5000003324892</original-order-no>
<custom-attributes>
  <custom-attribute attribute-id="itemTypeCode">S</custom-attribute>
</custom-attributes>
</order>
<order order-no="5000003324456">
<original-order-no>5000003324892</original-order-no>
<custom-attributes>
  <custom-attribute attribute-id="itemTypeCode">R</custom-attribute>     
</custom-attributes>
</order>
<order order-no="5000003324910">
<original-order-no>5000003324892</original-order-no>
<custom-attributes>
  <custom-attribute attribute-id="itemTypeCode">R</custom-attribute>     
</custom-attributes>
</order>
</orders>`

5000003324892
s
5000003324892
R
5000003324892
R
`

这是一个分组问题,请参见,在XSLT2.0中,您可以使用

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:key name="dupes"
        match="order[custom-attributes/custom-attribute[@attribute-id = 'itemTypeCode'] = 'S']"
        use="original-order-no"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="order[custom-attributes/custom-attribute[@attribute-id = 'itemTypeCode'] = 'S'][not(. is key('dupes', original-order-no)[1])]"/>

</xsl:transform>

使用XSLT1.0,您需要将最后一个模板更改为

    <xsl:template match="order[custom-attributes/custom-attribute[@attribute-id = 'itemTypeCode'] = 'S'][not(generate-id() = generate-id(key('dupes', original-order-no)[1]))]"/>

    <xsl:template match="order[custom-attributes/custom-attribute[@attribute-id = 'itemTypeCode'] = 'S'][not(generate-id() = generate-id(key('dupes', original-order-no)[1]))]"/>