如果子节点重复,如何删除节点

时间:2020-04-23 15:00:15

标签: xml xslt xslt-1.0

我正在尝试使用xslt1转换xml。

基本上,如果某个孩子的子节点已经存在,我想删除一个节点 输入的格式如下:

<Quote>
  … lots of data … 
  <ItemService>
     <ID></ID>
      …
     <Product>
       <InternalId>value</InternalId>
       <DeliveryInfo>
           <lots of subitems>
       </Deliveryinfo>
       <service>SERVICETEYPE</service>
       <some more infos/>
     </Product>
     …
  </Itemservice>
  <ItemService>
     <ID></ID>
      …
     <Product>
       <InternalId>value</InternalId>
       <DeliveryInfo>
           <lots of subitems>
       </deliveryinfo>
       <service>SERVICETEYPE</service>
       <some more infos/>
    </Product>
     … 
  </ItemService>
 … some more data …
</quote>

因此,如果已经存在另一个具有相同值的<ItemService>,我想删除<ItemService>

假设我们有10个ItemServices,其中有3个Product / service值为“ assembly”,那么我只希望在输出中保留一个(不在乎哪个)。

我已经尝试了很多事情,但是它从来没有加起来……

我认为这朝着正确的方向发展,有点不起作用...

任何帮助都将不胜感激,xslt不是我的茶,这真让我烦恼...

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:key name="product" match="Product" use="concat(generate-id(parent::*), service)"/> 
        
<xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
</xsl:template>

<xsl:template match="ItemService">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates select="Product[generate-id(.) = generate-id(key('product' , concat(generate-id(parent::*), service))[1])]" />
    </xsl:copy>
</xsl:template>
    
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

如果我猜对了(!),则您想要执行以下操作:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="item-by-service" match="ItemService" use="Product/service" />

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

<xsl:template match="Quote">
    <xsl:copy>
        <xsl:apply-templates select="*[not(self::ItemService)]"/>
        <!-- keep only distinct items -->
        <xsl:apply-templates select="ItemService[generate-id(.) = generate-id(key('item-by-service', Product/service)[1])]" />
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

可以简化为:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="item-by-service" match="ItemService" use="Product/service" />

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

<!-- remove duplicate items -->
<xsl:template match="ItemService[not(generate-id(.) = generate-id(key('item-by-service', Product/service)[1]))]"/>

</xsl:stylesheet>

重要提示:

XML区分大小写:Serviceservice不同,并且</product>不会关闭<Product>

相关问题