Click here to Skip to main content
13,665,907 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


58 bookmarked
Posted 17 Feb 2002

Grouping XML using XSLT

, 17 Feb 2002
Rate this:
Please Sign up or sign in to vote.
Using XSLT to group XML elements based on unique ID values in the XML. The XML is transformed into an HTML table.
<!-- Download Links --> <!-- Add the rest of your HTML here -->


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" ?>

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

<!-- Article image -->

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 <code>name="keyTeamID"</code> match="Employee" <code>use="TeamID"</code> />
<xsl:key <code>name="keyEmployeeID"</code> match="Employee" <code>use="EmployeeID"</code> />

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

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

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="<code>//Employee[TeamID=$lngTeamID]</code>" />

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="<code>$lstEmployee</code>[generate-id(.) = generate-id(key(<code>'keyEmployeeID', EmployeeID</code>)[1])]">

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

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

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" 


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

<xsl:template match="/">
   <title>Employee Hours By Team</title>
   <link type="text/css" rel="stylesheet" href="groupxml.css" />
   <h3>Employee Hours By Team</h3>
    <!-- Process each Team -->
    <code><xsl:for-each select="//Employee[generate-id(.) = generate-id(key('keyTeamID', TeamID)[1])]"></code>
     <xsl:variable name="lngTeamID"><xsl:value-of select="TeamID" /></xsl:variable>
     <!-- Select all the Employees belonging to the Team -->
     <code><xsl:variable name="lstEmployee" select="//Employee[TeamID=$lngTeamID]" /></code>
     <!-- Show details for Employees in Team -->
     <xsl:call-template name="ShowEmployeesInTeam">
      <code><xsl:with-param name="lstEmployee" select="$lstEmployee" /></code>
     <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)" />

<xsl:template name="ShowEmployeesInTeam">
 <code><xsl:param name="lstEmployee" /></code>
 <!-- Show the name of the Team currently being processed -->
  <td colspan="4" class="DarkBack">TEAM: <xsl:value-of select="<code>$lstEmployee[1]/TeamName</code>" /></td>
  <td colspan="1" class="DarkBack RightJustified">HOURS</td>
 <!-- Show the total hours for each Employee in the Team -->
 <code><xsl:for-each select="$lstEmployee[generate-id(.) = generate-id(key('keyEmployeeID', EmployeeID)[1])]"></code>
  <xsl:variable name="lngEmployeeID" select="EmployeeID" />
  <!-- Show details of each Employee -->
   <td colspan="4">
    <xsl:value-of select="$lstEmployee[EmployeeID=$lngEmployeeID]/Name" />
     <xsl:value-of select="$lstEmployee[EmployeeID=$lngEmployeeID]/Surname" />
   <td colspan="1" class="RightJustified">
    <!-- Show the total hours for the current Employee -->
    <code><xsl:value-of select="sum($lstEmployee[EmployeeID=$lngEmployeeID]/Hours)" /></code>
  <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)" />


The CSS used to render the table in the image:

{  border-collapse: collapse;
   width: 30%;
   table-layout: fixed;
   border-style: solid;
table, td
{  border-width: 1px;
{  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;
{  background-color: #0066FF;
   background-color: blue;
   color: white;
   font-weight: bold;
{  background-color: #99CCFF;
   color: black;
{  text-align: right;


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
Web Developer
South Africa South Africa
I live in the Northern Suburbs of Cape Town (South Africa).

You may also be interested in...


Comments and Discussions

QuestionXSLT grouping Pin
avdhoute6-Jul-14 18:54
memberavdhoute6-Jul-14 18:54 
QuestionXSLT grouping Pin
avdhoute30-Jun-14 1:15
memberavdhoute30-Jun-14 1:15 
GeneralMy vote of 4 Pin
VarunKumarGB14-Dec-10 22:23
memberVarunKumarGB14-Dec-10 22:23 
GeneralMy vote of 3 Pin
lijo011@yahoo.com15-Aug-10 23:57
memberlijo011@yahoo.com15-Aug-10 23:57 
GeneralGrouping XML data Pin
blindcapt31-Mar-09 18:15
memberblindcapt31-Mar-09 18:15 
GeneralPerformance issue Pin
Qing Jiang30-Mar-06 3:59
memberQing Jiang30-Mar-06 3:59 
QuestionHow can I describe binary data in XML? Pin
lei cheng28-Sep-02 20:29
susslei cheng28-Sep-02 20:29 
AnswerRe: How can I describe binary data in XML? Pin
MS le Roux29-Sep-02 20:06
memberMS le Roux29-Sep-02 20:06 
AnswerRe: How can I describe binary data in XML? Pin
Heath Stewart9-Jan-03 11:26
memberHeath Stewart9-Jan-03 11:26 
GeneralRendering an xml document with MSIE 5.5 Pin
Balteo5-Sep-02 22:13
memberBalteo5-Sep-02 22:13 
GeneralRe: Rendering an xml document with MSIE 5.5 Pin
MS le Roux6-Sep-02 0:19
memberMS le Roux6-Sep-02 0:19 
GeneralRe: Rendering an xml document with MSIE 5.5 Pin
Balteo6-Sep-02 4:19
memberBalteo6-Sep-02 4:19 
Generalgrouping and xslt Pin
AbsynthE22-Aug-02 8:34
memberAbsynthE22-Aug-02 8:34 
QuestionWhy the data are gone? Pin
sshhz8-May-02 0:48
membersshhz8-May-02 0:48 
AnswerRe: Why the data are gone? Pin
MarSCoZa8-May-02 1:06
memberMarSCoZa8-May-02 1:06 
GeneralRe: Why the data are gone? Pin
sshhz8-May-02 15:50
membersshhz8-May-02 15:50 
GeneralRe: Why the data are gone? Pin
MS le Roux8-May-02 20:17
memberMS le Roux8-May-02 20:17 
GeneralRe: Why the data are gone? Pin
gdltlxb5-Jun-02 2:45
membergdltlxb5-Jun-02 2:45 
GeneralNice going, one thing though... Pin
Paul Watson17-Feb-02 21:15
memberPaul Watson17-Feb-02 21:15 
GeneralRe: Nice going, one thing though... Pin
MarSCoZa17-Feb-02 22:00
memberMarSCoZa17-Feb-02 22:00 
GeneralRe: Nice going, one thing though... Pin
Matt Berther20-Feb-02 7:11
memberMatt Berther20-Feb-02 7:11 
GeneralRe: Nice going, one thing though... Pin
Paul Watson20-Feb-02 8:36
memberPaul Watson20-Feb-02 8:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06-2016 | 2.8.180810.1 | Last Updated 18 Feb 2002
Article Copyright 2002 by MS le Roux
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid