Transform to remove duplicate and copy rest

fronanliaad

New Member
I want the output xml to have grouped for the element 'c', according to the attribute 'f'. Here is my input xml and the xslt. I want the group to occur only once and the other nodes should be copied as is to the output. The xslt i tried, copies the entire input xml. So if there are two or more elements with c element and same attribute value for 'f', want the first occurence of that group to the output. My wanted result is also copied.input xml\[code\]<M> <a> <b> <c f="123"> <d>Al</d> <e NO="678"> <f>Y</f> <g> <h>FTO</h> </g> </e> </c> </b> </a> <a> <b> <c f="123"> <d>Al</d> <e NO="678"> <f>Y</f> <g> <h>FTO</h> </g> </e> </c> </b> </a> <a> <b> <c f="567"> <d>Al</d> <e NO="678"> <f>Y</f> <g> <h>FTO</h> </g> </e> </c> </b> </a> <a> <b> <somethingelse></somethingelse> </b> </a></M>\[/code\]wanted output xml\[code\]<M> <a> <b> <c f="123"> <d>Al</d> <e NO="678"> <f>Y</f> <g> <h>FTO</h> </g> </e> </c> </b> </a> <a> <b> <c f="567"> <d>Al</d> <e NO="678"> <f>Y</f> <g> <h>FTO</h> </g> </e> </c> </b> </a> <a> <b> <somethingelse></somethingelse> </b> </a></M>\[/code\]xslt i tried\[code\]<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:key name="mykey" match="c" use="@f"/> <xsl:template match= "c[generate-id() = generate-id(key('mykey',@f)[1]) ] "> <xsl:text/> <xsl:copy-of select="key('mykey',@f)[1]"/> </xsl:template> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template></xsl:stylesheet>\[/code\]
 
Back
Top