Search
Web Braintrove
Site Navigation
Home
Products
Tags
Levels
Dates
Authors
5 MOST RECENT
Locate the Design Checker Task Pane in InfoPath 2010
Locate Conditional Formatting and Custom Validation in InfoPath 2010
Conditionally Display Different Values In a Single Expression Box
Create a Button With a Blank Label
Create Shared Rules
5 MOST POPULAR
Access a Method in a Master Page with Code-Behind
Pass Query String Parameters to an ASP.NET Xml Control
Prevent Namespace Prefixes from Being Copied to the Output
Create Groups in a Drop-Down List Box
Configure IIS 7 for Custom Error Pages
5 MOST FORGOTTEN
Install Windows 7 Beta onto a Virtual Machine
Group Dates by Year, Month and Day
Configure Lab Color Mode Defaults for Braintrove Articles
Create a Monochromatic Dream Text Effect Using Styles
Combine Multiple Sums Into a Single Function
Books
Sams Teach Yourself XSLT in 21 Days
XSLT Cookbook
HTML and XHTML Step by Step
HTML, XHTML, and CSS Bible
Mastering XSLT
Magazines
Inside Web Design
Visual Studio Magazine
Visual Basic & Net Journal
MSDN Magazine
Inside Web Development
Microsoft MVP
This article has been viewed 515 times.

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.
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.
Bookmark this Article
StumbleUpon  Stumble It!
Digg  Digg It!
del.icio.us  del.icio.us
List of Figures
Listing 1 - The Pages.xml file.
Listing 2 - The Oldest-Newest.xsl file.
See Also
Get the Min or Max Value of a List Node
Article Tags
Great Deals
TigerDirect Exclusive Deals, Limited Time Offers, Act Now And Save!
Find all current special offers on Adobe products.
Try SugarSync Free!
Join WebHost4Life.com
TigerDirect
Computers4SURE (4SURE.com - An Office Depot Co.)
Copyright © 2006-2010 Braintrove. All rights reserved. Braintrove, braintrove.com, and the Braintrove logo are trademarks of Gregory Scot Collins in the United States and/or other countries. The names of actual companies and products mentioned herein may be the trademarks of their respective owners. Any rights not expressly granted herein are reserved.