Using xsl:for-each-group element, sequences can be grouped by the specified criterion. The select attribute on xsl:for-each-group specifies sequences to be grouped, the second attribute on this element determines the type of grouping.
In this example nodes are grouped by the value of "v" attribute. For this kind of grouping attribute group-by is used.
Inside xsl:for-each-group select attribute of xsl:apply-template which is set to "." selects the first node of the group.
Function current-group() is used if all nodes from the group are needed. Function current-grouping-key() gives the grouping value which is currently used.
|
XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> <xsl:template match="/aaa"> <xsl:for-each-group select="*" group-by="@v"> <sss> <xsl:apply-templates select="."/> </sss> <ggg id="{current-grouping-key()}"> <xsl:apply-templates select="current-group()"/> </ggg> </xsl:for-each-group> </xsl:template> <xsl:template match="*"> <xsl:copy-of select="."/> </xsl:template> </xsl:stylesheet> |
|
|
XML
<aaa> <bbb v="a2"/> <ccc v="1"/> <ddd v="1"/> <eee v="a2"/> <fff v="1"/> <ggg v="zz3"/> </aaa> |
Output
<sss> <bbb v="a2"/> </sss> <ggg id="a2"> <bbb v="a2"/> <eee v="a2"/> </ggg> <sss> <ccc v="1"/> </sss> <ggg id="1"> <ccc v="1"/> <ddd v="1"/> <fff v="1"/> </ggg> <sss> <ggg v="zz3"/> </sss> <ggg id="zz3"> <ggg v="zz3"/> </ggg> |
| Previous chapter: | Elements and attributes |
| Next chapter: | Multiple source documents |
| Previous page: | - - - |
| Next page: | Grouping with group-adjacent |