Previous | Next | Indexes
Zvon > Tutorials > XSLT 2.0 Tutorial > Groups (2/10) >

Grouping with group-adjacent

The group-adjacent attribute of xsl:for-each-group element enables "grouping of neighbors". The nodes are collected in the same group if they share a value specified by the group-adjacent attribute and in the population selected by select of xsl:for-each-group are neighbors, it means that there is no item with different value between them.
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-adjacent="@v">
                        <xsl:apply-templates  select="."/>
                        <group-members  id="{current-grouping-key()}">
                              <xsl:apply-templates  select="current-group()"/>
                        </group-members>
                  </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="a2"/>
            <ggg  v="a2"/>
            <hhh  v="1"/>
            <iii  v="zz3"/>
      </aaa>
Output

      <bbb  v="a2"/>
      <group-members  id="a2">
            <bbb  v="a2"/>
      </group-members>
      <ccc  v="1"/>
      <group-members  id="1">
            <ccc  v="1"/>
            <ddd  v="1"/>
      </group-members>
      <eee  v="a2"/>
      <group-members  id="a2">
            <eee  v="a2"/>
            <fff  v="a2"/>
            <ggg  v="a2"/>
      </group-members>
      <hhh  v="1"/>
      <group-members  id="1">
            <hhh  v="1"/>
      </group-members>
      <iii  v="zz3"/>
      <group-members  id="zz3">
            <iii  v="zz3"/>
      </group-members>


Previous chapter: Elements and attributes
Next chapter: Multiple source documents
Previous page: Grouping with group-by
Next page: Grouping with group-starting-with