Click here to Skip to main content
13,798,719 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 6 Nov 2011
Licenced CPOL

Grouping XML elements using XSLT version 1.0

, 6 Nov 2011
Rate this:
Please Sign up or sign in to vote.
A quick example of how to do grouping in XSLT version 1.0

Suppose you have an XML like:

    <Row Category="Sweet" Value="Chocolate" />
    <Row Category="Salty" Value="Potato Chips" />
    <Row Category="Salty" Value="Pop Corn" />
    <Row Category="Sour" Value="Lemon" />
    <Row Category="Sweet" Value="Biscuits" />
    <Row Category="Salty" Value="Fries" />

and you want to have these rows grouped by the 'Category' attribute like:

Pop Corn
Potato Chips



You can use Muenchian grouping just like the following:

<xsl:stylesheet version="1.0" xmlns:xsl=""

    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">

  <!-- First, define a key that uses our element or attribute we need to group by. In our case it's the Category attribute. -->
  <xsl:key name="food-by-Category" match="Row" use="@Category" />

  <xsl:template match="Rows">
    <!-- Now, we need to iterate on the Categories, This can be done by iterating on the first XML Row item 
         for each Category. We will do that by create a node set for the current Row item (.) and the first 
         item in the Key of the Current item  Category. Then we check the count to see is it 1 or 2.. 
         If 1, So we've the same item in the node set; We have a new Category.-->
    <xsl:for-each select="Row[count(. | key('food-by-Category', @Category)[1]) = 1]">
      <!-- Sort by the Category -->
      <xsl:sort select="@Category" />
      <xsl:value-of select="@Category" />
      <hr />
      <!-- Now loop on the items of this Category, 
                  We get them from the Key we defined -->
      <xsl:for-each select="key('food-by-Category', @Category)">
        <!-- Sort by the item Value -->
        <xsl:sort select="@Value" />
        <xsl:value-of select="@Value" />
        <br />
      <br />


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Adel Refaat
Australia Australia
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Nakul Solanki22-Sep-14 7:12
memberNakul Solanki22-Sep-14 7:12 
GeneralReason for my vote of 5 it helped me Pin
kyasamadhavi21-Feb-12 3:08
memberkyasamadhavi21-Feb-12 3:08 

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
Web05 | 2.8.181207.3 | Last Updated 6 Nov 2011
Article Copyright 2011 by Adel Refaat
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid