The xsl:apply-templates mechanism is very well suited to handling recursive structures in input documents.
If your XSLT code is not doing what you want it to do, then we can only help you correct it if you show us the code and explain the requirements more clearly.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference