Alternate group : 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 
Alternate group


File: Data.xml
<?xml version="1.0"?>
<SCENE>
  <TITLE>title 1</TITLE>
  <STAGEDIR>Enter</STAGEDIR>
  <SPEECH>
    <SPEAKER>I</SPEAKER>
    <LINE>A</LINE>
    <LINE>A</LINE>
    <LINE>C</LINE>
    <LINE>B</LINE>
    <LINE>H</LINE>
    <LINE>A</LINE>
  </SPEECH>
</SCENE>

File: Transform.xslt

<xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     
<xsl:output method="html" indent="yes"/>     

<xsl:template match="SPEECH">
  <tr>
  <xsl:for-each-group select="*" 
           group-adjacent="if (self::SPEAKER) then 0 else 1">
    <td valign="top">
       <xsl:for-each select="current-group()">
             <xsl:apply-templates select="."/>
          <xsl:if test="position()!=last()"><br/></xsl:if>
       </xsl:for-each>
    </td>
  </xsl:for-each-group>
  </tr>
</xsl:template>

<xsl:template match="/">
<html>
  <head>
    <title><xsl:value-of select="SCENE/TITLE"/></title>
  </head>
  <body>
    <h1><xsl:value-of select="SCENE/TITLE"/></h1>
    <xsl:apply-templates select="SCENE"/>
  </body>
</html>
</xsl:template>

<xsl:template match="ACT">
  <h2><xsl:value-of select="TITLE"/></h2>
  <xsl:apply-templates select="* except TITLE"/>
</xsl:template>

  
<xsl:template match="SCENE">
<table>
  <xsl:variable name="sequence" as="element()*">
    <xsl:for-each-group select="* except TITLE" 
        group-adjacent="if (self::SPEAKER) 
                        then 'SPEAKERS' else 'LINES'">
      <xsl:element name="{current-grouping-key()}">
        <xsl:copy-of select="current-group()"/>
      </xsl:element>
    </xsl:for-each-group>
  </xsl:variable>

  <xsl:for-each-group select="$sequence" group-starting-with="SPEAKERS">
    <tr>
      <xsl:for-each select="current-group()">
        <td valign="top">
          <xsl:for-each select="*">
            <xsl:apply-templates/>  
            <xsl:if test="position() != last()"><br/></xsl:if>
          </xsl:for-each>
        </td>
      </xsl:for-each>
    </tr>
  </xsl:for-each-group>
</table>
</xsl:template>   

<xsl:template match="STAGEDIR">
<i>&#xa0;&#xa0;<xsl:value-of select="."/></i>
</xsl:template>

</xsl:stylesheet>

Output:

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>title 1</title>
   </head>
   <body>
      <h1>title 1</h1>
      <table>
         <tr>
            <td valign="top">Enter<br>
                   I
                   A
                   A
                   C
                   B
                   H
                   A
                 
            </td>
         </tr>
      </table>
   </body>
</html>

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