[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
How_To_XML_Sort_XML_By_Attributes_XSLT
Create an XML document and link it to a XSL stylesheet.
Create a XSL stylesheet.
In the example the ISBN and author attributes are held within the <title> element which is a child element of <book>. Refer to the attributes through their parent elements as follows: <xsl:sort select="title/@ISBN" />.
<xsl:sort> must be placed after the <xsl:for-each> or <xsl:apply-templates> templates.
<xsl:sort select="title/@ISBN" /> sorts each <title> element attribute value. The default sort mode 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="ISBN" data-type="number"> will sort the numbers starting from the smallest.
XML FAQ
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
How_To_XML_Sort_XML_By_Attributes_XSLT
How to sort XML data by attributes using XSLT?
The example shows how to sort XML data by the attribute @ISBN held within a child element <title>. An XSL stylesheet will display the sorted elements in a HTML table.Create an XML document and link it to a XSL stylesheet.
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="sort.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 a 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="40%"><B>Title</B></TD>
<TD width="10%"><B>Publisher</B></TD>
</TR>
</THEAD>
<TBODY>
<xsl:for-each select="books/book">
<xsl:sort select="title/@ISBN" />
<TR>
<TD width="40%"><xsl:value-of select="title/@ISBN" /></TD>
<TD width="10%"><xsl:value-of select="title/@author" /></TD>
</TR>
</xsl:for-each>
</TBODY>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>In the example the ISBN and author attributes are held within the <title> element which is a child element of <book>. Refer to the attributes through their parent elements as follows: <xsl:sort select="title/@ISBN" />.
<xsl:sort> must be placed after the <xsl:for-each> or <xsl:apply-templates> templates.
<xsl:sort select="title/@ISBN" /> sorts each <title> element attribute value. The default sort mode 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="ISBN" data-type="number"> will sort the numbers starting from the smallest.
XML FAQ
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
