0

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>

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.