Create a Node-Set from a Delimited List
Written by
Gregory Scot Collins
Saturday, 3 June 2006, 7:23 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.
Having an XML node containing a delimited list, you can create a node-set of each item in that list using the MSXML node-set() extension function, a named template and parameters. The following code demonstrates how to do this by creating two variables. The first variable makes recursive calls to a named template to process each item in succession by picking off the first item, and then passing the remainder of the list back in to the same named template. The second variable takes the resulting list of <Item> elements created with the named template and converts them into a useable node-set.
Create the DelimitedList.xml file
We need a simple XML file to transform that contains our delimited list and identifies the delimiter. Copy the contents of Listing 1 into a text editor, and then save the file as DelimitedList.xml.
If the delimiter will always be the same, you can hardcode it into the transform rather than identifying it in the XML, but if there is even a remote possibility that the delimiter will change from one node to another, it is best practice to specify it along with the list.
Create the NodeSetFromDelimitedList.xsl file
Copy the contents of Listing 2 into a text editor, and then save the file as NodeSetFromDelimitedList.xsl. Be sure to save it in the same folder as the DelimitedList.xml file.
The variable ITEM_NODESET_TEMP is built by calling a named template to produce a list of <Item> elements each containing a single item from the delimited list. The named template takes two parameters: DELIMITED_LIST and DELIMITER.
As we reach the last item in the list, the delimiter is no longer present in DELIMITED_LIST. As a result both the ITEM and LIST variables will be blank. This is our signal that we are on the last item, which value is contained in DELIMITED_LIST—this is why we use an xsl:choose when displaying the item value.
The variable ITEM_NODESET uses the MSXML node-set() extension function to convert ITEM_NODESET_TEMP into a usable node-set. To use the Microsoft implementation of the node-set() extension function, we declared the xmlns namespace prefix on the root <xsl:stylesheet> element of the style sheet. Although the namespace prefix can be changed, the namespace URI must be exactly "urn:schemas-microsoft-com:xslt".
Finally, looping through each item in our custom node-set contained in the ITEM_NODESET variable, we output its position and value.
Try It
Applying the transform to our XML results in the following output:
Item 1: Braintrove
Item 2: Examples
Item 3: Rock!
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




