for-each-group select="make|model" group-ending-with="*[position() mod 3 = 0]" : for each group « XSLT stylesheet « XML Tutorial

XML Tutorial
1. Introduction
2. Namespace
3. XML Schema
4. XPath
5. XSLT stylesheet
Java
XML
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 Tutorial » XSLT stylesheet » for each group 
5. 54. 1. for-each-group select="make|model" group-ending-with="*[position() mod 3 = 0]"
File: Data.xml

<?xml version="1.0"?>
<!-- carlist.xml -->
<cars>
  <make>Alfa Romeo</make>
  <make>Bentley</make>
  <make>Chevrolet</make>
  <make>Dodge</make>
  <make>Eagle</make>
  <make>Ford</make>
  <make>GMC</make>
</cars>

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="/">
    <html>
      <head>
        <title>Car Makes and Models</title>
      </head>
      <body>
        <h1>Car Makes and Models</h1>
        <paragraph>Here are the car makes and models in 
        our input document.</p>
        <table border="1" cellpadding="5">
          <xsl:apply-templates select="cars"/>
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="cars">
    <xsl:for-each-group select="make|model" group-ending-with="*[position() mod 3 = 0]">
      <tr>
        <xsl:apply-templates select="current-group()"/>
        <xsl:if test="count(current-group()) lt 3">
          <td style="background: #CCCCCC;"
            colspan="{3 - count(current-group())}">
          </td>
        </xsl:if>
      </tr>
    </xsl:for-each-group>
  </xsl:template>

  <xsl:template match="make">
    <td>
      <xsl:apply-templates/>
    </td>
  </xsl:template>

  <xsl:template match="model">
    <td style="font-style: italic; font-weight: bold;">
      <xsl:apply-templates/>
    </td>
  </xsl:template>
  
</xsl:stylesheet>

Output:

<html>
   <head>
      <title>Car Makes and Models</title>
   </head>
   <body>
      <h1>Car Makes and Models</h1>
      <paragraph>Here are the car makes and models in 
         our input document.
      </p>
      <table border="1" cellpadding="5">
         <tr>
            <td>Alfa Romeo</td>
            <td>Bentley</td>
            <td>Chevrolet</td>
         </tr>
         <tr>
            <td>Dodge</td>
            <td>Eagle</td>
            <td>Ford</td>
         </tr>
         <tr>
            <td>GMC</td>
            <td style="background: #CCCCCC;" colspan="2"></td>
         </tr>
      </table>
   </body>
</html>
5. 54. for each group
5. 54. 1. for-each-group select="make|model" group-ending-with="*[position() mod 3 = 0]"
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.