<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:variable name="proof" select="//Export"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>Calibration Report</title>
        <style>
          table   { border-collapse: collapse;  }
          tbody > tr:first-child td {
          border-top: .1em solid black;
          }
         tr      { text-align: left; }
          td, th  { padding-left: .5em; padding-right: .5em }
          .left   { text-align: left; }
          .right  { text-align: right; }
		  .farbig { background-color: #F0F0F0; }
		  .sensor { background-color: #D0E0F0; }
		  .trenner { border-left: 1px solid #B0B0B0; }
        </style>
      </head>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="Sources"/>
  <xsl:template match="Version"/>
  <xsl:template match="FileDate"/>
  <xsl:template match="AdditionalInfos"/>
  <xsl:template match="Signals">
    <h1>
      Calibration 
	<xsl:choose>
	  <xsl:when test="$proof='Proof'">
		Proof
	  </xsl:when>
	  <xsl:when test="$proof='Calibrated'">
		Report
	  </xsl:when>
	</xsl:choose>
    </h1>
    <h2>
      <xsl:value-of select="/Calibration/AdditionalInfos/TestStand"/>
      <br/>
      <xsl:value-of select="/Calibration/FileDate"/>
    </h2>
    <table>
      <colgroup>
        <col/>
        <col/>
        <col/>
        <col/>
        <xsl:if test="$proof='Proof' or $proof='Calibrated'">
          <col/>
          <col/>
          <col/>
          <col/>
          <col/>
          <col/>
          <col/>
          <col/>
        </xsl:if>
      </colgroup>
      <thead>
        <xsl:if test="$proof='Proof' or $proof='Calibrated'">
          <tr>
            <th/>
			<xsl:choose>
			  <xsl:when test="$proof='Proof'">
				<th class="trenner" colspan="3">Current</th>
				<th class="trenner" colspan="8">Proof</th>
			  </xsl:when>
			  <xsl:when test="$proof='Calibrated'">
				<th class="trenner" colspan="3">Old Calibration</th>
				<th class="trenner" colspan="8">New Calibration</th>
			  </xsl:when>
			</xsl:choose>
		<th/><th/><th/><th/><th/><th/><th/>
          </tr>
        </xsl:if>
        <tr class="farbig">
          <th>
            Sensor
          </th>
          <th class="trenner">
            Calibration Date
          </th>
          <th>
            Factor
          </th>
          <th>
            Offset [V]
          </th>
		  <xsl:if test="$proof='Proof' or $proof='Calibrated'">
            <th class="trenner">
              Factor
            </th>
            <th>
              Offset [V]
            </th>
            <th>
              Signal <it>f</it> [Hz]
            </th>
            <th>
              Signal Ampl.
            </th>
            <th>
              Noise <it>f</it> [Hz]
            </th>
            <th>
              SNR [dB]
            </th>
            <th>
              Harmonics [%]
            </th>
            <th>
              Change [dB]
            </th>
          </xsl:if>
        </tr>
      </thead>
      <tbody>
        <xsl:apply-templates/>
      </tbody>
    </table>
  </xsl:template>
  <xsl:template match="Input">
    <tr>
      <xsl:variable name="spec" select="CalibrationSpec"/>
      <xsl:variable name="unit" select="/Calibration/Sources/CalibrationSpec[Name=$spec]/Unit"/>
      <td class="sensor">
        <xsl:apply-templates select="Name"/>
      </td>
      <td class="trenner">
        <xsl:apply-templates select="Date"/>
      </td>
      <td class="right">
        <xsl:apply-templates select="Calibration"/>&#x2009;V/<xsl:value-of select="$unit"/>
      </td>
      <td class="right">
        <xsl:apply-templates select="DC"/>
      </td>
	  <xsl:if test="$proof='Proof' or $proof='Calibrated'">
        <td class="trenner">
          <xsl:apply-templates select="ProofCalibration"/>&#x2009;V/<xsl:value-of select="$unit"/>
        </td>
        <td class="right">
          <xsl:apply-templates select="ProofDC"/>
        </td>
        <td class="right">
          <xsl:apply-templates select="SignalFrequency"/>
        </td>
        <td class="right">
          <xsl:apply-templates select="Amplitude"/>&#x2009;<xsl:choose><xsl:when test="boolean(LogReference) = false or LogReference = 0"><xsl:value-of select="$unit"/></xsl:when><xsl:otherwise>dB</xsl:otherwise></xsl:choose>
        </td>
        <td class="right">
          <xsl:apply-templates select="NoiseFrequency"/>
        </td>
        <td class="right">
          <xsl:apply-templates select="SNR"/>
        </td>
        <td class="right">
          <xsl:apply-templates select="Distortion"/>
        </td>
        <xsl:variable name ="color">
        <xsl:choose>
          <xsl:when test="Deviation > /Calibration/Sources/CalibrationSpec[Name=$spec]/MaximumDeviation">
            red
          </xsl:when>
          <xsl:otherwise>
            green
          </xsl:otherwise>        
        </xsl:choose>                
      </xsl:variable>
        <td class="right" style="color:{$color};">
          <xsl:apply-templates select="Deviation"/>
        </td>
      </xsl:if>
    </tr>
  </xsl:template>
</xsl:stylesheet>
