Create a Variable with a Unique Sorted Node-Set
Written by
Gregory Scot Collins
Monday, 8 May 2006, 7:27 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:
In complex transforms it might be inconvenient-if not impossible-to accomplish your task without having a variable defined as a sorted node-set. This can be accomplished in two steps: First, a temporary variable is created that sorts the nodes. Second, a node-set is built from the temporary variable, while filtering for unique values. Both steps are required because the first variable does not constitute a node-set and subsequently cannot be used as such, and because we cannot perform sorting within the node-set() function. With our final node-set variable, we can accomplish our design.
The sorted variable can be defined globally at the top of your .xsl file as demonstrated here, or you can define it locally within a template. Let's create some sample files to demonstrate how to create and use a variable with a unique sorted node-set.
Create the source files
We need a simple XML file to transform. Copy the contents of Listing 1 into a text editor, and then save the file as Customers.xml.
Copy the contents of Listing 2 into a text editor, and then save the file as Customers.xsl. Be sure to save it in the same folder as the Customers.xml file.
Try it
Applying the transform to our XML results in the following output:
SORTED UNIQUE CITIES:
Centerville
Fairview
Midway
Riverside
Salem
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




