xslt - I need to remove dupilciate element that matches exactly -
i have input xml follows. target remove duplicate elements matches 1 another.
input xml
<namespaces> <namespace prefix="dc">http://purl.org/dc/elements/1.1/</namespace> <namespace prefix="gz">http://www.tso.co.uk/assets/namespace/gazette</namespace> <namespace prefix="xsl">http://www.w3.org/1999/xsl/transform</namespace> <namespace prefix="atom">http://www.w3.org/2005/atom</namespace> <namespace prefix="xd">http://www.oxygenxml.com/ns/doc/xsl</namespace> <namespace prefix="xs">http://www.w3.org/2001/xmlschema</namespace> <namespace prefix="xhtml">http://www.w3.org/1999/xhtml</namespace> <namespace prefix="atom">http://www.w3.org/2005/atom</namespace> <namespace prefix="xd">http://www.oxygenxml.com/ns/doc/xsl</namespace> <namespace prefix="xs">http://www.w3.org/2001/xmlschema</namespace> <namespace prefix="xhtml">http://www.w3.org/1999/xhtml</namespace> <namespace prefix="atom">http://www.w3.org/2005/atom</namespace> <namespace prefix="xs">http://www.w3.org/2001/xmlschema</namespace> <namespace prefix="gz">http://www.tso.co.uk/assets/namespace/gazette</namespace> <namespace prefix="gz">http://www.tso.co.uk/assets/namespace/gazette</namespace> </namespaces>
and expected output[from above input xml snippet] should follows:
<namespaces> <namespace prefix="dc">http://purl.org/dc/elements/1.1/</namespace> <namespace prefix="gz">http://www.tso.co.uk/assets/namespace/gazette</namespace> <namespace prefix="xsl">http://www.w3.org/1999/xsl/transform</namespace> <namespace prefix="atom">http://www.w3.org/2005/atom</namespace> <namespace prefix="xd">http://www.oxygenxml.com/ns/doc/xsl</namespace> <namespace prefix="xs">http://www.w3.org/2001/xmlschema</namespace> <namespace prefix="xhtml">http://www.w3.org/1999/xhtml</namespace> <namespace prefix="gz">http://www.tso.co.uk/assets/namespace/gazette</namespace> </namespaces>
i need xslt code achieve this. idea? please
this can interpreted grouping problem - want group elements based on combination of prefix
attribute , string value, , retain first element in each group. in xslt 2.0 simple for-each-group
<xsl:template match="namespaces"> <namespaces> <xsl:for-each-group select="namespace" group-by="concat(@prefix, ':', .)"> <xsl:sequence select="." /> </xsl:for-each-group> </namespaces> </xsl:template>
in xslt 1.0 can define key , use muenchian technique
<xsl:key name="nskey" match="namespace" use="concat(@prefix, ':', .)" /> <xsl:template match="namespaces"> <namespaces> <xsl:copy-of select="namespace[ generate-id() = generate-id(key('nskey', concat(@prefix, ':', .))[1])]" /> </namespaces> </xsl:template>
(this assumes have 1 namespaces
element in document, if have more 1 technique still works need more complex key including generate-id(..)
produce groups per-parent rather per-document).
Comments
Post a Comment