xsl-list
[Top] [All Lists]

RE: Basic string parsing/splitting question.

2004-06-10 14:20:21
From: Stoaks, Max [mailto:Stoaks_Max(_at_)gsb(_dot_)stanford(_dot_)edu] 

I have a comma delimited list of tokens in a string that I 
want to get the first three from (if there are more than 
three). Else just the first one or two.

I.e. Given this string:
"Dogs, Big Sheep, Little Mice, Ants, Pelicans"

I'd like the result to be:
"Dogs, Big Sheep, Little Mice ..." 

Given:
"Dogs, Big Sheep, Little Mice"
It should just say:
"Dogs, Big Sheep, Little Mice"

Ditto for shorter ones.

This is a FAQ - see, for example, Dave Pawson's FAQ which has a similar
example at

http://www.dpawson.co.uk/xsl/sect2/N7240.html#d8763e280

You can do this fine in xslt 1.0 without going to exslt.  Generally, you
use a recursive template.  You pass the string in to the named template
as a parameter.  You test for the presence of a comma, and break the
string into a "first" and a "last" part.  Output the first part.  If
there is a last part, pass it to the same template. Otherwise output
your terminal string, which in your case will be "...". You may need a
bit more logic to output a comma after each bit, depending on whether
you keep the comma when you split the string.

Cheers,

Tom P


<Prev in Thread] Current Thread [Next in Thread>