<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="Scf_Parameters">
   <html>
      <head>
         <h1> Exported SCF data </h1>
      </head>
      <body>
      <xsl:variable name="num_cols">
         <xsl:call-template name="Count_Parameters">
            <xsl:with-param name="counters" select="./Data_Set[1]"/>
         </xsl:call-template>
      </xsl:variable>
      <xsl:choose>
         <xsl:when test="$num_cols &gt; 0 and $num_cols &lt; 6">
            <xsl:call-template name="Small_Table">
            </xsl:call-template>
         </xsl:when>
         <xsl:when test="$num_cols &gt; 5 and $num_cols &lt; 12">
            <xsl:call-template name="Medium_Table">
            </xsl:call-template>
         </xsl:when>
         <xsl:otherwise>
            <xsl:call-template name="Large_Table">
            </xsl:call-template>
         </xsl:otherwise>
      </xsl:choose>
      </body>
   </html>
</xsl:template>

<xsl:template match="Data_Set[1]">
      <tr>
        <th> Data Sample </th>
        <th> Start Time </th>
        <th> Stop Time </th>
        <xsl:apply-templates select="Data_Item" mode="Label"/>
      </tr>
      <tr>
        <td><xsl:value-of select="Number"/></td>
        <td><xsl:value-of select="./Start_Time"/></td>
        <td><xsl:value-of select="./Stop_Time"/></td>
        <xsl:apply-templates select="Data_Item" mode="Data_Val"/>
      </tr>
</xsl:template>

<xsl:template match="Data_Set[position() !=1]">
        <tr>
           <td><xsl:value-of select="Number"/></td>
           <td><xsl:value-of select="./Start_Time"/></td>
           <td><xsl:value-of select="./Stop_Time"/></td>
           <xsl:apply-templates select="Data_Item" mode="Data_Val"/>
        </tr>
</xsl:template>

<xsl:template match="Data_Length | Scan_Block_Index | Scan_Unit | Scan_Length | Center_Scan | Scan_Low | Scan_High">
</xsl:template>


<xsl:template name="Scan_Label">
  <xsl:param name="scan_block"/>
   <xsl:for-each select="/Scf_Parameters/Scan">
     <xsl:variable name="scan_index" select="./Scan_Block_Index"/>
     <xsl:if test="$scan_index = $scan_block">
        <th>Center Scan
        <br></br>
        (<xsl:value-of select="./Scan_Unit"/>)</th>
     </xsl:if>
   </xsl:for-each>
</xsl:template>

<xsl:template match="Data_Item" mode="Label">
        <xsl:variable name="data_length" select="./Data_Length"/>
        <xsl:if test="$data_length &gt; 1">
           <xsl:variable name="which_scan" select="./Scan_Index"/>
           <xsl:call-template name="Scan_Label">
               <xsl:with-param name="scan_block" select="$which_scan"/>
           </xsl:call-template>
        </xsl:if>
        <th>
        <xsl:value-of select="./Name"/>
        <br></br>
        (<xsl:value-of select="./Unit"/>)
        </th>
</xsl:template>

<xsl:template name="Scan_Data">
  <xsl:param name="scan_block"/>
   <xsl:for-each select="/Scf_Parameters/Scan">
     <xsl:variable name="scan_index" select="./Scan_Block_Index"/>
     <xsl:if test="$scan_index = $scan_block">
        <td>
        <xsl:call-template name="Display_Vector">
            <xsl:with-param name="cval" select="./Center_Scan"/>
        </xsl:call-template>
        </td>
     </xsl:if>
   </xsl:for-each>
</xsl:template>

<xsl:template match="Data_Item" mode="Data_Val">
    <xsl:variable name="data_length" select="./Data_Length"/>
    <xsl:if test="$data_length &gt; 1">
       <xsl:variable name="which_scan" select="./Scan_Index"/>
       <xsl:call-template name="Scan_Data">
           <xsl:with-param name="scan_block" select="$which_scan"/>
       </xsl:call-template>
    </xsl:if>
    <td>
    <xsl:call-template name="Display_Vector">
        <xsl:with-param name="cval" select="./Values"/>
    </xsl:call-template>
    </td>
</xsl:template>

<xsl:template name="Count_Parameters">
  <xsl:param name="counters"/>
  <xsl:choose>
  <xsl:when test="$counters">
      <xsl:variable name="first" select="count($counters/Data_Item)"/>
      <xsl:variable name="num_scan" select="count($counters/Data_Item/Scan_Index)"/>
      <xsl:value-of select="3 + $first + $num_scan"/>
  </xsl:when>
  <xsl:otherwise> 0 </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="Small_Table">
    <table border="1" bgcolor="yellow" width="800">
      <xsl:apply-templates/>
      </table>
</xsl:template>
 
<xsl:template name="Medium_Table">
    <table border="1" bgcolor="yellow" width="1800">
      <xsl:apply-templates/>
      </table>
</xsl:template>
 
<xsl:template name="Large_Table">
    <table border="1" bgcolor="yellow" width="2800">
      <xsl:apply-templates/>
      </table>
</xsl:template>

<xsl:template name="Display_Vector">
  <xsl:param name="cval"/>
    <xsl:variable name="line1" select="concat(normalize-space($cval), ' ')"/>
    <xsl:variable name="first" select="substring-before($line1, ' ')"/>
    <xsl:variable name="rest" select="substring-after($line1, ' ')"/>
    <xsl:choose>
    <xsl:when test="$first">
          <xsl:value-of select="$first"/>
          <br></br>
      <xsl:call-template name="Display_Vector">
        <xsl:with-param name="cval" select="$rest"/>
      </xsl:call-template>
    </xsl:when>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>
