I have a xml that has so many elements and most of that contain attributes.. for some of the attributes values are same so I need to group them and generate diff xml. I/p Ex
In XSLT 2.0 you should be able to do it with <xsl:for-each-group>
, current-grouping-key()
and current-group()
Example:
<xsl:for-each-group
select="TestNode/*"
group-by="@format"
>
<group format="{current-grouping-key()}">
<xsl:for-each select="current-group()">
<xsl:copy-of select="."/>
</xsl:for-each>
</group>
</xsl:for-each-group>
See: http://www.w3.org/TR/xslt20/#grouping
In XSLT 1.0 you would use Muenchian grouping.
Define a key "format", from which we can easily select all elements given a format name. Than apply Muenchian grouping to find the unique formats in the input.
Then it gets simple. The "*" template will be applied once per format, and uses the key() to fetch all entries for that format.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:key name="format" match="TestNode/*" use="@format" />
<xsl:template match="TestNode">
<body>
<xsl:apply-templates select="*[generate-id(.)=generate-id(key('format',@format)[1])]"/>
</body>
</xsl:template>
<xsl:template match="*">
<format format="{@format}">
<xsl:copy-of select="key('format', @format)" />
</format>
</xsl:template>
</xsl:stylesheet>