Get the Oldest or Newest Date Value of a List Node
Written by
Gregory Scot Collins
Thursday, 24 August 2006, 9:40 AM
This article has been tested to work with the following products and versions. No guarantee of compatibility, with or without modification, is offered for products or versions other than those listed.
IN THIS ARTICLE:
No useful functions are available in XSLT 1.0 to get the oldest or newest date value of a list node. Luckily this functionality is easily represented using XSLT constructs, allowing us to achieve our desired results. Let's examine how to define variables to get the oldest or newest value of a list of dates. If you need to get the min or max value of a list of numbers, please refer to Get the Min or Max Value of a List Node.
Create the Pages.xml file
We need a simple XML file to transform that contains a list of items from which we will get our oldest or newest date value. Copy the contents of Listing 1 into a text editor, and then save the file as Pages.xml.
Create the Oldest-Newest.xsl file
Now we need a transform that will define variables which get our oldest or newest date value. We use XSLT constructs for date values instead of XPath expressions, as you can use for number values, because the non-digit characters prevent the greater-than and less-than comparison from working properly.
Copy the contents of Listing 2 into a text editor, and then save the file as Oldest-Newest.xsl. Be sure to save it in the same folder as the Pages.xml file.
Understanding the transform
For the OLDEST_POST and NEWEST_POST variables we sort—ascending for the oldest date and descending for the newest date—using a for-each statement. From the sorted list we retrieve the first value by testing for position() = 1, and ingore remaining values. These XSLT constructs will also work for number values; which style you choose is personal preference.
Try It
Applying the transform to our XML results in the following output that correctly gets the oldest and newest date values of the posted attributes in our list:
The oldest page was posted on 2006-01-15.
The newest page was posted on 2006-11-07.
Copyright ©2006
Braintrove. All rights reserved. This material may not be copied, published, broadcast, rewritten
or redistributed. You may, however, use the techniques, along with any associated code and files in
your development. This material is provided "AS IS" without warranty of any kind. You accept full
responsibility and liability for any and all results associated with use of this material.
Stumble It!
Digg It!
del.icio.us




