使用xsl从xml文件中删除重复的节点

时间:2013-07-25 15:16:09

标签: xml xslt xslt-1.0 xmlnode

我正在寻找一种解决方案来删除不基于Exact节点名称的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>

注意:<property name="**could be any thing**">

所需的输出是:

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

1 个答案:

答案 0 :(得分:0)

通过XSLT 1.0实现此目的的一种方法是使用Muenchian Grouping方法仅输出唯一的<property>元素(基于其@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>