Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Developer's Guide

Sorting in Document Order by Default

In Internet Explorer, the default way to display a straightforward table is to display content in the order in which it appears in the source document. This default sort order is called the document order.

For a short document, it is not difficult to locate a particular item, even if the document is not rigidly ordered. But for longer documents, it is important for items to be in some logical sequence for easy reference.

To introduce basic sorting, the following is a complete example that does not sort rows.

Example

This XSLT style sheet processes the prodsort.xml file. The output is a simple table with rows for each product in the file, and columns for data such as the product category and product number.

XML File (prodsort.xml)

Use the prodsort.xml in Sample XML Data File for XSLT Sorting, and change the href attribute to reference prodsort0.xsl.

XSLT File (prodsort0.xsl)

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
   <HTML>
      <HEAD>
         <TITLE>Wooden Rings and More!</TITLE>
         <STYLE type="text/css">
            th {background-color: silver;
               font-family: Tahoma,Verdana,Arial,sans-serif}
            td {background-color: white;
               font-family: Tahoma,Verdana,Arial,sans-serif}
         </STYLE>
      </HEAD>
      <BODY>
         <xsl:apply-templates/>
      </BODY>
   </HTML>
</xsl:template>

<xsl:template match="products">
   <TABLE width="75%">
      <tr>
         <th>Category</th>
         <th>Prod ID</th>
         <th>Name/Version</th>
         <th>Description</th>
         <th>Price/Units</th>
      </tr>
      <xsl:apply-templates/>
   </TABLE>
</xsl:template>

<xsl:template match="product">
   <tr>
      <td valign="top"><xsl:value-of select="categ"/></td>
      <td valign="top"><xsl:value-of select="@prodID"/></td>
      <td valign="top"><xsl:value-of select="concat(name, '/', version)"/></td>
      <td valign="top"><xsl:value-of select="descr"/></td>
      <td valign="top" align="center"><xsl:value-of select="concat(price, ' (', price/@curr, ')')"/></td>
   </tr>
</xsl:template>

</xsl:stylesheet>

Formatted Output

Processor Output

A portion of the output is shown here.

<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-16">
<TITLE>Wooden Rings and More!</TITLE>
<STYLE type="text/css">
            th {background-color: silver;
               font-family: Tahoma,Verdana,Arial,sans-serif}
            td {background-color: white;
               font-family: Tahoma,Verdana,Arial,sans-serif}
         </STYLE>
</HEAD>
<BODY>
<table width="75%">
<tr>
<th>Category</th>
<th>Prod ID</th>
<th>Name/Version</th>
<th>Description</th>
<th>Price/Units</th>
</tr>
<tr>
<td valign="top">Software</td>
<td valign="top">AX5608</td>
<td valign="top">FooBar/1.5</td>
<td valign="top">Processes foo objects using standard FB API</td>
<td valign="top" align="center">149.99 (USD)</td>
</tr>
...
</TABLE>
</BODY>
</HTML>