kumar
kumar

Reputation: 389

remove duplicate nodes from xml file using xslt from bottom to top

I need to remove the duplicates from an XML file from bottom to top, Because I will be adding lot of projects(elements) to this XML file and I don't want the new value to be overwritten by old value.

In the following example, project "staticproperties" and febrelease2013 having two variables "prop1" and "prop2". But the latest values for these variables are from propject febrelease2013.

Is it possible always to copy the nodes from bottom to top.

In the following url the code is working fine, but it is coping from top to bottom.

remove duplicate nodes from xml file using xsl

Example:

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

Expected output is:

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

Upvotes: 0

Views: 144

Answers (1)

G. Ken Holman
G. Ken Holman

Reputation: 4393

The following should do the trick:

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>

Instead of identifying the node to be the first from the key table, I'm identifying it to be the last.

Upvotes: 1

Related Questions