Looks like you're trying to code this in a way that doesn't work for XSLT, but you haven't shown any code so it's hard to see where your misunderstanding lies.
Let us know what output you want to produce and we may be able to suggest the right design approach.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference