for each group : Grouping « 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 » Grouping 
5. 53. 1. for each group
File: Data.xml

<?xml version="1.0"?>
<addressbook>
  <address>
    <first-name>Doris</first-name>
    <last-name>Smith</last-name>
    <city>New York</city>
    <state>WI</state>
  </address>
  <address>
    <first-name>Mary</first-name>
    <last-name>Smith</last-name>
    <city>Vancouver</city>
    <state>MA</state>
  </address>

</addressbook>

File: Transform.xslt

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

  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:text>Customers grouped by state&#xA;&#xA;</xsl:text>
    <xsl:for-each-group select="/addressbook/address" group-by="state">
      <xsl:sort select="state"/>
      <xsl:text>  State = </xsl:text>
      <xsl:value-of select="current-grouping-key()"/>
      <xsl:text>&#xA;</xsl:text>
      <xsl:for-each select="current-group()">
        <xsl:text>&#x9;</xsl:text>
        <xsl:value-of select="(first-name, last-name)" separator=" "/>
        <xsl:text>, </xsl:text>
        <xsl:value-of select="city"/>
        <xsl:text>&#xA;</xsl:text>
      </xsl:for-each>
    </xsl:for-each-group>
  </xsl:template>

</xsl:stylesheet>

Output:

Customers grouped by state

  State = MA
  Mary Smith, Vancouver
  State = WI
  Doris Smith, New York
5. 53. Grouping
5. 53. 1. for each group
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.