It is possible to do what you are attempting to achieve. I wish there was a chunk of your code. However, take a look at the Zvon website
http://zvon.org/xxl/XSLTutorial/Books/Book1/index.html for possible solutions in their tutorials' sections.

Hope that helps.