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

Grouping in sequences with group-by

The xsl:for-each-group with group-by can be used on any sequence not only on collections of nodes.

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="1 to 10"
                              group-by=". mod 3">
                        <value>
                              <xsl:value-of  select="."/>
                        </value>
                        <group-members>
                              <xsl:for-each  select="current-group()">
                                    <value>
                                          <xsl:value-of  select="."/>
                                    </value>
                              </xsl:for-each>
                        </group-members>
                  </xsl:for-each-group>
            </xsl:template>

      </xsl:stylesheet>
XML

      <aaa/>
Output

      <value>1</value>
      <group-members>
            <value>1</value>
            <value>4</value>
            <value>7</value>
            <value>10</value>
      </group-members>
      <value>2</value>
      <group-members>
            <value>2</value>
            <value>5</value>
            <value>8</value>
      </group-members>
      <value>3</value>
      <group-members>
            <value>3</value>
            <value>6</value>
            <value>9</value>
      </group-members>


Previous chapter: Elements and attributes
Next chapter: Multiple source documents
Previous page: Grouping with group-ending-with
Next page: Grouping in sequences with group-adjacent