Process Individual Items in a Delimited List
Written by
Gregory Scot Collins
Friday, 26 May 2006, 9:37 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 process each item in that list using a named template and parameters. The following code demonstrates how recursive calls to a named template will allow you 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.
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 DelimitedList.xsl file
Copy the contents of Listing 2 into a text editor, and then save the file as DelimitedList.xsl. Be sure to save it in the same folder as the DelimitedList.xml file.
The named template takes three parameters: DELIMITED_LIST, DELIMITER, and COUNT. The first time we call the named template we do not pass in the COUNT parameter, allowing it to resort to the default value we specified with select="1".
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.
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




