0

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

我读了几篇类似的帖子:

XSL - 删除重复节点但保留原始节点

使用 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>
4

1 回答 1

0

通过 XSLT 1.0 执行此操作的一种方法是利用Muenchian 分组方法仅输出唯一<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>
于 2013-07-25T15:52:00.743 回答