Its too difficult to look for a solution if one sees only part of the problem.
So you might post your xml input file.
For me the xslt debugger of WSAD is a great help to understand xslt/xPath. XMLSpy seems to have similar feature in their version 5, which is even better because it shows output.
It surely is an efective help for problems like yours.
from xml-spy site:
Step through an XSLT transformation process line-by-line with a 3-panel interface that displays the input XML document (with context node highlighted), the XSLT stylesheet (with current node highlighted), and the output document as it is being built in real-time.
http://www.xmlspy.com/features_xsl.html Don't know if there are any openSource solutions for that.