sort with current-grouping-key() function : Sort « 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 » Sort 
sort with current-grouping-key() function


File: Data.xml

<?xml version="1.0" encoding="utf-8"?>
<employees>
  <employee>
    <FirstName>A</FirstName>
    <LastName>B</LastName>
    <Country>USA</Country>
  </employee>
  <employee>
    <FirstName>C</FirstName>
    <LastName>D</LastName>
    <Country>UK</Country>
  </employee>

</employees>


File: Transform.xslt

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

  <xsl:template match="/">
    <html>
      <body>
        <h3>header 3</h3>
        <xsl:for-each-group select="employees/employee"
          group-by="Country">
          <xsl:sort select="current-grouping-key()" />
          <paragraph>
            :
            <b>
              <xsl:value-of
                select="current-grouping-key()" />
            </b>
            <ul>
              <xsl:apply-templates
                select="current-group()">
                <xsl:sort select="LastName" />
              </xsl:apply-templates>
            </ul>
          </paragraph>
        </xsl:for-each-group>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="employee">
    <li>
      <xsl:value-of select="LastName" />
      ,
      <xsl:value-of select="FirstName" />
    </li>
  </xsl:template>
</xsl:stylesheet>

Output:

<html>
   <body>
      <h3>header 3</h3>
      <paragraph>
                     :
                     <b>UK</b><ul>
            <li>D
                     ,
                     C
            </li>
         </ul>
      </paragraph>
      <paragraph>
                     :
                     <b>USA</b><ul>
            <li>B
                     ,
                     A
            </li>
         </ul>
      </paragraph>
   </body>
</html>

 
Related examples in the same category
1. Sort value first then output
2. sort select="salary" data-type="number" order="descending"
3. Sort by two columns
4. Sort by attribute value
5. Sort by substring
6. sort by different level of node
7. sort element by data type
8. for each sort descending
9. sort by attribute
10. Sort by element text
11. Set sort order as ascending
12. sorts in text
13. sorts in numeric mode.
14. sorts upercase letters first
15. sorts lowercase letters first
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.