Get the Min or Max Value of a List Node
Written by
Gregory Scot Collins
Wednesday, 23 August 2006, 11:04 PM
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:
One frustration developers have with XSLT 1.0 is the lack of useful functions available for data processing. Functions to get the min or max value of a list node are among those missing. Luckily these two functions are easily represented using XPath expressions, allowing us to achieve our desired results. Let's examine how to define variables to get the min or max value of a list of numbers. If you need to get the oldest or newest value of a list of dates, please refer to Get the Oldest or Newest Date 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 min or max value. Copy the contents of Listing 1 into a text editor, and then save the file as Pages.xml.
Create the Min-Max.xsl file
Now we need a transform that will define variables which get our min or max value. If you prefer, you may use the XSLT constructs required to get the oldest or newest date value instead of the XPath expessions shown in Listing 2.
Copy the contents of Listing 2 into a text editor, and then save the file as Min-Max.xsl. Be sure to save it in the same folder as the Pages.xml file.
Understanding the transform
For the MIN_VISITS and MAX_VISITS variables we get the desired value from an XPath expression that uses a filter to compare the value of each visit attribute against every preceding and following sibling. After looping through each visit attribute, the correct min or max value is returned to the variable.
Try It
Applying the transform to our XML results in the following output that correctly gets the min and max values of the visit attribute in our list:
The fewest visits to a page were 133.
The most visits to a page were 316.
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




