This kind of thing is much easier if you use XSLT 2.0, which has regular expression support, but if you are forced to use XSLT 1.0 you need to do recursion. Look at the string-handling templates such as str:tokenize at
www.exslt.org for examples (or in a good XSLT textbook)
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference