Abbey Workshop

XSLT: Split a Value List Into Elements

This page covers how to convert a list of space separated values into separate elements. This is covered in Michael Kay's first book which is linked below. (If you work with XSLT a lot, you need to own this book as it is the most comprehensive reference you can find on XSLT 1.0. Highly recommended.)

The need for using this technique has come up a couple of times in the last couple of months at work. I thought it best to put some sample code online so the next time someone asks about it, they can be referred here.

For this example, assume that we have some data about classes offered at a school or university this semester. Along with each class name, there is a list of student IDs for that class. Some naughty programmer has given us a list of student IDs (social security numbers) separated by spaces in a single element instead of marking up the data correctly. Our task is to convert the data inside the element into separate <id> elements. A sample of our input file is shown below.

classes.xml

Download source for: classes.xml

The example shows two different ways the data can be formatted. The technique converts both examples into elements since whitespace is normalized before each item is removed from the list.

The basic approach is to pass the contents of the <studentList> element to a template which extracts each item. This template is called recursively until all items have been removed from the list. A sample style sheet is shown below:

split-values.xsl

Download source for: split-values.xsl

The key to splitting apart the list are lines 20-31. The template first normalizes the space on line 22. This has the affect of converting any extra spaces or line feeds into a single space. The concat() function adds a space as a delimiter so that the last item in the list is followed by a space. This allows the last item in the list to be matched and returned. Line 23 takes the first item from the list and returns that item in an element on line 25. Line 24 takes the rest of the items and stores them in the variable $remaining. If $remaining is not empty on line 26, then the template is called again. The process repeats until all the items have been removed from the list.

The file is transformed using Ant, see this how to one the basics of XSLT transforms using Ant.

After processing, the output file looks like this:

classes-idList.xml

Download source for: classes-idList.xml

A much better looking set of output. You could also extend this code a bit more and use delimiters other than a space. For example:

split-values-delim.xsl

Download source for: split-values-delim.xsl

The only real change is lines 18-22. Since you are no longer using a space as a delimiter, you can't add delimiters as before since the normalize-space() function will not remove extras. So in this case, the delimiter is added to the final item in the list. A shout out to Dave D'Amico for writing that last example.

For more information, see the following books

Google
  Web abbeyworkshop.com   

Copyright © Abbey Workshop 2006

Music Only Search
The Black MP3 Album
The MP3 Album Lyrics
Wprld Music Plus
Mp3 Songs Blog
Music Search Store
First Music Search
Mega MP3 Blog
E MP3 BLog
My MP3 Diary
Fast MP3 Finder
My Blog MP3
Mp3 Songs Mart
My MP3 Songs Album
Top Music Group
Mp3 Tune Records
Song Album Guide
Music DataDite
MP3 Buying Guide
Top MP3 Downloads
MP3 Tune Festival
FAST MP3 Downloads
MP3 Mart Online
Upper MP3
Premium MP3 Site
Music Data online
Fine MP3 Audio
Cheap Music Albums
Fine mp3 downloads
World MP3 Music Records
Mp3 Music Data Direct
The Fine Mp3
MP3 Files World
Home Music Store
DatMP3
Fat MP3 Audio
MP3 ZigZag
MP3 Songs Direct
MP3 Tune World
E MP3 Log
Boom MP3 Music
Speedy MP3
Top 20 MP3 music
Deep MP3 World
Deep MP3 Download
Best-20 MP3
Top Twenty MP3
Deep MP3
Big MP3 Guide
Deep MP3 Audio
Mp3 R.O.B.
MP3-Pop Online
Greate-20-MP3
MP3 Appear
3T-MP3