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

<xsl:template match="Idfs_Parameters">
   <html>
      <head>
         <h1> Exported IDFS 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="Sensor" mode="Label"/>
        <xsl:apply-templates select="Mode_Start_Time" mode="Label"/>
        <xsl:apply-templates select="Mode_Stop_Time" mode="Label"/>
        <xsl:apply-templates select="Mode" 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="Sensor" mode="Data_Val"/>
        <xsl:apply-templates select="Mode_Start_Time" mode="Data_Val"/>
        <xsl:apply-templates select="Mode_Stop_Time" mode="Data_Val"/>
        <xsl:apply-templates select="Mode" 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="Sensor" mode="Data_Val"/>
           <xsl:apply-templates select="Mode_Start_Time" mode="Data_Val"/>
           <xsl:apply-templates select="Mode_Stop_Time" mode="Data_Val"/>
           <xsl:apply-templates select="Mode" mode="Data_Val"/>
        </tr>
</xsl:template>

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

<xsl:template name="Scan_Label">
  <xsl:param name="scan_block"/>
   <xsl:for-each select="/Idfs_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="Mode_Start_Time" mode="Label">
        <th> Mode Start Time </th>
</xsl:template>

<xsl:template match="Mode_Stop_Time" mode="Label">
        <th> Mode Stop Time </th>
</xsl:template>

<xsl:template match="Sensor" 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:apply-templates select="Calibration" mode="Label"/>
        <xsl:apply-templates select="Pitch_Angle" mode="Label"/>
        <xsl:apply-templates select="Start_Azimuthal_Angle" mode="Label"/>
        <xsl:apply-templates select="Stop_Azimuthal_Angle" mode="Label"/> 
        <xsl:apply-templates select="Data_Quality" mode="Label"/>
</xsl:template>

<xsl:template match="Mode | Data_Quality | Calibration | Pitch_Angle" mode="Label">
        <th>
        <xsl:value-of select="./Name"/>
        <br></br>
        (<xsl:value-of select="./Unit"/>)
        </th>
</xsl:template>

<xsl:template match="Start_Azimuthal_Angle | Stop_Azimuthal_Angle" mode="Label">
        <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="/Idfs_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_Quality" mode="Data_Val">
        <td><xsl:value-of select="./Values"/></td>
</xsl:template>

<xsl:template match="Calibration | Pitch_Angle" mode="Data_Val">
    <td>
    <xsl:call-template name="Display_Vector">
        <xsl:with-param name="cval" select="./Values"/>
    </xsl:call-template>
    </td>
</xsl:template>

<xsl:template match="Start_Azimuthal_Angle | Stop_Azimuthal_Angle" mode="Data_Val">
    <td>
    <xsl:call-template name="Display_Vector">
        <xsl:with-param name="cval" select="./Values"/>
    </xsl:call-template>
    </td>
</xsl:template>

<xsl:template match="Sensor" 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:apply-templates select="Calibration" mode="Data_Val"/>
        <xsl:apply-templates select="Pitch_Angle" mode="Data_Val"/>
        <xsl:apply-templates select="Start_Azimuthal_Angle" mode="Data_Val"/>
        <xsl:apply-templates select="Stop_Azimuthal_Angle" mode="Data_Val"/>
        <xsl:apply-templates select="Data_Quality" mode="Data_Val"/>
</xsl:template>

<xsl:template match="Mode" mode="Data_Val">
       <td><xsl:value-of select="./Values"/></td>
</xsl:template>

<xsl:template match="Mode_Start_Time | Mode_Stop_Time" mode="Data_Val">
        <td><xsl:value-of select="."/></td>
</xsl:template>

<xsl:template name="Count_Parameters">
  <xsl:param name="counters"/>
      <xsl:variable name="first" select="count($counters/Sensor)"/>
      <xsl:variable name="second" select="count($counters/Sensor/Data_Quality)"/>
      <xsl:variable name="third" select="count($counters/Sensor/Start_Azimuthal_Angle)"/>
      <xsl:variable name="fourth" select="count($counters/Sensor/Stop_Azimuthal_Angle)"/>
      <xsl:variable name="fifth" select="count($counters/Sensor/Pitch_Angle)"/>
      <xsl:variable name="sixth" select="count($counters/Sensor/Calibration)"/>
      <xsl:variable name="seventh" select="count($counters/Mode)"/>
      <xsl:variable name="add_mode">
         <xsl:choose>
           <xsl:when test="$seventh &gt; 0">2</xsl:when>
           <xsl:otherwise>0</xsl:otherwise>
         </xsl:choose>
      </xsl:variable>
      <xsl:variable name="num_scan" select="count($counters/Sensor/Scan_Index)"/>
      <xsl:variable name="add_scan">
         <xsl:choose>
           <xsl:when test="$num_scan &gt; 0">$first</xsl:when>
           <xsl:otherwise>0</xsl:otherwise>
         </xsl:choose>
      </xsl:variable>
      <xsl:value-of select="3 + $first + $second + $third + $fourth + $fifth + $sixth + $seventh + $add_mode + $add_scan"/>
</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>
