There's no quick fix here: you just have to write the text handling code to do it. In 1.0, that means using recursive templates. It will be a lot easier in 2.0.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference