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

时间:2015-11-20 07:26:41

标签: xslt

在给定的xml中,当ItemtypeCode值为' S'时,它应该检查重复'原始顺序 - 否'价值,如果找到,则删除相应的订单' node,当ItemtypeCode值为' R'时,它可以允许重复。

请为此方案建议XSLT。 在此先感谢!

XML

<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="5000003324678">
<original-order-no>5000003324892</original-order-no>
<custom-attributes>
  <custom-attribute attribute-id="itemTypeCode">S</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>

预期输出

<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>`

1 个答案:

答案 0 :(得分:0)

这是一个分组问题,请参阅http://www.jenitennison.com/xslt/grouping/muenchian.xml,在XSLT 2.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>

使用XSLT 1.0,您需要将最后一个模板更改为

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