It would be useful to have some idea of what output you are trying to produce. At present it's not clear what exactly your problem is. It might also be useful to see your endless loops - it's hard to see where people have gone wrong if they don't show you their code.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference