for-each-group select="html/body/*" group-adjacent : for each group « XSLT stylesheet « XML

XML
1. CSS Style
2. SVG
3. XML Schema
4. XQuery
5. XSLT stylesheet
Java
XML Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
XML » XSLT stylesheet » for each group 
for-each-group select="html/body/*" group-adjacent


File: Data.xml
<?xml version="1.0"?>
<html>
  <body>
    <h2>S</h2>
    <p class="item">D <code>key</code> </p>
    <p class="item">S</p>
    <p class="note">T</p>
    <p class="note">M</p>
    <p class="note">X</p>
    <p class="item">F</p>
    <h2>Steps for grouping in XSLT 2.0</h2>
    <p class="item">A</p>
    <p class="item">B</p>
    <p class="note">C</p>
    <p class="item">D</p>
  </body>
</html>


File: Transform.xslt

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="html" include-content-type="no"/>

  <xsl:template match="/">
      <xsl:for-each-group select="html/body/*"
          group-adjacent="if (self::p[@class='item']) then 1 
                          else if (self::p[@class='note']) then 2 
                          else 3">
          <xsl:choose>
            <xsl:when test="current-grouping-key() = 1">
              <ul>
                <xsl:for-each select="current-group()">
                  <li>
                    <xsl:copy-of select="@*[not(name()='class')]"/>
                    <xsl:apply-templates select="*|text()"/>
                  </li>
                </xsl:for-each>
              </ul>
            </xsl:when>
            <xsl:when test="current-grouping-key() = 2">
              <xsl:variable name="starting-point">
                <xsl:number count="p[@class='note']" level="any" format="1"/>
              </xsl:variable>
                <p>
                      <xsl:value-of 
                        select="if (count(current-group()) gt 1)
                                then 'Notes' 
                                else 'Note'"/>
                </p>
                    <ol start="{$starting-point}">
                      <xsl:for-each select="current-group()">
                        <li>
                          <xsl:copy-of select="@*[not(name()='class')]"/>
                          <xsl:apply-templates select="*|text()"/>
                        </li>
                      </xsl:for-each>
                    </ol> 
            </xsl:when>
            <xsl:otherwise>
              <xsl:for-each select="current-group()">
                <xsl:apply-templates select="."/>
              </xsl:for-each>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each-group>
  </xsl:template>

  <xsl:template match="*">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

Output:

<h2>S</h2>
<ul>
   <li>D <code>key</code> 
   </li>
   <li>S</li>
</ul>
<p>Notes</p>
<ol start="1">
   <li>T</li>
   <li>M</li>
   <li>X</li>
</ol>
<ul>
   <li>F</li>
</ul>
<h2>Steps for grouping in XSLT 2.0</h2>
<ul>
   <li>A</li>
   <li>B</li>
</ul>
<p>Note</p>
<ol start="4">
   <li>C</li>
</ol>
<ul>
   <li>D</li>
</ul>

 
Related examples in the same category
1. use for-each-group
2. for-each-group select="current-group() except ."
3. Composite
4. Alternate group
5. Group by department
6. Multi-level
7. Sort by department
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.