Wednesday, November 30, 2022
Code Search
Code Bank
.NET Applications
XSD Schema Generator
.NET/XML Training


Sorting Dates using XSLT Extension Objects

Version 1.0 of XSLT doesn’t support sorting dates using the xsl:sort tag. This can present a problem since dates frequently play an important role in various applications. For example, if you have the following XML document, XSLT does not provide a way to sort the Customer nodes based upon the data contained within the CustomerSince nodes:

<Customers> <Customer> <CustomerID>ANATR</CustomerID> ... <CustomerSince>2002-1-4</CustomerSince> </Customer> <Customer> <CustomerID>ANTON</CustomerID> ... <CustomerSince>2002-1-3</CustomerSince> </Customer> <Customer> <CustomerID>AROUT</CustomerID> ... <CustomerSince>2002-1-2</CustomerSince> </Customer> ... </Customers>

This sample application demonstrates how to create a .NET XSLT extension object using the C# language. The extension object can be called from within an XSLT stylesheet and used to sort data such as the CustomerSince node shown above. The extension object also allows dates to be converted to long and short date formats. An example of calling the extension object from within an XSLT stylesheet is shown below:

<xsl:for-each select="XSLTDateTime:SortByDate(., "Customer","CustomerSince")"> <tr> <td><xsl:value-of select="ContactName" /></td> <td> <xsl:value-of select="XSLTDateTime:ToDateString(CustomerSince, $DateType)" /> </td> </tr> </xsl:for-each>

Aside from demonstrating how to create and utilize XSLT extension objects, the sample application code also shows how to implement the IComparer interface to perform custom comparisons between objects.

Teaches XSLT, XPathNavigator, XPathNodeIterator
Requirements .NET 1.1
Example: Click Here
View Source Code: Click Here
Download Code: Click Here


.NET, SharePoint and Silverlight Training Solutions
    Online, Onsite and Video Training on .NET and SharePoint technologies available through The Wahlin Group.

© 2007 Wahlin Consulting LLC
XML for ASP.NET Developers in bookstores