Well, your template for match="table" doesn't appear to do an apply-templates, and if it did, then it wouldn't match the template with match="table/thead" because the parent of your thead is a tgroup; and if you fixed that, then the
<xsl:if test="count(/table/thead/row)!='0'"> wouldn't be satisfied (it would throw a type error in 2.0 comparing an integer to a string, and if you fixed that, it would return false, because of the tgroup element); and if you fixed that, then the xsl:for-each wouldn't select anything, because your thead element does not have a child called table.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference