Search
Web Braintrove
Site Navigation
Home
Products
Tags
Levels
Dates
Authors
5 MOST RECENT
Locate the Design Checker Task Pane in InfoPath 2010
Locate Conditional Formatting and Custom Validation in InfoPath 2010
Conditionally Display Different Values In a Single Expression Box
Create a Button With a Blank Label
Create Shared Rules
5 MOST POPULAR
Access a Method in a Master Page with Code-Behind
Pass Query String Parameters to an ASP.NET Xml Control
Prevent Namespace Prefixes from Being Copied to the Output
Create Groups in a Drop-Down List Box
Configure IIS 7 for Custom Error Pages
5 MOST FORGOTTEN
Install Windows 7 Beta onto a Virtual Machine
Group Dates by Year, Month and Day
Configure Lab Color Mode Defaults for Braintrove Articles
Create a Monochromatic Dream Text Effect Using Styles
Combine Multiple Sums Into a Single Function
Books
Learning XSLT
Beginning XSLT
Inside XSLT
Web Design with XML: Generating Webpages with XML, CSS, XSLT and Format
HTML and XML for Beginners
Magazines
SQL Server Magazine
Inside Visual Basic
Asp.netpro
Visual Basic & Net Journal
Soa Web Services Journal
Microsoft MVP
This article has been viewed 650 times.

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.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt">
 
<xsl:output method="html" indent="no"/>
 
<xsl:variable name="ITEM_NODESET_TEMP">
    <xsl:call-template name="CreateNodeSetFromDelimitedList">
        <xsl:with-param name="DELIMITED_LIST" select="/Root/DelimitedList"/>
        <xsl:with-param name="DELIMITER" select="/Root/DelimitedList/@delimiter"/>
    </xsl:call-template>
</xsl:variable>
<xsl:variable name="ITEM_NODESET" select="msxsl:node-set($ITEM_NODESET_TEMP)/*"/>
 
<xsl:template match="/Root">
    <html><body>
        <xsl:for-each select="$ITEM_NODESET">
            <div>
                Item <xsl:value-of select="position()"/>:
                <xsl:value-of select="."/>
            </div>
        </xsl:for-each>
    </body></html>
</xsl:template>
 
<xsl:template name="CreateNodeSetFromDelimitedList">
    <xsl:param name="DELIMITED_LIST"/>
    <xsl:param name="DELIMITER"/>
    <xsl:variable name="ITEM" select="substring-before($DELIMITED_LIST, $DELIMITER)"/>
    <xsl:variable name="LIST" select="substring-after($DELIMITED_LIST, $DELIMITER)"/>
 
    <Item>
        <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>
    </Item>
 
    <xsl:if test="$LIST">
        <xsl:call-template name="CreateNodeSetFromDelimitedList">
            <xsl:with-param name="DELIMITED_LIST" select="$LIST"/>
            <xsl:with-param name="DELIMITER" select="$DELIMITER"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>
 
</xsl:stylesheet>
Listing 2. The NodeSetFromDelimitedList.xsl 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!
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 NodeSetFromDelimitedList.xsl file.
See Also
Process Individual Items in 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.