It's very odd to have this test inside the loop, since its value doesn't depend on anything in the loop: I wonder if this is what you really intended?
I can't see any reason for an extraneous comma (but there's way too much detail here for me to study). Are you looking at the HTML output as source, or as displayed in the browser? If you generate badly structured HTML tables, browsers can sometimes display text in very peculiar places.
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference