5,446,542 members and growing! (18,484 online)
Email Password   helpLost your password?
Languages » XML » XSLT     Intermediate

Grouping XML using XSLT

By MS le Roux

Using XSLT to group XML elements based on unique ID values in the XML. The XML is transformed into an HTML table.
XMLWin2K, Windows, Dev

Posted: 17 Feb 2002
Updated: 17 Feb 2002
Views: 109,157
Bookmarked: 43 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
14 votes for this Article.
Popularity: 4.92 Rating: 4.29 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 12.5%
3
3 votes, 37.5%
4
4 votes, 50.0%
5

Introduction

Processing a list of XML elements using XSLT is fairly simple if you want to process each element. But what if you want to group the XML elements, to show a summary? Consider the following XML:

<?xml version="1.0" ?>
<Employees>
 <Employee>
  <TeamID>1</TeamID>
  <TeamName>Sales</TeamName>
  <TaskID>1</TaskID>
  <Hours>5</Hours>
  <EmployeeID>1</EmployeeID>
  <Name>Bob</Name>
  <Surname>Shibob</Surname>
 </Employee>
 <Employee>
  <TeamID>1</TeamID>
  <TeamName>Sales</TeamName>
  <TaskID>2</TaskID>
  <Hours>4</Hours>
  <EmployeeID>1</EmployeeID>
  <Name>Bob</Name>
  <Surname>Shibob</Surname>
 </Employee>
 <Employee>
  <TeamID>1</TeamID>
  <TeamName>Sales</TeamName>
  <TaskID>4</TaskID>
  <Hours>7</Hours>
  <EmployeeID>2</EmployeeID>
  <Name>Sara</Name>
  <Surname>Lee</Surname>
 </Employee>
 <Employee>
  <TeamID>2</TeamID>
  <TeamName>Finance</TeamName>
  <TaskID>5</TaskID>
  <Hours>2</Hours>
  <EmployeeID>3</EmployeeID>
  <Name>John</Name>
  <Surname>Smith</Surname>
 </Employee>
 <Employee>
  <TeamID>2</TeamID>
  <TeamName>Finance</TeamName>
  <TaskID>3</TaskID>
  <Hours>4</Hours>
  <EmployeeID>4</EmployeeID>
  <Name>Penny</Name>
  <Surname>Wise</Surname>
 </Employee>
 <Employee>
  <TeamID>2</TeamID>
  <TeamName>Finance</TeamName>
  <TaskID>5</TaskID>
  <Hours>3</Hours>
  <EmployeeID>4</EmployeeID>
  <Name>Penny</Name>
  <Surname>Wise</Surname>
 </Employee>
</Employees>

Suppose that you need to show a summary of Employee hours, grouped by Team. Something like this:

Sample Image - groupxml.jpg

The unwieldy approach

One way to do this is to loop through the list of <Employee> elements, and only show a row whenever the EmployeeID changes. While this would work, this approach is unwieldy and inefficient, because for each <Employee> being processed, you would be required to keep track of the IDs of the previous <Employee> element. This is not a pretty sight.

The efficient approach

A cleaner, more efficient way to do this is to build a list of unique keys, then use these keys to group the results. (This is called the Muenchian Method.)

First, you must define the keys required to group the <Employee> elements. You will need one for the TeamID, and one for the EmployeeID.

<xsl:key name="keyTeamID" match="Employee" use="TeamID" />
<xsl:key name="keyEmployeeID" match="Employee" use="EmployeeID" />

Select the first element of each group of elements for each unique TeamID.

<xsl:for-each select="//Employee[generate-id(.) = generate-id(key('keyTeamID', TeamID)[1])]">

Get all the <Employee> elements that belong to that Team, into a variable.

<!-- Save the ID of the Team to a variable -->
<xsl:variable name="lngTeamID"><xsl:value-of select="TeamID" /></xsl:variable>
<!-- Select all the Employees belonging to the Team -->
<xsl:variable name="lstEmployee" select="//Employee[TeamID=$lngTeamID]" />

The <Employee> elements in this list must now be grouped by EmployeeID. This is similar to grouping by TeamID, except that in this case you only need to select elements in the list contained in the variable; you do not need to select elements from the entire result set.

<xsl:for-each select="$lstEmployee[generate-id(.) = generate-id(key('keyEmployeeID', EmployeeID)[1])]">

It is now fairly simple to show the total Hours for each Employee.

<xsl:value-of select="sum($lstEmployee[EmployeeID=$lngEmployeeID]/Hours)" />

The full source

This is the entire XSLT used to render the table in the image:

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

<!-- Define keys used to group elements -->
<xsl:key name="keyTeamID" match="Employee" use="TeamID" />
<xsl:key name="keyEmployeeID" match="Employee" use="EmployeeID" />

<xsl:template match="/">
 <html>
  <head>
   <title>Employee Hours By Team</title>
   <link type="text/css" rel="stylesheet" href="groupxml.css" />
  </head>
  <body>
   <h3>Employee Hours By Team</h3>
   <table>
   
    <!-- Process each Team -->
    <xsl:for-each select="//Employee[generate-id(.) = generate-id(key('keyTeamID', TeamID)[1])]">
     <xsl:variable name="lngTeamID"><xsl:value-of select="TeamID" /></xsl:variable>
     <!-- Select all the Employees belonging to the Team -->
     <xsl:variable name="lstEmployee" select="//Employee[TeamID=$lngTeamID]" />
     <!-- Show details for Employees in Team -->
     <xsl:call-template name="ShowEmployeesInTeam">
      <xsl:with-param name="lstEmployee" select="$lstEmployee" />
     </xsl:call-template>
    </xsl:for-each>
   
    <tr>
     <td colspan="4" class="RightJustified DarkBack">Grand Total</td>
     <td colspan="1" class="RightJustified DarkBack">
      <!-- Show Grand Total of hours for all Employees -->
      <xsl:value-of select="sum(//Employee/Hours)" />
     </td>
    </tr>
   </table>
  </body>
 </html>
</xsl:template>

<xsl:template name="ShowEmployeesInTeam">
 <xsl:param name="lstEmployee" />
 
 <!-- Show the name of the Team currently being processed -->
 <tr>
  <td colspan="4" class="DarkBack">TEAM:&#160;<xsl:value-of select="$lstEmployee[1]/TeamName" /></td>
  <td colspan="1" class="DarkBack RightJustified">HOURS</td>
 </tr>
 
 <!-- Show the total hours for each Employee in the Team -->
 <xsl:for-each select="$lstEmployee[generate-id(.) = generate-id(key('keyEmployeeID', EmployeeID)[1])]">
  <xsl:variable name="lngEmployeeID" select="EmployeeID" />
  <!-- Show details of each Employee -->
  <tr>
   <td colspan="4">
    <xsl:value-of select="$lstEmployee[EmployeeID=$lngEmployeeID]/Name" />
    &#160;<xsl:value-of select="$lstEmployee[EmployeeID=$lngEmployeeID]/Surname" />
   </td>
   <td colspan="1" class="RightJustified">
    <!-- Show the total hours for the current Employee -->
    <xsl:value-of select="sum($lstEmployee[EmployeeID=$lngEmployeeID]/Hours)" />
   </td>
  </tr>
 </xsl:for-each>
 
 <tr>
  <td colspan="4" class="LightBack RightJustified">Sub-Total</td>
  <td colspan="1" class="LightBack RightJustified">
   <!-- Show the total hours for all Employees in the Team -->
   <xsl:value-of select="sum($lstEmployee/Hours)" />
  </td>
 </tr>
</xsl:template>

</xsl:stylesheet>

The CSS used to render the table in the image:

table
{  border-collapse: collapse;
   width: 30%;
   table-layout: fixed;
   border-style: solid;
}
table, td
{  border-width: 1px;
}
td
{  color: black;
   font-family: Arial;
   font-size: x-small;
   border-right-style: none;
   border-left-style: none;
   border-top-style: solid;
   border-bottom-style: solid;
}
.DarkBack
{  background-color: #0066FF;
   background-color: blue;
   color: white;
   font-weight: bold;
}
.LightBack
{  background-color: #99CCFF;
   color: black;
}
.RightJustified
{  text-align: right;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

MS le Roux


I live in the Northern Suburbs of Cape Town (South Africa).

Occupation: Web Developer
Location: South Africa South Africa

Other popular XML articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralPerformance issuememberQing Jiang4:59 30 Mar '06  
GeneralHow can I describe binary data in XML?susslei cheng21:29 28 Sep '02  
GeneralRe: How can I describe binary data in XML?memberMS le Roux21:06 29 Sep '02  
GeneralRe: How can I describe binary data in XML?memberHeath Stewart12:26 9 Jan '03  
GeneralRendering an xml document with MSIE 5.5memberBalteo23:13 5 Sep '02  
GeneralRe: Rendering an xml document with MSIE 5.5memberMS le Roux1:19 6 Sep '02  
GeneralRe: Rendering an xml document with MSIE 5.5memberBalteo5:19 6 Sep '02  
Generalgrouping and xsltmemberAbsynthE9:34 22 Aug '02  
GeneralWhy the data are gone?membersshhz1:48 8 May '02  
GeneralRe: Why the data are gone?memberMarSCoZa2:06 8 May '02  
GeneralRe: Why the data are gone?membersshhz16:50 8 May '02  
GeneralRe: Why the data are gone?memberMS le Roux21:17 8 May '02  
GeneralRe: Why the data are gone?membergdltlxb3:45 5 Jun '02  
GeneralNice going, one thing though...memberPaul Watson22:15 17 Feb '02  
GeneralRe: Nice going, one thing though...memberMarSCoZa23:00 17 Feb '02  
GeneralRe: Nice going, one thing though...memberMatt Berther8:11 20 Feb '02  
GeneralRe: Nice going, one thing though...memberPaul Watson9:36 20 Feb '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Feb 2002
Editor: Paul Watson
Copyright 2002 by MS le Roux
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project