<xsl:stylesheet version="1.1"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xhtml="http://www.w3.org/1999/xhtml"
>

  <xsl:output method="xml"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    doctype-public="-//W3C/DTD XHTML 1.0 Strict//EN"
    indent="yes"/>
  
  <!-- This is an identity template - it copies everything
        that doesn't match another template -->
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="processing-instruction()">
  </xsl:template>

  <xsl:template match="//xhtml:div[@id='xmldata']/*">
    <xsl:variable name="schema" select="//xsd:schema"/>
    <xsl:variable name="tabletypename"
                  select="$schema/xsd:element[@name=name(current())]/@type"/>
    <xsl:variable name="rowtypename"
                  select="$schema/xsd:complexType[@name=$tabletypename]/xsd:sequence/xsd:element[@name='row']/@type"/>
    <xsl:variable name="tablename" select="$schema/xsd:complexType[@name=$rowtypename]/xsd:sequence/xsd:element[1]/@name"/>
     
    <table xmlns="http://www.w3.org/1999/xhtml"
      class="pure-table pure-table-bordered pure-table-striped" style="text-align: left; width: 100%;">
    
      <thead style="text-align: center">
      <tr>
        <xsl:for-each select="$schema/xsd:complexType[@name=$rowtypename]/xsd:sequence/xsd:element/@name">
          <th><xsl:value-of select="."/></th>
        </xsl:for-each>
      </tr>
      </thead>

      <tbody>
      <xsl:for-each select="xhtml:row">
        <xsl:choose>
          <xsl:when test="position() != last()">
            <tr>
              <xsl:for-each select="*">
                <td><xsl:value-of select="."/></td>
              </xsl:for-each>
            </tr>
          </xsl:when>
        </xsl:choose>
      </xsl:for-each>
      </tbody>
      <tfoot>
      <xsl:for-each select="xhtml:row">
        <xsl:choose>
          <xsl:when test="position() = last()">
              <tr>
                <xsl:for-each select="*">
                  <th style="text-align: center"><xsl:value-of select="."/></th>
                </xsl:for-each>
              </tr>
          </xsl:when>
        </xsl:choose>
      </xsl:for-each>
      </tfoot>

    </table>

  </xsl:template>

</xsl:stylesheet>
