Q:

XSLT add table header base on hierarchy level

here is part of my XML represents the hierarchy of Terms. TopTerm is the outermost parent and ChildTerm is the child, which can have as many children as possible.
<TopTerm ID="1" Entity="Term" Name="ENVIRONMENTAL MANAGEMENT">
    <ChildTerm Relationship="narrower" ID="8" Entity="Term" Name="Auditing">
        <ChildTerm Relationship="narrower" ID="36" Entity="Term" Name="Environmental audit" />
        <ChildTerm Relationship="narrower" ID="46" Entity="Term" Name="Type of audit []" />
    </ChildTerm>
    <ChildTerm Relationship="narrower" ID="11" Entity="Term" Name="Incidents">
        <ChildTerm Relationship="narrower" ID="71" Entity="Term" Name="Bruce Beresford" />
        <ChildTerm Relationship="narrower" ID="35" Entity="Term" Name="Case name" />
        <ChildTerm Relationship="narrower" ID="83" Entity="Term" Name="Jack Lemmon" />
        <ChildTerm Relationship="narrower" ID="87" Entity="Term" Name="Mary Pcikford" />
    </ChildTerm>
    <ChildTerm Relationship="narrower" ID="16" Entity="Term" Name="Monitoring" />
    <ChildTerm Relationship="narrower" ID="18" Entity="Term" Name="Policies and procedures" />
</TopTerm>

I'd like to have an XSLT 1.0 HTML output with table, and the result should look like this
<table>
   <tr>
     <th>Level 1</th>
     <th>Level 2</th>
     <th>Level 3</th>
     <th>Level 4</th>   
   </tr>
   <tr>
     <td>ENVIRONMENTAL MANAGEMENT</td>
     <td>Auditing</td>
     <td>Environmental audit</td>
   </tr>
</table>

Something like that. My problem is I don't know the depth of this hierarchy to add the appropriate <th>Level x</th> where x can be any number base on depth. And the term level should match the table heading. 

A:

My problem is I don't know the depth of this hierarchy to add the
  appropriate <th>Level x</th> where x can be any number base on
  depth.

Well, you will just have to grab the one that is the deepest and iterate over its ancestors. Try it this way:
XSLT 1.0
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="TopTerm">
    <table border="1">
        <thead>
            <xsl:apply-templates select="descendant::ChildTerm[not(*)]" mode="header">
                <xsl:sort select="count(ancestor-or-self::*)" data-type="number" order="descending"/>
            </xsl:apply-templates>
        </thead>
        <tbody>
            <xsl:apply-templates select="descendant::ChildTerm[not(*)]"/>
        </tbody>
    </table>
</xsl:template>

<xsl:template match="ChildTerm" mode="header">
    <xsl:if test="position()=1">
        <tr>
            <xsl:for-each select="ancestor-or-self::*">
                <th><xsl:value-of select="concat('Level ', position())"/></th>
            </xsl:for-each>
        </tr>
    </xsl:if>
</xsl:template>

<xsl:template match="ChildTerm">
    <tr>
        <xsl:for-each select="ancestor-or-self::*">
            <td><xsl:value-of select="@Name"/></td>
        </xsl:for-each>
    </tr>
</xsl:template>

</xsl:stylesheet>

