Can't see any obvious bug, I'm afraid. Try and trim it down to the bare essentials, and repost with a minimal source document, showing the desired output and the actual output.
Note that running in the browser is the worst imaginable development environment for XSLT debugging.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference