【问题标题】:remove duplicate nodes from xml file using xsl使用 xsl 从 xml 文件中删除重复节点
【发布时间】:2013-07-25 15:16:09
【问题描述】:

我正在寻找一种解决方案来从 XML 文件中删除所有重复项,而不是基于确切的节点名称,相反,我正在寻找一种可以识别所有重复节点并删除它们的解决方案。只有第一个节点应该存在,其余的重复节点被删除。

我读过几篇类似的帖子:

XSL - remove the duplicate node but keep the original

Removing duplicate elements with XSLT

例子:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<projects>
        <project id="staticproperties">
            <property name="prop1">removing this prop if its duplicate</property>       
            <property name="prop2">removing this prop if its duplicate</property>               
            <property name="prop3">removing this prop if its duplicate</property>       
            <property name="prop4">removing this prop if its duplicate</property>   
            </project>
        <project id="febrelease2013">
            <property name="prop">testing prop from pom.xml</property>
            <property name="prop1">removing this prop if its duplicate</property>   
            <property name="prop3">removing this prop if its duplicate</property>       
            <property name="prop1">removing this prop if its duplicate</property>               
            <property name="prop5">removing this prop if its duplicate</property>   
        </project>
</projects>

注意:&lt;property name="**could be any thing**"&gt;

期望的输出是:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<projects>
        <project id="staticproperties">
            <property name="prop1">removing this prop if its duplicate</property>       
            <property name="prop2">removing this prop if its duplicate</property>               
            <property name="prop3">removing this prop if its duplicate</property>       
            <property name="prop4">removing this prop if its duplicate</property>   
            </project>
        <project id="febrelease2013">
            <property name="prop">testing prop from pom.xml</property>      
            <property name="prop5">removing this prop if its duplicate</property>   
        </project>
</projects>

【问题讨论】:

    标签: xml xslt xslt-1.0 xmlnode


    【解决方案1】:

    通过 XSLT 1.0 实现此目的的一种方法是利用 Muenchian Grouping 方法仅输出唯一的 &lt;property&gt; 元素(基于它们的 @name 属性)。

    例如,当这个 XSLT:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:output omit-xml-declaration="no" indent="yes"/>
      <xsl:strip-space elements="*"/>
    
      <xsl:key name="kPropertyByName" match="property" use="@name"/>
    
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template
        match="property[
                 not(
                   generate-id() =
                   generate-id(key('kPropertyByName', @name)[1])
                 )
               ]"/>
    
    </xsl:stylesheet>
    

    ...对提供的 XML 应用,产生想要的结果:

    <?xml version="1.0" encoding="UTF-8"?>
    <projects>
      <project id="staticproperties">
        <property name="prop1">removing this prop if its duplicate</property>
        <property name="prop2">removing this prop if its duplicate</property>
        <property name="prop3">removing this prop if its duplicate</property>
        <property name="prop4">removing this prop if its duplicate</property>
      </project>
      <project id="febrelease2013">
        <property name="prop">testing prop from pom.xml</property>
        <property name="prop5">removing this prop if its duplicate</property>
      </project>
    </projects>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多