RE: [xsl] Basic string parsing/splitting question.

Subject: RE: [xsl] Basic string parsing/splitting question.
From: "Passin, Tom" <tpassin@xxxxxxxxxxxx>
Date: Thu, 10 Jun 2004 17:20:21 -0400
> From: Stoaks, Max [mailto:Stoaks_Max@xxxxxxxxxxxxxxxx] 
> 
> 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

Current Thread