xslt - remove duplicate nodes from xml file using xsl -
i looking solution delete duplicates xml file not based on exact node name, instead, looking solution can identify duplicate nodes , delete them. first node should exist, , rest of duplicate nodes deleted.
i read couple of similar posts:
xsl - remove duplicate node keep original
removing duplicate elements xslt
example:
<?xml version="1.0" encoding="utf-8" standalone="no"?> <projects> <project id="staticproperties"> <property name="prop1">removing prop if duplicate</property> <property name="prop2">removing prop if duplicate</property> <property name="prop3">removing prop if duplicate</property> <property name="prop4">removing prop if duplicate</property> </project> <project id="febrelease2013"> <property name="prop">testing prop pom.xml</property> <property name="prop1">removing prop if duplicate</property> <property name="prop3">removing prop if duplicate</property> <property name="prop1">removing prop if duplicate</property> <property name="prop5">removing prop if duplicate</property> </project> </projects>
note: <property name="**could thing**">
desired output is:
<?xml version="1.0" encoding="utf-8" standalone="no"?> <projects> <project id="staticproperties"> <property name="prop1">removing prop if duplicate</property> <property name="prop2">removing prop if duplicate</property> <property name="prop3">removing prop if duplicate</property> <property name="prop4">removing prop if duplicate</property> </project> <project id="febrelease2013"> <property name="prop">testing prop pom.xml</property> <property name="prop5">removing prop if duplicate</property> </project> </projects>
one way via xslt 1.0 utilizing muenchian grouping methodology output unique <property>
elements (based on @name
attribute).
for example, when 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>
...is applied against provided xml, wanted result produced:
<?xml version="1.0" encoding="utf-8"?> <projects> <project id="staticproperties"> <property name="prop1">removing prop if duplicate</property> <property name="prop2">removing prop if duplicate</property> <property name="prop3">removing prop if duplicate</property> <property name="prop4">removing prop if duplicate</property> </project> <project id="febrelease2013"> <property name="prop">testing prop pom.xml</property> <property name="prop5">removing prop if duplicate</property> </project> </projects>
Comments
Post a Comment