[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
How_To_XML_Sort_XML_By_Elements_XSLT

How to sort XML by elements using XSLT?

The example shows how to sort XML data by child elements <publisher> of the parent element <book>. The XSL stylesheet will display the sorted elements in an HTML table.

Create the XML document and link it to the XSL stylesheet.

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="books_sorted.xsl"?>
<books>
   <book>  
	<title ISBN="0321173481" author="Michael R. Sweet">
	OpenGL Programming Guide Fourth Edition</title>		
	<publisher>Addison-Wesley</publisher>
	<year>2004</year>
   </book>
   <book>
	<title ISBN="0849371643" author="Gerald Farin">
	Curves and Surfaces for CAGD: A Practical Guide</title>
	<publisher>Academic Press</publisher>
	<year>2002</year>
   </book>
   <book>
	<title ISBN="1558606696" author="David Rogers">
	An Introduction to NURBS: With Historical Perspective</title>
	<publisher>Academic Press</publisher>
	<year>2001</year>
   </book>
   <book>
	<title ISBN="1568810849" author="Gerald Farin">
	NURBS: From Projective Geometry to Practical Use</title>
	<publisher>A K Peters</publisher>
	<year>1999</year>
   </book>
</books>


Create the XSL stylesheet.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:template match="/">
<html>
<head><title>Books</title>
</head>
<body>
<table width="100%" border="1">
  <THEAD>
   <TR>
       <TD width="50%"><B>Publisher</B></TD>
       <TD width="50%"><B>Year</B></TD>
   </TR>
  </THEAD> 
  <TBODY>
   <xsl:for-each select="books/book">
    <xsl:sort select="publisher" />
    <TR>	  
       <TD width="50%"><xsl:value-of select="publisher" /></TD>
       <TD width="50%"><xsl:value-of select="year" /></TD>
    </TR>
   </xsl:for-each>
  </TBODY>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>


<xsl:sort> must be placed right after the <xsl:for-each> or <xsl:apply-templates> elements.

<xsl:sort select="publisher" /> sorts the selected elements' text alphabetically. The default sorting is ascending and the default data-type is text.

When sorting numbers, make sure to set the data-type attribute of <xsl:sort> to number.

<xsl:sort select="year" data-type="number" order="descending"/> will sort the dates starting from the latest times.

XML FAQ

last edited (April 19, 2004) by lillu, Number of views: 2355, Current Rev: 5 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.