Search
Web Braintrove
Site Navigation
Home
Products
Tags
Levels
Dates
Authors
5 MOST RECENT
Perform a Case-Insensitive String Comparison without Using Translate()
Force a Field Value to a Boolean Using Rules
Quickly Crop to a Shadow
Sum Repeating Time Values
Preview and Open Forms after Upgrading to Internet Explorer 8
5 MOST POPULAR
Pass Query String Parameters to an ASP.NET Xml Control
Access a Method in a Master Page with Code-Behind
Prevent Namespace Prefixes from Being Copied to the Output
Create Groups in a Drop-Down List Box
Change the Default Action of the Power Button on the Windows Vista Start Menu
5 MOST FORGOTTEN
Install Windows 7 Beta onto a Virtual Machine
Configure Lab Color Mode Defaults for Braintrove Articles
Permanently Change the Default Styles for New Word Documents
Enable Themes in Windows Server
Use Logical Operators in Place of Conditional Expressions
Books
creative html design.2
Special Edition Using HTML and XHTML
XSLT and XPath On The Edge, Unlimited Edition
HTML Pocket Reference
XSLT: Working with XML and HTML
Magazines
Soa Web Services Journal
MSDN Magazine
Web Site Management And Internet Advertising Trends
.net: The Internet Magazine
Inside Web Development
Microsoft MVP
This article has been viewed 390 times.

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.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
<xsl:output method="html" indent="no"/>
 
<xsl:template match="/Root">
    <html><body>
        <xsl:call-template name="ProcessDelimitedListItems">
            <xsl:with-param name="DELIMITED_LIST" select="DelimitedList"/>
            <xsl:with-param name="DELIMITER" select="DelimitedList/@delimiter"/>
        </xsl:call-template>
    </body></html>
</xsl:template>
 
<xsl:template name="ProcessDelimitedListItems">
    <xsl:param name="DELIMITED_LIST"/>
    <xsl:param name="DELIMITER"/>
    <xsl:param name="COUNT" select="1"/>
    <xsl:variable name="ITEM" select="substring-before($DELIMITED_LIST, $DELIMITER)"/>
    <xsl:variable name="LIST" select="substring-after($DELIMITED_LIST, $DELIMITER)"/>
 
    <div>
        Item <xsl:value-of select="$COUNT"/>:
        <xsl:choose>
            <xsl:when test="$ITEM">
                <xsl:value-of select="$ITEM"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$DELIMITED_LIST"/>
            </xsl:otherwise>
        </xsl:choose>
    </div>
 
    <xsl:if test="$LIST">
        <xsl:call-template name="ProcessDelimitedListItems">
            <xsl:with-param name="DELIMITED_LIST" select="$LIST"/>
            <xsl:with-param name="DELIMITER" select="$DELIMITER"/>
            <xsl:with-param name="COUNT" select="number($COUNT) + 1"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>
 
</xsl:stylesheet>
Listing 2. The DelimitedList.xsl 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!
Bookmark this Article
StumbleUpon  Stumble It!
Digg  Digg It!
del.icio.us  del.icio.us
List of Figures
Listing 1 - The DelimitedList.xml file.
Listing 2 - The DelimitedList.xsl file.
See Also
Create a Node-Set from a Delimited List
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.