You code doesn;t actually generate any output within the nested for-each loops. Perhaps you took it out for clarity; but in that case you could have taken a lot of other things out for clarity as well, and you are asking us to debug code which is not the code you actually ran. If you are still having difficulty getting this to work please submit a complete example that's simplified as much as you can, showing the input, the stylesheet, the expected output and the actual output.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference