使用xslt从下到上从xml文件中删除重复的节点

时间:2013-07-29 20:50:16

标签: xml xslt duplicate-removal

我需要从下到上从XML文件中删除重复项,因为我将向该XML文件添加许多项目(元素),并且我不希望新值被旧值覆盖。 / p>

在以下示例中,项目“staticproperties”和febrelease2013具有两个变量“prop1”和“prop2”。但这些变量的最新值来自propject febrelease2013。

是否总是可以从下到上复制节点。

在下面的网址中,代码工作正常,但它正在从上到下进行处理。

remove duplicate nodes from xml file using xsl

示例:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<projects>
        <project id="staticproperties">
            <property name="prop1">old-value</property>       
            <property name="prop2">abc</property>               
            <property name="prop3">old-value</property>       
            <property name="prop4">def</property>   
            </project>
        <project id="febrelease2013">
            <property name="prop">abcd123</property>
            <property name="prop1">new-value</property>   
            <property name="prop3">new-value</property>                   
            <property name="prop5">defg</property>   
        </project>
</projects>

预期输出为:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<projects>
        <project id="staticproperties">               
            <property name="prop2">abc</property>    
            <property name="prop4">def</property>    
            </project>
        <project id="febrelease2013">
            <property name="prop">abcd123</property>
            <property name="prop1">new-value</property>   
            <property name="prop3">new-value</property>                   
            <property name="prop5">defg</property>    
        </project>
</projects>

1 个答案:

答案 0 :(得分:1)

以下应该可以解决问题:

t:\ftemp>type projects.xml
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<projects>
        <project id="staticproperties">
            <property name="prop1">old-value</property>
            <property name="prop2">abc</property>
            <property name="prop3">old-value</property>
            <property name="prop4">def</property>
            </project>
        <project id="febrelease2013">
            <property name="prop">abcd123</property>
            <property name="prop1">new-value</property>
            <property name="prop3">new-value</property>
            <property name="prop5">defg</property>
        </project>
</projects>
t:\ftemp>xslt projects.xml projects2.xsl
<?xml version="1.0" encoding="utf-8"?><projects>
        <project id="staticproperties">

            <property name="prop2">abc</property>

            <property name="prop4">def</property>
            </project>
        <project id="febrelease2013">
            <property name="prop">abcd123</property>
            <property name="prop1">new-value</property>
            <property name="prop3">new-value</property>
            <property name="prop5">defg</property>
        </project>
</projects>
t:\ftemp>type projects2.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

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

<xsl:key name="properties" match="property" use="@name"/>

<xsl:template match="property">
 <xsl:if test="generate-id(.)=generate-id(key('properties',@name)[last()])">
   <xsl:call-template name="copy-this"/>
 </xsl:if>
</xsl:template>

</xsl:stylesheet>

t:\ftemp>

我没有将节点标识为密钥表中的第一个节点,而是将其识别为最后一个节点。