title: Outputting multiple tables in XSLT at once
body: <p>I have an XML document:</p>
  
  <pre><code>&lt;Gym&gt;
  
  &lt;Trainee&gt;
      &lt;Trainee_ID&gt;1521&lt;/Trainee_ID&gt;
      &lt;Trainee_Name&gt;Mary Andersen&lt;/Trainee_Name&gt;
      &lt;Trainee_Age&gt;23&lt;/Trainee_Age&gt;
  &lt;/Trainee&gt;
  
  &lt;Trainee&gt;
      &lt;Trainee_ID&gt;1522&lt;/Trainee_ID&gt;
      &lt;Trainee_Name&gt;Jane Sellers&lt;/Trainee_Name&gt;
      &lt;Trainee_Age&gt;56&lt;/Trainee_Age&gt;
  &lt;/Trainee&gt;
  
  &lt;Trainee&gt;
      &lt;Trainee_ID&gt;1523&lt;/Trainee_ID&gt;
      &lt;Trainee_Name&gt;Julie Aniston&lt;/Trainee_Name&gt;
      &lt;Trainee_Age&gt;32&lt;/Trainee_Age&gt;
  &lt;/Trainee&gt;
  
  &lt;Class&gt;
      &lt;Trainee_ID&gt;1521&lt;/Trainee_ID&gt;
      &lt;Course_ID&gt;A21&lt;/Course_ID&gt;
      &lt;Class_Room&gt;A1&lt;/Class_Room&gt;
  &lt;/Class&gt;
  
  &lt;Class&gt;
      &lt;Trainee_ID&gt;1522&lt;/Trainee_ID&gt;
      &lt;Course_ID&gt;A22&lt;/Course_ID&gt;
      &lt;Class_Room&gt;B2&lt;/Class_Room&gt;
  &lt;/Class&gt;
  
  &lt;Class&gt;
      &lt;Trainee_ID&gt;1523&lt;/Trainee_ID&gt;
      &lt;Course_ID&gt;B24&lt;/Course_ID&gt;
      &lt;Class_Room&gt;B3&lt;/Class_Room&gt;
  &lt;/Class&gt;
  
  &lt;Course&gt;
      &lt;Course_ID&gt;A21&lt;/Course_ID&gt;
      &lt;Course_Title&gt;Yoga&lt;/Course_Title&gt;
      &lt;Course_Hours_Per_Week&gt;2&lt;/Course_Hours_Per_Week&gt;
  &lt;/Course&gt;
  
  &lt;Course&gt;
      &lt;Course_ID&gt;A22&lt;/Course_ID&gt;
      &lt;Course_Title&gt;Pilates&lt;/Course_Title&gt;
      &lt;Course_Hours_Per_Week&gt;2&lt;/Course_Hours_Per_Week&gt;
  &lt;/Course&gt;
  
  &lt;Course&gt;
      &lt;Course_ID&gt;B24&lt;/Course_ID&gt;
      &lt;Course_Title&gt;Aerobic&lt;/Course_Title&gt;
      &lt;Course_Hours_Per_Week&gt;3&lt;/Course_Hours_Per_Week&gt;
  &lt;/Course&gt;
  
  &lt;/Gym&gt;
  </code></pre>
  
  <p>What I want to do is make 3 tables with XSL, with header the name of the children element (only once), sub-headers the name of the grandchildren elements (only once) and the rest rows and columns will be filled with the values of the grandchildren elements.</p>
  
  <p>This is as far as I got:</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  
  &lt;xsl:template match="/Gym"&gt;
      &lt;html&gt;
      &lt;body&gt;
          &lt;xsl:for-each select="*"&gt;
              &lt;table border="1"&gt;
              &lt;tr&gt;
              &lt;xsl:if test="position()='1' or position()='4' or position()='7'"&gt;
              &lt;xsl:value-of select="translate(name(.), 'abcdefghijklnmopqrstuvwxyz', 'ABCDEFGHIJKLNMOPQRSTUVWXYZ')"/&gt;     
              &lt;/xsl:if&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
              &lt;xsl:if test="position()='1' or position()='4' or position()='7'"&gt;
                  &lt;xsl:for-each select="*"&gt;
                  &lt;th&gt;&lt;xsl:value-of select="name(.)"/&gt;&lt;/th&gt;
                  &lt;/xsl:for-each&gt;
              &lt;/xsl:if&gt;
              &lt;/tr&gt;
              &lt;tr&gt;        
              &lt;xsl:for-each select="*"&gt;   
                  &lt;th&gt;            
                  &lt;xsl:value-of select="."/&gt;
                  &lt;/th&gt;
              &lt;/xsl:for-each&gt;
              &lt;/tr&gt;
              &lt;/table&gt;
              &lt;p&gt;&lt;/p&gt;
          &lt;/xsl:for-each&gt;
      &lt;/body&gt;
      &lt;/html&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>and this is the output:</p>
  
  <p><a href="https://i.stack.imgur.com/mpYU7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mpYU7.png" alt="Output"></a><br>
  <sub>(source: <a href="http://img1.UploadScreenshot.com/images/main/1/2917105455.png" rel="nofollow noreferrer">UploadScreenshot.com</a>)</sub>  </p>
  
  <p>What is happening is that every time a new table is being created for each sub-element but what I want is a smart code that will make automatically all 3 tables and then make the headers and sub-headers and after that it will input the data.
  What I want is something like this (ignore the colors and the text formatting):</p>
  
  <p><img src="https://i.stack.imgur.com/TB3wR.png" alt="Output"></p>
  
  <p>What I want my XSLT code to do is create one table for each element instance, then put the name of the element as a header, after that put as headers the sub-elements once and then fill the table with the data.</p>