<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet version="1.0"    
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt">

<!-- (c) 2022, Trimble Inc. All rights reserved.                                               -->
<!-- Permission is hereby granted to use, copy, modify, or distribute this style sheet for any -->
<!-- purpose and without fee, provided that the above copyright notice appears in all copies   -->
<!-- and that both the copyright notice and the limited warranty and restricted rights notice  -->
<!-- below appear in all supporting documentation.                                             -->

<!-- TRIMBLE INC. PROVIDES THIS STYLE SHEET "AS IS" AND WITH ALL FAULTS.                       -->
<!-- TRIMBLE INC. SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY               -->
<!-- OR FITNESS FOR A PARTICULAR USE. TRIMBLE INC. DOES NOT WARRANT THAT THE                   -->
<!-- OPERATION OF THIS STYLE SHEET WILL BE UNINTERRUPTED OR ERROR FREE.                        -->

<xsl:output method="html" omit-xml-declaration="no" encoding="utf-8"/>

<!-- Set the numeric display details i.e. decimal point, thousands separator etc -->
<xsl:variable name="DecPt" select="string('.')"/>    <!-- Change as appropriate for US/European -->
<xsl:variable name="GroupSep" select="string(',')"/> <!-- Change as appropriate for US/European -->
<!-- Also change decimal-separator & grouping-separator in decimal-format below 
     as appropriate for US/European output -->
<xsl:decimal-format name="Standard" 
                    decimal-separator="."
                    grouping-separator=","
                    infinity="Infinity"
                    minus-sign="-"
                    NaN="?"
                    percent="%"
                    per-mille="&#2030;"
                    zero-digit="0" 
                    digit="#" 
                    pattern-separator=";" />

<xsl:variable name="DecPl0" select="string('#0')"/>
<xsl:variable name="DecPl1" select="string(concat('#0', $DecPt, '0'))"/>
<xsl:variable name="DecPl2" select="string(concat('#0', $DecPt, '00'))"/>
<xsl:variable name="DecPl3" select="string(concat('#0', $DecPt, '000'))"/>
<xsl:variable name="DecPl4" select="string(concat('#0', $DecPt, '0000'))"/>
<xsl:variable name="DecPl5" select="string(concat('#0', $DecPt, '00000'))"/>
<xsl:variable name="DecPl6" select="string(concat('#0', $DecPt, '000000'))"/>
<xsl:variable name="DecPl8" select="string(concat('#0', $DecPt, '00000000'))"/>

<xsl:variable name="fileExt" select="'htm'"/>

<xsl:variable name="useChainageTerminology">false</xsl:variable>
<xsl:variable name="useRailTerminology">false</xsl:variable>

<!-- Define keys to speed up record searchs -->
<xsl:key name="tgtID-search" match="/JOBFile/FieldBook/TargetRecord" use="@ID"/>
<xsl:key name="antID-search" match="/JOBFile/FieldBook/AntennaRecord" use="@ID"/>
<xsl:key name="travID-search" match="/JOBFile/FieldBook/TraverseDefinitionRecord" use="@ID"/>
<xsl:key name="refID-search" match="/JOBFile/FieldBook/ReferenceRecord" use="@ID"/>
<xsl:key name="equipID-search" match="/JOBFile/FieldBook/GPSEquipmentRecord" use="@ID"/>

<!-- User variable definitions - Appropriate fields are displayed on the       -->
<!-- Survey Controller screen to allow the user to enter specific values       -->
<!-- which can then be used within the style sheet definition to control the   -->
<!-- output data.                                                              -->
<!--                                                                           -->
<!-- All user variables must be identified by a variable element definition    -->
<!-- named starting with 'userField' (case sensitive) followed by one or more  -->
<!-- characters uniquely identifying the user variable definition.             -->
<!--                                                                           -->
<!-- The text within the 'select' field for the user variable description      -->
<!-- references the actual user variable and uses the '|' character to         -->
<!-- separate the definition details into separate fields as follows:          -->
<!-- For all user variables the first field must be the name of the user       -->
<!-- variable itself (this is case sensitive) and the second field is the      -->
<!-- prompt that will appear on the Survey Controller screen.                  -->
<!-- The third field defines the variable type - there are four possible       -->
<!-- variable types: Double, Integer, String and StringMenu.  These variable   -->
<!-- type references are not case sensitive.                                   -->
<!-- The fields that follow the variable type change according to the type of  -->
<!-- variable as follow:                                                       -->
<!-- Double and Integer: Fourth field = optional minimum value                 -->
<!--                     Fifth field = optional maximum value                  -->
<!--   These minimum and maximum values are used by the Survey Controller for  -->
<!--   entry validation.                                                       -->
<!-- String: No further fields are needed or used.                             -->
<!-- StringMenu: Fourth field = number of menu items                           -->
<!--             Remaining fields are the actual menu items - the number of    -->
<!--             items provided must equal the specified number of menu items. -->
<!--                                                                           -->
<!-- The style sheet must also define the variable itself, named according to  -->
<!-- the definition.  The value within the 'select' field will be displayed in -->
<!-- the Survey Controller as the default value for the item.                  -->

<!-- Need to use the select attribute for userField definitions and also include the -->
<!-- contents in single quotes so that the user information can be picked up by the  -->
<!-- Survey Controller and ASCII File Generator.  This means it is not possible to   -->
<!-- have text that includes any apostrophes (single quotes) in it.                  -->
<xsl:variable name="userField1" select="'DetailedRpt|Output detailed report|stringMenu|2|Yes|No'"/>
<xsl:variable name="DetailedRpt" select="'Yes'"/>

<xsl:variable name="userField2" select="'RptContents|Report contents|stringMenu|3|Field book data only|Reduced coordinates only|All'"/>
<xsl:variable name="RptContents" select="'All'"/>

<xsl:variable name="userField3" select="'vectorDeltasType|Report GNSS deltas as|stringMenu|2|Mark to mark|APC to APC'"/>
<xsl:variable name="vectorDeltasType" select="'APC to APC'"/>

<xsl:variable name="userField4" select="'includeSatelliteDetails|Satellite information|stringMenu|2|Yes|No'"/>
<xsl:variable name="includeSatelliteDetails" select="'No'"/>

<xsl:variable name="IncludeBorders" select="'Yes'"/>

<xsl:param name="saveAsPDF" select="'false'"/>  <!-- Parameter passed in to indicate whether the output html file is going to be converted to and saved as a pdf file -->
<xsl:param name="onWindows" select="'true'"/>   <!-- Parameter passed in to indicate whether the platform the system is running on is Windows or Android -->
<xsl:param name="dpiScale" select="1.00"/>      <!-- Parameter passed in providing the dpi scale factor in play on the platform the system is running on -->

<!-- Save the possible selected values in variables for comparison purposes -->
<!-- so that there is no need to include the translated strings in quotes.  -->
<xsl:variable name="YesStr">Yes</xsl:variable>
<xsl:variable name="NoStr">No</xsl:variable>
<xsl:variable name="FieldBookDataOnlyStr">Field book data only</xsl:variable>
<xsl:variable name="ReducedCoordinatesOnlyStr">Reduced coordinates only</xsl:variable>
<xsl:variable name="AllStr">All</xsl:variable>

<!-- **************************************************************** -->
<!-- Set global variables from the Environment section of JobXML file -->
<!-- **************************************************************** -->
<xsl:variable name="DistUnit"   select="/JOBFile/Environment/DisplaySettings/DistanceUnits" />
<xsl:variable name="ElevUnit"   select="/JOBFile/Environment/DisplaySettings/HeightUnits" />
<xsl:variable name="AngleUnit"  select="/JOBFile/Environment/DisplaySettings/AngleUnits" />
<xsl:variable name="CoordOrder" select="/JOBFile/Environment/DisplaySettings/CoordinateOrder" />
<xsl:variable name="TempUnit"   select="/JOBFile/Environment/DisplaySettings/TemperatureUnits" />
<xsl:variable name="PressUnit"  select="/JOBFile/Environment/DisplaySettings/PressureUnits" />
<xsl:variable name="AreaUnit"   select="/JOBFile/Environment/DisplaySettings/AreaUnits" />
<xsl:variable name="VolumeUnit" select="/JOBFile/Environment/DisplaySettings/VolumeUnits" />

<!-- Setup conversion factor for coordinate and distance values -->
<xsl:variable name="DistConvFactor">
  <xsl:choose>
    <xsl:when test="$DistUnit='Metres'">1.0</xsl:when>
    <xsl:when test="$DistUnit='InternationalFeet'">3.280839895</xsl:when>
    <xsl:when test="$DistUnit='USSurveyFeet'">3.2808333333357</xsl:when>
    <xsl:otherwise>1.0</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<!-- Setup conversion factor for coordinate and distance values -->
<xsl:variable name="ElevConvFactor">
  <xsl:choose>
    <xsl:when test="$ElevUnit='Metres'">1.0</xsl:when>
    <xsl:when test="$ElevUnit='InternationalFeet'">3.280839895</xsl:when>
    <xsl:when test="$ElevUnit='USSurveyFeet'">3.2808333333357</xsl:when>
    <xsl:otherwise>1.0</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<!-- Setup conversion factor for angular values -->
<xsl:variable name="AngleConvFactor">
  <xsl:choose>
    <xsl:when test="$AngleUnit='DMSDegrees'">1.0</xsl:when>
    <xsl:when test="$AngleUnit='Gons'">1.111111111111</xsl:when>
    <xsl:when test="$AngleUnit='Mils'">17.77777777777</xsl:when>
    <xsl:otherwise>1.0</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<!-- Setup conversion factor for area values -->
<xsl:variable name="AreaConvFactor">
  <xsl:choose>
    <xsl:when test="$AreaUnit = 'SquareMetres'">1.0</xsl:when>
    <xsl:when test="$AreaUnit = 'SquareMiles'">
      <xsl:value-of select="1.0 div 2589988.110336"/>
    </xsl:when>
    <xsl:when test="$AreaUnit = 'SquareFeet'">
      <xsl:value-of select="3.280839895 * 3.280839895"/>
    </xsl:when>
    <xsl:when test="$AreaUnit = 'SquareUSSurveyFeet'">
      <xsl:value-of select="3.2808333333357 * 3.2808333333357"/>
    </xsl:when>
    <xsl:when test="$AreaUnit = 'Acres'">
      <xsl:value-of select="1.0 div 4046.8564224"/>
    </xsl:when>
    <xsl:when test="$AreaUnit = 'Hectares'">0.0001</xsl:when>
    <xsl:otherwise>1.0</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<!-- Setup conversion factor for volume values -->
<xsl:variable name="VolConvFactor">
  <xsl:choose>
    <xsl:when test="$VolumeUnit = 'CubicMetres'">1.0</xsl:when>
    <xsl:when test="$VolumeUnit = 'CubicFeet'">
      <xsl:value-of select="3.280839895 * 3.280839895 * 3.280839895"/>
    </xsl:when>
    <xsl:when test="$VolumeUnit = 'CubicUSSurveyFeet'">
      <xsl:value-of select="3.2808333333357 * 3.2808333333357 * 3.2808333333357"/>
    </xsl:when>
    <xsl:when test="$VolumeUnit = 'CubicYards'">
      <xsl:value-of select="3.280839895 * 3.280839895 * 3.280839895 div 27.0"/>
    </xsl:when>
    <xsl:when test="$VolumeUnit = 'CubicUSSurveyYards'">
      <xsl:value-of select="3.2808333333357 * 3.2808333333357 * 3.2808333333357 div 27.0"/>
    </xsl:when>
    <xsl:when test="$VolumeUnit = 'AcreFeet'">
      <xsl:value-of select="1.0 div 1233.48183754752"/>
    </xsl:when>
    <xsl:when test="$VolumeUnit = 'USAcreFeet'">
      <xsl:value-of select="1.0 div 1233.4892384681"/>
    </xsl:when>
    <xsl:otherwise>1.0</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<!-- Setup boolean variable for coordinate order -->
<xsl:variable name="NECoords">
  <xsl:choose>
    <xsl:when test="$CoordOrder='North-East-Elevation'">true</xsl:when>
    <xsl:when test="$CoordOrder='X-Y-Z'">true</xsl:when>
    <xsl:otherwise>false</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<!-- Set up global variables for the north, east, elevation, delta north, delta east and -->
<!-- delta elevation prompts according to the type of coordinate labeling.               -->
<xsl:variable name="northPrompt">
  <xsl:choose>
    <xsl:when test="($CoordOrder='North-East-Elevation') or ($CoordOrder='East-North-Elevation')">North</xsl:when>
    <xsl:when test="$CoordOrder='XYZ_CAD'">Y</xsl:when>
    <xsl:otherwise>X</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="eastPrompt">
  <xsl:choose>
    <xsl:when test="($CoordOrder='North-East-Elevation') or ($CoordOrder='East-North-Elevation')">East</xsl:when>
    <xsl:when test="$CoordOrder='XYZ_CAD'">X</xsl:when>
    <xsl:otherwise>Y</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="elevPrompt">
  <xsl:choose>
    <xsl:when test="($CoordOrder='North-East-Elevation') or ($CoordOrder='East-North-Elevation')">Elevation</xsl:when>
    <xsl:otherwise>Z</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="dNorthPrompt">
  <xsl:choose>
    <xsl:when test="($CoordOrder='North-East-Elevation') or ($CoordOrder='East-North-Elevation')">Δ North</xsl:when>
    <xsl:when test="($CoordOrder='XYZ_CAD')">ΔY</xsl:when>
    <xsl:otherwise>ΔX</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="dEastPrompt">
  <xsl:choose>
    <xsl:when test="($CoordOrder='North-East-Elevation') or ($CoordOrder='East-North-Elevation')">Δ East</xsl:when>
    <xsl:when test="($CoordOrder='XYZ_CAD')">ΔX</xsl:when>
    <xsl:otherwise>ΔY</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="dElevPrompt">
  <xsl:choose>
    <xsl:when test="($CoordOrder='North-East-Elevation') or ($CoordOrder='East-North-Elevation')">ΔElev</xsl:when>
    <xsl:when test="($CoordOrder='XYZ_CAD')">ΔZ</xsl:when>
    <xsl:otherwise>ΔZ</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<!-- Setup conversion factor for pressure values -->
<!-- Pressure values in JobXML file are always in millibars (hPa) -->
<xsl:variable name="PressConvFactor">
  <xsl:choose>
    <xsl:when test="$PressUnit='MilliBar'">1.0</xsl:when>
    <xsl:when test="$PressUnit='InchHg'">0.029529921</xsl:when>
    <xsl:when test="$PressUnit='mmHg'">0.75006</xsl:when>
    <xsl:otherwise>1.0</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="hzPrecisionScalar">
  <xsl:choose>
    <xsl:when test="/JOBFile/Environment/DisplaySettings/PrecisionUnits = '1Sigma'">0.7071</xsl:when>    <!-- 1.0 / 1.4142 - conversion from DRMS (Distance Root Mean Square) to 1 sigma -->
    <xsl:when test="/JOBFile/Environment/DisplaySettings/PrecisionUnits = 'DRMS'">1.0</xsl:when>
    <xsl:when test="/JOBFile/Environment/DisplaySettings/PrecisionUnits = '95Percent'">1.7303</xsl:when> <!-- 2.447 / 1.4142 conversion from DRMS to 1 sigma then to 95% -->
    <xsl:when test="/JOBFile/Environment/DisplaySettings/PrecisionUnits = '99Percent'">2.1461</xsl:when> <!-- 3.035 / 1.4142 conversion from DRMS to 1 sigma then to 99% -->
    <xsl:otherwise>1.0</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="vtPrecisionScalar">
  <xsl:choose>
    <xsl:when test="/JOBFile/Environment/DisplaySettings/PrecisionUnits = '1Sigma'">1.0</xsl:when>
    <xsl:when test="/JOBFile/Environment/DisplaySettings/PrecisionUnits = 'DRMS'">1.0</xsl:when>
    <xsl:when test="/JOBFile/Environment/DisplaySettings/PrecisionUnits = '95Percent'">1.960</xsl:when>
    <xsl:when test="/JOBFile/Environment/DisplaySettings/PrecisionUnits = '99Percent'">2.575</xsl:when>
    <xsl:otherwise>1.0</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="hzPrecisionPrompt">
  <xsl:choose>
    <xsl:when test="/JOBFile/Environment/DisplaySettings/PrecisionUnits = '1Sigma'"> (1 sigma)</xsl:when>
    <xsl:when test="/JOBFile/Environment/DisplaySettings/PrecisionUnits = 'DRMS'"> (DRMS)</xsl:when>
    <xsl:when test="/JOBFile/Environment/DisplaySettings/PrecisionUnits = '95Percent'"> (95%)</xsl:when>
    <xsl:when test="/JOBFile/Environment/DisplaySettings/PrecisionUnits = '99Percent'"> (99%)</xsl:when>
    <xsl:otherwise></xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="vtPrecisionPrompt">
  <xsl:choose>
    <xsl:when test="/JOBFile/Environment/DisplaySettings/PrecisionUnits = '1Sigma'"> (1 sigma)</xsl:when>
    <xsl:when test="/JOBFile/Environment/DisplaySettings/PrecisionUnits = 'DRMS'"> (1 sigma)</xsl:when>
    <xsl:when test="/JOBFile/Environment/DisplaySettings/PrecisionUnits = '95Percent'"> (95%)</xsl:when>
    <xsl:when test="/JOBFile/Environment/DisplaySettings/PrecisionUnits = '99Percent'"> (99%)</xsl:when>
    <xsl:otherwise></xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="prodVersion">
  <xsl:choose>
    <xsl:when test="/JOBFile/@productVersion">
      <xsl:value-of select="/JOBFile/@product"/>
      <xsl:value-of select="' '"/>
      <xsl:value-of select="/JOBFile/@productVersion"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>Trimble Survey Controller </xsl:text>
      <xsl:value-of select="format-number(/JOBFile/@version div 100, $DecPl2, 'Standard')"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<!-- When the html file created by this style sheet is being converted to a pdf file, the font size  -->
<!-- setting can be quite critical in attempting to get a consistent pdf output on different devices -->
<!-- and operating systems. There are 3 parameters passed in by Trimble Access (saveAsPDF, onWindows -->
<!-- and dpiScale) that are used to achieve as consistent an output as possible.                     -->
<xsl:variable name="fontDefn">  <!-- Sets up the appropriate CSS text settings depending on the passed in parameters -->
  <xsl:choose>
    <xsl:when test="($onWindows = 'true') or ($saveAsPDF = 'false')">font-size: small;</xsl:when>
    <xsl:otherwise>
      <xsl:text>font-size: </xsl:text>
      <xsl:value-of select="ceiling($dpiScale * 12)"/>
      <xsl:text>px;</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="schemaVersion" select="JOBFile/@version"/>

<xsl:variable name="gonsDecPlaces">4</xsl:variable>

<xsl:variable name="WGS84SemiMajorAxis" select="6378137.000"/>
<xsl:variable name="WGS84Flattening" select="298.257222101"/>
<xsl:variable name="WGS84EccentricitySquared" select="0.00669438002290"/>
<xsl:variable name="Pi" select="3.14159265358979323846264"/>
<xsl:variable name="halfPi" select="$Pi div 2.0"/>

<!-- **************************************************************** -->
<!-- ************************** Main Loop *************************** -->
<!-- **************************************************************** -->
<xsl:template match="/" >
  <html>

  <title>Survey Report</title>
  <h1>Survey Report</h1>

  <!-- Set the font size for use in tables -->
  <style type="text/css">
    html { font-family: Arial }
    body, table, td, th
    {
      <xsl:value-of select="$fontDefn"/>
      padding: 2px;
    }

    table.border {border-style: solid; border-width: 1px; border-color: black; border-collapse: collapse;}
    table.noBorder {border-style: none; border-width: 0;}
  </style>

  <head>
  </head>

  <body>
    <xsl:call-template name="StartTable"/>
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Job name</xsl:with-param>
        <xsl:with-param name="Val" select="JOBFile/@jobName"/>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Creation date</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:call-template name="FormattedDateString">
            <xsl:with-param name="date" select="substring-before(JOBFile/@TimeStamp, 'T')"/>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Version</xsl:with-param>
        <xsl:with-param name="Val" select="$prodVersion"/>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Distance Units</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:choose>
            <xsl:when test="$DistUnit = 'InternationalFeet'">International feet</xsl:when>
            <xsl:when test="$DistUnit = 'USSurveyFeet'">US survey feet</xsl:when>
            <xsl:otherwise>Meters</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Angle units</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:choose>
            <xsl:when test="$AngleUnit = 'Gons'">Gons</xsl:when>
            <xsl:when test="$AngleUnit = 'Mils'">Mils</xsl:when>
            <xsl:otherwise>Degrees</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Pressure Units</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:choose>
            <xsl:when test="$PressUnit = 'InchHg'">inHg</xsl:when>
            <xsl:when test="$PressUnit = 'mmHg'">mmHg</xsl:when>
            <xsl:otherwise>mbar</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Temperature Units</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:choose>
            <xsl:when test="$TempUnit = 'Fahrenheit'">Fahrenheit</xsl:when>
            <xsl:otherwise>Celsius</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
      </xsl:call-template>
    <xsl:call-template name="EndTable"/>
    <xsl:call-template name="SeparatingLine"/>

    <!-- Output the Coordinate System details applying at time of export.                  -->
    <!-- This is also the coordinate system in terms of which all coordinates are computed -->
    <xsl:apply-templates select="JOBFile/Environment/CoordinateSystem"/>
    
    <xsl:if test="($RptContents = $AllStr) or ($RptContents = $FieldBookDataOnlyStr)">
      <xsl:call-template name="SeparatingLine"/>
      <xsl:call-template name="StartTable"/>
        <caption align="top"><p align="left"><b>
          <xsl:text>Collected Field Data</xsl:text>
          <xsl:if test="count(/JOBFile/FieldBook/PointRecord[ECEFDeltas and (Deleted = 'false')]) != 0">
            <xsl:text> (ECEF deltas: </xsl:text>
            <xsl:value-of select="$vectorDeltasType"/>
            <xsl:text>)</xsl:text>
          </xsl:if>
        </b></p></caption>
      <xsl:call-template name="EndTable"/>
      <xsl:call-template name="BlankLine"/>
      <!-- Select the FieldBook node to process -->
      <xsl:apply-templates select="JOBFile/FieldBook" />
    </xsl:if>
    
    <xsl:if test="($RptContents = $AllStr) or ($RptContents = $ReducedCoordinatesOnlyStr)">
      <!-- Select Reductions node to process -->
      <xsl:apply-templates select="JOBFile/Reductions" />
    </xsl:if>

  </body>
  </html>
</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** FieldBook Node Processing ******************** -->
<!-- **************************************************************** -->
<xsl:template match="FieldBook">
<!-- Process the records under the FieldBook node in the order encountered -->
  <xsl:for-each select="*">
    <xsl:choose>
      <!-- Handle Corrections record -->
      <xsl:when test="name(current()) = 'CorrectionsRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle Job Properties record -->
      <xsl:when test="name(current()) = 'JobPropertiesRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle Point record -->
      <xsl:when test="name(current()) = 'PointRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle Station record -->
      <xsl:when test="name(current()) = 'StationRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle BackBearing record -->
      <xsl:when test="name(current()) = 'BackBearingRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle Atmosphere record -->
      <xsl:when test="name(current()) = 'AtmosphereRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle StationResiduals record -->
      <xsl:when test="name(current()) = 'StationResiduals'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle BaseSurveyOptions record -->
      <xsl:when test="name(current()) = 'BaseSurveyOptionsRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle RoverSurveyOptions record -->
      <xsl:when test="name(current()) = 'RoverSurveyOptionsRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle SurveyEvent record -->
      <xsl:when test="name(current()) = 'SurveyEventRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle Reference record (GPS base point) -->
      <xsl:when test="name(current()) = 'ReferenceRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle Initialisation record -->
      <xsl:when test="name(current()) = 'InitialisationRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle Feature Coding record -->
      <xsl:when test="name(current()) = 'FeatureCodingRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle Instrument record -->
      <xsl:when test="name(current()) = 'InstrumentRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle Rounds mean turned angles residuals record -->
      <xsl:when test="name(current()) = 'RoundsMeanTurnedAngleResiduals'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle Road definition record -->
      <xsl:when test="name(current()) = 'RoadRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle Note record -->
      <xsl:when test="name(current()) = 'NoteRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle Line record -->
      <xsl:when test="name(current()) = 'LineRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle Arc record -->
      <xsl:when test="name(current()) = 'ArcRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle Inverse record -->
      <xsl:when test="name(current()) = 'InverseRecord'">
        <xsl:apply-templates select="current()"/>
      </xsl:when>

      <!-- Handle Calibration Point record -->
      <xsl:when test="name(current()) = 'CalibrationPointRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle GPS Equipment record -->
      <xsl:when test="name(current()) = 'GPSEquipmentRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle Post Process record -->
      <xsl:when test="name(current()) = 'PostProcessRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle Coordinate System record -->
      <xsl:when test="name(current()) = 'CoordinateSystemRecord'">
        <xsl:if test="$DetailedRpt = $YesStr">
          <xsl:apply-templates select="current()"/> 
        </xsl:if>
      </xsl:when>

      <!-- Handle Projection record -->
      <xsl:when test="name(current()) = 'ProjectionRecord'">
        <xsl:if test="$DetailedRpt = $YesStr">
          <xsl:apply-templates select="current()"/> 
        </xsl:if>
      </xsl:when>
      
      <!-- Handle Datum record -->
      <xsl:when test="name(current()) = 'DatumRecord'">
        <xsl:if test="$DetailedRpt = $YesStr">
          <xsl:apply-templates select="current()"/> 
        </xsl:if>
      </xsl:when>

      <!-- Handle Horizontal Adjustment record -->
      <xsl:when test="name(current()) = 'HorizontalAdjustmentRecord'">
        <xsl:if test="$DetailedRpt = $YesStr">
          <xsl:apply-templates select="current()"/> 
        </xsl:if>
      </xsl:when>

      <!-- Handle Vertical Adjustment record -->
      <xsl:when test="name(current()) = 'VerticalAdjustmentRecord'">
        <xsl:if test="$DetailedRpt = $YesStr">
          <xsl:apply-templates select="current()"/> 
        </xsl:if>
      </xsl:when>

      <!-- Handle Traverse Definition record -->
      <xsl:when test="name(current()) = 'TraverseDefinitionRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle Traverse Adjustment record -->
      <xsl:when test="name(current()) = 'TraverseAdjustmentRecord'">
      <!-- TraverseAdjustmentRecords can be written out a number of -->
      <!-- times within a file but we are only really interested in -->
      <!-- the one preceding a TraverseClosureRecord -->
      <xsl:if test="name(following-sibling::*[1]) = 'TraverseClosureRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:if>

      </xsl:when>

      <!-- Handle Traverse Closure record -->
      <xsl:when test="name(current()) = 'TraverseClosureRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle COGO Transformation record -->
      <xsl:when test="name(current()) = 'CogoTransformationRecord'">
        <xsl:apply-templates select="current()"/> 
      </xsl:when>

      <!-- Handle area computation record -->
      <xsl:when test="name(current()) = 'ComputeAreaRecord'">
        <xsl:apply-templates select="current()"/>
      </xsl:when>

      <!-- Handle subdivide area computation record -->
      <xsl:when test="name(current()) = 'SubdivideAreaRecord'">
        <xsl:apply-templates select="current()"/>
      </xsl:when>

      <!-- Handle volume computation record -->
      <xsl:when test="name(current()) = 'VolumeRecord'">
        <xsl:apply-templates select="current()"/>
      </xsl:when>

      <!-- Handle tilt sensor calibration record -->
      <xsl:when test="name(current()) = 'TiltSensorCalibrationRecord'">
        <xsl:apply-templates select="current()"/>
      </xsl:when>

      <!-- Handle SBAS offset record -->
      <xsl:when test="name(current()) = 'SBASOffsetRecord'">
        <xsl:apply-templates select="current()"/>
      </xsl:when>

      <!-- Handle RTX-RTK offset record -->
      <xsl:when test="name(current()) = 'RTXRTKOffsetRecord'">
        <xsl:apply-templates select="current()"/>
      </xsl:when>

      <!-- Handle recorded compute distance record -->
      <xsl:when test="name(current()) = 'ComputeDistanceRecord'">
        <xsl:apply-templates select="current()"/>
      </xsl:when>

      <!-- Handle panorama record -->
      <xsl:when test="name(current()) = 'PanoramaRecord'">
        <xsl:apply-templates select="current()"/>
      </xsl:when>

      <!-- Handle scanning record -->
      <xsl:when test="name(current()) = 'ScanningRecord'">
        <xsl:apply-templates select="current()"/>
      </xsl:when>

      <!-- Handle MediaFile record -->
      <xsl:when test="name(current()) = 'MediaFileRecord'">
        <xsl:apply-templates select="current()"/>
      </xsl:when>

      <!-- Handle image record (that is not part of a panorama) -->
      <xsl:when test="(name(current()) = 'ImageRecord') and (./PanoramaID = '') ">
        <xsl:apply-templates select="current()"/>
      </xsl:when>

    </xsl:choose>
  </xsl:for-each>
</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** Reductions Node Processing ******************* -->
<!-- **************************************************************** -->
<xsl:template match="Reductions">
  <xsl:call-template name="BlankLine"/>
  <xsl:call-template name="SeparatingLine"/>
  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left"><b>Reduced points</b></p></caption>
  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="BlankLine"/>
  <xsl:choose>
    <xsl:when test="@error">  <!-- There is an error attribute so the reduced coords have not been computed -->
      <xsl:value-of select="@error"/>  <!-- Display the error message -->
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="Point"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** CorrectionsRecord Output ********************* -->
<!-- **************************************************************** -->
<xsl:template match="CorrectionsRecord">
  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Corrections</p></caption>
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">South azimuth (grid)</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:if test="SouthAzimuth[.='true']">Yes</xsl:if>
        <xsl:if test="SouthAzimuth[.='false']">No</xsl:if>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Grid coords</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:if test="GridOrientation[.='IncreasingNorthEast']">Increase North-East</xsl:if>
        <xsl:if test="GridOrientation[.='IncreasingSouthWest']">Increase South-West</xsl:if>
        <xsl:if test="GridOrientation[.='IncreasingNorthWest']">Increase North-West</xsl:if>
        <xsl:if test="GridOrientation[.='IncreasingSouthEast']">Increase South-East</xsl:if>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Magnetic declination</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle" select="MagneticDeclination"/>
          <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
        </xsl:call-template>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Distances</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:if test="Distances[.='GroundDistance']">Ground</xsl:if>
        <xsl:if test="Distances[.='GridDistance']">Grid</xsl:if>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Neighborhood adjustment</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:if test="NeighbourhoodAdjustment/Applied[.='true']">On</xsl:if>
        <xsl:if test="NeighbourhoodAdjustment/Applied[.='false']">Off</xsl:if>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:if test="NeighbourhoodAdjustment/Applied[.='true']">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Weight exponent</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:variable name="WeightExp" select="NeighbourhoodAdjustment/WeightExponent" />
          <xsl:value-of select="format-number($WeightExp, $DecPl1, 'Standard')" />    
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** JobPropertiesRecord Output ******************* -->
<!-- **************************************************************** -->
<xsl:template match="JobPropertiesRecord">
  <xsl:if test="Reference != '' or Description != '' or Operator != '' or JobNote != ''">
    <xsl:call-template name="StartTable"/>
      <caption align="top"><p align="left">Job properties</p></caption>
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Reference</xsl:with-param>
        <xsl:with-param name="Val" select="Reference"/>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Description</xsl:with-param>
        <xsl:with-param name="Val" select="Description"/>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Operator</xsl:with-param>
        <xsl:with-param name="Val" select="Operator"/>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Notes</xsl:with-param>
        <xsl:with-param name="Val" select="JobNote"/>
      </xsl:call-template>
  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>
  </xsl:if>
</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** FeatureCodingRecord Output ******************* -->
<!-- **************************************************************** -->
<xsl:template match="FeatureCodingRecord">
  <xsl:if test="LibraryName != ''">
    <xsl:call-template name="StartTable"/>
      <caption align="top"><p align="left">Feature library</p></caption>
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Library name</xsl:with-param>
        <xsl:with-param name="Val" select="LibraryName"/>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Library File Name</xsl:with-param>
        <xsl:with-param name="Val" select="SourceFilename"/>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Attribute Support</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:if test="AttributesStored[.='true']">Yes</xsl:if>
          <xsl:if test="AttributesStored[.='false']">No</xsl:if>
        </xsl:with-param>
      </xsl:call-template>

  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>
  </xsl:if>
</xsl:template>


<!-- **************************************************************** -->
<!-- **************** CoordinateSystemRecord Output ***************** -->
<!-- **************************************************************** -->
<xsl:template match="CoordinateSystemRecord">
  <xsl:if test="SystemName != '' or ZoneName != '' or DatumName != ''">
    <xsl:call-template name="StartTable"/>
      <caption align="top"><p align="left">Coordinate system</p></caption>
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">System</xsl:with-param>
        <xsl:with-param name="Val" select="SystemName"/>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Zone</xsl:with-param>
        <xsl:with-param name="Val" select="ZoneName"/>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Datum</xsl:with-param>
        <xsl:with-param name="Val" select="DatumName"/>
      </xsl:call-template>
  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>
  </xsl:if>
</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** InstrumentRecord Output ********************* -->
<!-- **************************************************************** -->
<xsl:template match="InstrumentRecord">
  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Instrument</p></caption>
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Instrument type</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:call-template name="InstrumentTypeText">
          <xsl:with-param name="Type" select="Type"/>
        </xsl:call-template>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">EDM Refractive Index</xsl:with-param>
      <xsl:with-param name="Val" select="format-number(RefractiveIndex, $DecPl1, 'Standard')"/>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">EDM Carrier Wavelength</xsl:with-param>
      <xsl:with-param name="Val" select="format-number(CarrierWavelength, $DecPl1, 'Standard')"/>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Horizontal circle mode</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:if test="SetHorizontalCircleMode[.='DoNothing']">None</xsl:if>
        <xsl:if test="SetHorizontalCircleMode[.='Zero']">Set to zero</xsl:if>
        <xsl:if test="SetHorizontalCircleMode[.='SetToAzimuth']">Set to azimuth</xsl:if>
        <xsl:if test="SetHorizontalCircleMode[.='SetHorizontalNull']">None</xsl:if>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Horizontal Angle Precision</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle" select="HorizontalAnglePrecision"/>
          <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
        </xsl:call-template>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Vertical Angle Precision</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle" select="VerticalAnglePrecision"/>
          <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
        </xsl:call-template>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">EDM precision</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:value-of select="format-number(EDMPrecision * 1000, $DecPl0, 'Standard')"/><xsl:text>mm +</xsl:text>
        <xsl:value-of select="EDMppm"/><xsl:text>ppm</xsl:text>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:if test="InstrumentAppliedEDMConstant and (string(number(InstrumentAppliedEDMConstant)) != 'NaN')">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">EDM constant</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:value-of select="format-number(InstrumentAppliedEDMConstant * 1000, $DecPl0, 'Standard')"/>
          <xsl:text>mm</xsl:text>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="CenteringError != 0">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Instrument centering error</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(CenteringError * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="BacksightCenteringError != 0">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Backsight centering error</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(BacksightCenteringError * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>
  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>

  <!-- Now output the Instrument details table -->
  <xsl:call-template name="InstrumentDetailsRecord"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** Instrument Type Text Output ****************** -->
<!-- **************************************************************** -->
<xsl:template name="InstrumentTypeText">
  <xsl:param name="Type"/>

  <xsl:choose>
    <xsl:when test="$Type = 'TrimbleTTS'">Trimble TTS</xsl:when>
    <xsl:when test="$Type = 'Trimble3300'">Trimble 3300</xsl:when>
    <xsl:when test="$Type = 'Trimble3600Elta'">Trimble 3600 Elta</xsl:when>
    <xsl:when test="$Type = 'Trimble3600GDM'">Trimble 3600 GDM</xsl:when>
    <xsl:when test="$Type = 'Trimble600M'">Trimble 600M</xsl:when>
    <xsl:when test="$Type = 'Trimble5600'">Trimble 5600</xsl:when>
    <xsl:when test="$Type = 'TrimbleSSeries'">Trimble S Series</xsl:when>
    <xsl:when test="$Type = 'TrimbleVXandSSeries'">Trimble VX/S Series</xsl:when>
    <xsl:when test="$Type = 'TrimbleSX10'">Trimble SX10</xsl:when>
    <xsl:when test="($Type = 'SETBasic') or ($Type = 'SETEnhanced')">Sokkia SET</xsl:when>
    <xsl:when test="$Type = 'Geodimeter400'">Geodimeter 400</xsl:when>
    <xsl:when test="$Type = 'Geodimeter500Servo'">Geodimeter 500 Servo</xsl:when>
    <xsl:when test="$Type = 'Geodimeter600'">Geodimeter 600</xsl:when>
    <xsl:when test="$Type = 'Geodimeter600Servo'">Geodimeter 600 Servo</xsl:when>
    <xsl:when test="$Type = 'Geodimeter600Robotic'">Geodimeter 600 Robotic</xsl:when>
    <xsl:when test="$Type = 'Geodimeter4000'">Geodimeter 4000</xsl:when>
    <xsl:when test="$Type = 'LeicaTC300'">Leica TC300</xsl:when>
    <xsl:when test="$Type = 'LeicaTC500'">Leica TC500</xsl:when>
    <xsl:when test="$Type = 'LeicaTC800'">Leica TC800</xsl:when>
    <xsl:when test="($Type = 'LeicaT1000_6') or ($Type = 'LeicaT1000_14')">Leica T1000</xsl:when>
    <xsl:when test="$Type = 'LeicaTC1100'">Leica TC1100</xsl:when>
    <xsl:when test="$Type = 'LeicaTC1100ServoGeoCom'">Leica TC1100 Servo</xsl:when>
    <xsl:when test="$Type = 'LeicaTC1100RoboticGeoCom'">Leica TC1100 Robotic</xsl:when>
    <xsl:when test="$Type = 'LeicaTC1600'">Leica TC1600</xsl:when>
    <xsl:when test="$Type = 'LeicaTC2000'">Leica TC2000</xsl:when>
    <xsl:when test="$Type = 'Nikon'">Nikon</xsl:when>
    <xsl:when test="$Type = 'Pentax'">Pentax</xsl:when>
    <xsl:when test="$Type = 'TopconGeneric'">Topcon</xsl:when>
    <xsl:when test="$Type = 'Topcon500'">Topcon 500</xsl:when>
    <xsl:when test="$Type = 'ZeissElta2'">Zeiss Elta2</xsl:when>
    <xsl:when test="$Type = 'ZeissElta3'">Zeiss Elta3</xsl:when>
    <xsl:when test="$Type = 'ZeissElta4'">Zeiss Elta4</xsl:when>
    <xsl:when test="$Type = 'ZeissEltaC'">Zeiss EltaC</xsl:when>
    <xsl:when test="$Type = 'ZeissRecEltaE'">Zeiss RecEltaE</xsl:when>
    <xsl:when test="$Type = 'ZeissRSeries'">Zeiss R Series</xsl:when>
    <xsl:when test="$Type = 'Manual'">Manual</xsl:when>
    <xsl:otherwise><xsl:value-of select="$Type"/></xsl:otherwise>  <!-- Simply return the passed in Type -->
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- *************** InstrumentDetailsRecord Output ***************** -->
<!-- **************************************************************** -->
<xsl:template name="InstrumentDetailsRecord">
  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Instrument details</p></caption>
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Model</xsl:with-param>
      <xsl:with-param name="Val" select="Model"/>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Serial number</xsl:with-param>
      <xsl:with-param name="Val" select="Serial"/>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Firmware version</xsl:with-param>
      <xsl:with-param name="Val" select="FirmwareVersion"/>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Horizontal collimation</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle" select="InstrumentAppliedHorizontalCollimation"/>
          <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
        </xsl:call-template>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Vertical collimation</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle" select="InstrumentAppliedVerticalCollimation"/>
          <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
        </xsl:call-template>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Trunnion axis tilt correction</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle" select="TrunionAxisTiltCorrection"/>
          <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
        </xsl:call-template>
      </xsl:with-param>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************** PointRecord Output ************************ -->
<!-- **************************************************************** -->
<xsl:template match="PointRecord">
  <xsl:if test="Deleted = 'false'">  <!-- only output if not deleted -->
    <xsl:if test="Grid">
      <xsl:call-template name="GridPoint"/> 
    </xsl:if>

    <xsl:if test="Circle">
      <!-- Only output the observation if it is not from a Scan station setup (no station position or orientation) -->
      <xsl:variable name="stnID" select="StationID"/>
      <xsl:if test="/JOBFile/FieldBook/StationRecord[@ID = $stnID]/StationType != 'ScanStation'">
        <xsl:call-template name="Observation"/>
      </xsl:if>
    </xsl:if>

    <xsl:if test="MTA">
      <xsl:call-template name="MeanTurnedAngle"/> 
    </xsl:if>

    <xsl:if test="ECEF or ECEFDeltas">
      <xsl:call-template name="GPSPosOrDeltas"/>
      <xsl:if test="($DetailedRpt = $YesStr) and QualityControl1/Warnings">
        <xsl:call-template name="OutputGNSSWarningsTable"/>
      </xsl:if>
    </xsl:if>

    <xsl:if test="RTXECEF">
      <xsl:call-template name="RTXPos"/>
    </xsl:if>

    <xsl:if test="WGS84">
      <xsl:call-template name="WGS84Pos"/> 
    </xsl:if>

    <xsl:if test="Local">
      <xsl:call-template name="LocalPos"/>
    </xsl:if>

    <xsl:if test="Polar">
      <xsl:call-template name="Polar"/> 
    </xsl:if>

    <xsl:if test="Features">  <!-- Point has feature and attribute details - write them out -->
      <xsl:call-template name="FeatureAndAttributes"/> 
    </xsl:if>

    <xsl:if test="Stakeout">  <!-- Point has stakeout data - write it out -->
      <xsl:call-template name="Stakeout"/> 
    </xsl:if>

    <xsl:if test="LaserOffset">
      <xsl:call-template name="LaserOffset"/> 
    </xsl:if>

    <xsl:if test="ResectionStandardErrors">
      <xsl:call-template name="ResectionStandardErrors"/> 
    </xsl:if>

    <xsl:if test="EnteredData">
      <xsl:call-template name="EnteredData"/> 
    </xsl:if>

    <xsl:if test="Measure3DAxes">
      <xsl:call-template name="Measure3DAxes"/>
    </xsl:if>

    <xsl:if test="MeasureToSurfacePoint">
      <xsl:call-template name="MeasureToSurfacePoint"/>
    </xsl:if>
  </xsl:if>
</xsl:template>


<!-- **************************************************************** -->
<!-- **************** Grid Point Details Output ********************* -->
<!-- **************************************************************** -->
<xsl:template name="GridPoint">
  <xsl:param name="reductionsPoint" select="'false'"/>
  
  <xsl:call-template name="StartTable"/>
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1"><xsl:text>Point</xsl:text>
        <xsl:if test="Method = 'TraverseAdjusted'"><xsl:text> (Adjusted)</xsl:text></xsl:if>
      </xsl:with-param>
      <xsl:with-param name="Val1" select="Name"/>

      <xsl:with-param name="Hdr2">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'"><xsl:value-of select="$northPrompt"/></xsl:when>
          <xsl:otherwise><xsl:value-of select="$eastPrompt"/></xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'">
            <xsl:value-of select="format-number(Grid/North * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number(Grid/East * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>

      <xsl:with-param name="Hdr3">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'"><xsl:value-of select="$eastPrompt"/></xsl:when>
          <xsl:otherwise><xsl:value-of select="$northPrompt"/></xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'">
            <xsl:value-of select="format-number(Grid/East * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number(Grid/North * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>

      <xsl:with-param name="Hdr4" select="$elevPrompt"/>
      <xsl:with-param name="Val4">
        <xsl:choose>
          <xsl:when test="$reductionsPoint != 'false'">
            <!-- Check to see if the referenced PointRecord has an EchoSounding element -->
            <!-- and if so apply the depth value to get the resultant elevation.        -->
            <xsl:variable name="ptRecID" select="ID"/>
            <xsl:choose>
              <xsl:when test="/JOBFile/FieldBook/PointRecord[@ID = $ptRecID]/EchoSounding">
                <xsl:variable name="depth" select="/JOBFile/FieldBook/PointRecord[@ID = $ptRecID]/EchoSounding/Depth"/>
                <xsl:value-of select="format-number((Grid/Elevation - $depth) * $ElevConvFactor, $DecPl3, 'Standard')"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="format-number(Grid/Elevation * $ElevConvFactor, $DecPl3, 'Standard')"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number(Grid/Elevation * $ElevConvFactor, $DecPl3, 'Standard')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>

      <xsl:with-param name="Hdr5">Code</xsl:with-param>
      <xsl:with-param name="Val5" select="Code"/>
    </xsl:call-template>
    <!-- Now output any point descriptions -->
    <xsl:call-template name="OutputDescriptions"/>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************* WGS-84 Position Output ********************* -->
<!-- **************************************************************** -->
<xsl:template name="WGS84Pos">
  <xsl:call-template name="StartTable"/>
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Point (Global)</xsl:with-param>
      <xsl:with-param name="Val1" select="Name"/>

      <xsl:with-param name="Hdr2">Latitude</xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:call-template name="FormatLatLong">
          <xsl:with-param name="theAngle" select="WGS84/Latitude"/>
          <xsl:with-param name="northLbl">N</xsl:with-param>
          <xsl:with-param name="eastLbl">E</xsl:with-param>
          <xsl:with-param name="southLbl">S</xsl:with-param>
          <xsl:with-param name="westLbl">W</xsl:with-param>
        </xsl:call-template>
      </xsl:with-param>

      <xsl:with-param name="Hdr3">Longitude</xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:call-template name="FormatLatLong">
          <xsl:with-param name="theAngle" select="WGS84/Longitude"/>
          <xsl:with-param name="isLat" select="'false'"/>
          <xsl:with-param name="northLbl">N</xsl:with-param>
          <xsl:with-param name="eastLbl">E</xsl:with-param>
          <xsl:with-param name="southLbl">S</xsl:with-param>
          <xsl:with-param name="westLbl">W</xsl:with-param>
        </xsl:call-template>
      </xsl:with-param>

      <xsl:with-param name="Hdr4">Height</xsl:with-param>
      <xsl:with-param name="Val4" select="format-number(WGS84/Height * $ElevConvFactor, $DecPl3, 'Standard')"/>

      <xsl:with-param name="Hdr5">Code</xsl:with-param>
      <xsl:with-param name="Val5" select="Code"/>
    </xsl:call-template>
    <!-- Now output any point descriptions -->
    <xsl:call-template name="OutputDescriptions"/>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************** Local ellipsoid Position Output ***************** -->
<!-- **************************************************************** -->
<xsl:template name="LocalPos">
  <xsl:call-template name="StartTable"/>
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Point (Local)</xsl:with-param>
      <xsl:with-param name="Val1" select="Name"/>

      <xsl:with-param name="Hdr2">Latitude</xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:call-template name="FormatLatLong">
          <xsl:with-param name="theAngle" select="Local/Latitude"/>
          <xsl:with-param name="northLbl">N</xsl:with-param>
          <xsl:with-param name="eastLbl">E</xsl:with-param>
          <xsl:with-param name="southLbl">S</xsl:with-param>
          <xsl:with-param name="westLbl">W</xsl:with-param>
        </xsl:call-template>
      </xsl:with-param>

      <xsl:with-param name="Hdr3">Longitude</xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:call-template name="FormatLatLong">
          <xsl:with-param name="theAngle" select="Local/Longitude"/>
          <xsl:with-param name="isLat" select="'false'"/>
          <xsl:with-param name="northLbl">N</xsl:with-param>
          <xsl:with-param name="eastLbl">E</xsl:with-param>
          <xsl:with-param name="southLbl">S</xsl:with-param>
          <xsl:with-param name="westLbl">W</xsl:with-param>
        </xsl:call-template>
      </xsl:with-param>

      <xsl:with-param name="Hdr4">Height</xsl:with-param>
      <xsl:with-param name="Val4" select="format-number(Local/Height * $ElevConvFactor, $DecPl3, 'Standard')"/>

      <xsl:with-param name="Hdr5">Code</xsl:with-param>
      <xsl:with-param name="Val5" select="Code"/>
    </xsl:call-template>
    <!-- Now output any point descriptions -->
    <xsl:call-template name="OutputDescriptions"/>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- **************** Observation Details Output ******************** -->
<!-- **************************************************************** -->
<xsl:template name="Observation">
  <xsl:call-template name="StartTable"/>
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1"><xsl:text>Point</xsl:text>
        <xsl:if test="Classification = 'BackSight'"><xsl:text> (B.S.)</xsl:text></xsl:if>
      </xsl:with-param>
      <xsl:with-param name="Val1" select="Name"/>

      <xsl:with-param name="Hdr2">HA</xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle" select="Circle/HorizontalCircle"/>
          <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
        </xsl:call-template>
      </xsl:with-param>

      <xsl:with-param name="Hdr3">VA</xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle" select="Circle/VerticalCircle"/>
          <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
        </xsl:call-template>
      </xsl:with-param>

      <xsl:with-param name="Hdr4">SD</xsl:with-param>
      <xsl:with-param name="Val4" select="format-number(Circle/EDMDistance * $DistConvFactor, $DecPl3, 'Standard')"/>

      <xsl:with-param name="Hdr5">Code</xsl:with-param>
      <xsl:with-param name="Val5" select="Code"/>
    </xsl:call-template>
    <!-- Now output any point descriptions -->
    <xsl:call-template name="OutputDescriptions"/>

    <xsl:if test="($DetailedRpt = $YesStr) and
                  ((string(number(Circle/HorizontalCircleStandardError)) != 'NaN') or
                   (string(number(Circle/VerticalCircleStandardError)) != 'NaN') or
                   (string(number(Circle/EDMDistanceStandardError)) != 'NaN'))">
      <!-- Add a second line containing the observation std error values -->
      <xsl:call-template name="OutputTableLine">
        <xsl:with-param name="Hdr1">Std Errors</xsl:with-param> <!-- No value associated with this -->

        <xsl:with-param name="Hdr2">HA</xsl:with-param>
        <xsl:with-param name="Val2">
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="Circle/HorizontalCircleStandardError"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
        </xsl:with-param>

        <xsl:with-param name="Hdr3">VA</xsl:with-param>
        <xsl:with-param name="Val3">
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="Circle/VerticalCircleStandardError"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
        </xsl:with-param>

        <xsl:with-param name="Hdr4">SD</xsl:with-param>
        <xsl:with-param name="Val4" select="format-number(Circle/EDMDistanceStandardError * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <!-- Add a third line containing the target ht and prism constant values plus tilt distance if details available -->
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Target height</xsl:with-param>
      <xsl:with-param name="Val1"> 
        <xsl:for-each select="key('tgtID-search', TargetID)[1]">
          <xsl:value-of select="format-number(TargetHeight * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:for-each>
      </xsl:with-param>
      
      <xsl:with-param name="Hdr2">Prism constant</xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:for-each select="key('tgtID-search', TargetID)[1]">
          <xsl:value-of select="format-number(PrismConstant * 1000, $DecPl1, 'Standard')"/><xsl:text>mm</xsl:text>
        </xsl:for-each>
      </xsl:with-param>

      <xsl:with-param name="Hdr3">
        <xsl:if test="DeviceAxisOrientation">Tilt distance</xsl:if>  <!-- There are tilt details available -->
      </xsl:with-param>

      <xsl:with-param name="Val3">
        <xsl:if test="DeviceAxisOrientation">                         <!-- There are tilt details available -->
          <xsl:variable name="tiltDist">
            <xsl:call-template name="ComputeTiltDistance">
              <xsl:with-param name="reducedAntennaTargetHt">
                <xsl:for-each select="key('tgtID-search', TargetID)[1]">
                  <xsl:value-of select="TargetHeight"/>
                </xsl:for-each>
              </xsl:with-param>
            </xsl:call-template>
          </xsl:variable>
          <xsl:value-of select="format-number($tiltDist * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:if>
      </xsl:with-param>

      <xsl:with-param name="Hdr4">
        <xsl:if test="TargetLockDetails">Target locked</xsl:if>  <!-- There is a target lock record -->
      </xsl:with-param>

      <xsl:with-param name="Val4">
        <xsl:if test="TargetLockDetails">                         <!-- There is a target lock record -->
          <xsl:choose>
            <xsl:when test="TargetLockDetails/TargetLocked = 'true'">Yes</xsl:when>
            <xsl:otherwise>No</xsl:otherwise>
          </xsl:choose>
        </xsl:if>
      </xsl:with-param>

      <xsl:with-param name="Hdr5">
        <xsl:if test="TargetLockDetails">Lock type</xsl:if>      <!-- There is a target lock record -->
      </xsl:with-param>

      <xsl:with-param name="Val5">
        <xsl:if test="TargetLockDetails">                         <!-- There is a target lock record -->
          <xsl:choose>
            <xsl:when test="TargetLockDetails/LockType = 'AutoLock'">Autolock</xsl:when>
            <xsl:when test="TargetLockDetails/LockType = 'FineLock'">FineLock</xsl:when>
            <xsl:when test="TargetLockDetails/LockType = 'LongRangeFineLock'">LR FineLock</xsl:when>
            <xsl:otherwise>None</xsl:otherwise>
          </xsl:choose>
        </xsl:if>
      </xsl:with-param>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>

  <xsl:if test="TargetLockDetails">
  
  </xsl:if>

  <xsl:if test="DistanceOffset"> <!-- Add Distance offset details -->
    <xsl:call-template name="StartTable"/>
      <xsl:variable name="offsetStr">
        <xsl:choose>
          <xsl:when test="DistanceOffset/Direction">  <!-- This is a pre v11.30 distance offset record -->
            <xsl:variable name="dirStr">
              <xsl:if test="DistanceOffset/Direction = 'Right'">Right</xsl:if>
              <xsl:if test="DistanceOffset/Direction = 'Left'">Left</xsl:if>
              <xsl:if test="DistanceOffset/Direction = 'Out'">Out</xsl:if>
              <xsl:if test="DistanceOffset/Direction = 'In'">In</xsl:if>
            </xsl:variable>
            <xsl:value-of select="concat(format-number(DistanceOffset/Distance * $DistConvFactor, $DecPl3, 'Standard'), ' ', $dirStr)"/>
          </xsl:when>
          <xsl:otherwise>  <!-- This must be the new v11.30 form of distance offset record -->
            <xsl:variable name="LeftRightStr">
              <xsl:if test="DistanceOffset/LeftRightOffset != 0.0">
                <xsl:value-of select="format-number(concat(substring('-',2 - (DistanceOffset/LeftRightOffset &lt; 0)), '1') * DistanceOffset/LeftRightOffset * $DistConvFactor, $DecPl3, 'Standard')"/> <!-- Output the absolute value -->
                <xsl:if test="DistanceOffset/LeftRightOffset &lt; 0.0"> Left</xsl:if>
                <xsl:if test="DistanceOffset/LeftRightOffset &gt;= 0.0"> Right</xsl:if>
              </xsl:if>
            </xsl:variable>
            <xsl:variable name="InOutStr">
              <xsl:if test="DistanceOffset/InOutOffset != 0.0">
                <xsl:value-of select="format-number(concat(substring('-',2 - (DistanceOffset/InOutOffset &lt; 0)), '1') * DistanceOffset/InOutOffset * $DistConvFactor, $DecPl3, 'Standard')"/> <!-- Output the absolute value -->
                <xsl:if test="DistanceOffset/InOutOffset &lt; 0.0"> In</xsl:if>
                <xsl:if test="DistanceOffset/InOutOffset &gt;= 0.0"> Out</xsl:if>
              </xsl:if>
            </xsl:variable>
            <xsl:variable name="DownUpStr">
              <xsl:if test="DistanceOffset/DownUpOffset != 0.0">
                <xsl:value-of select="format-number(concat(substring('-',2 - (DistanceOffset/DownUpOffset &lt; 0)), '1') * DistanceOffset/DownUpOffset * $DistConvFactor, $DecPl3, 'Standard')"/> <!-- Output the absolute value -->
                <xsl:if test="DistanceOffset/DownUpOffset &lt; 0.0"> Down</xsl:if>
                <xsl:if test="DistanceOffset/DownUpOffset &gt;= 0.0"> Up</xsl:if>
              </xsl:if>
            </xsl:variable>
            <xsl:variable name="tempStr">
              <xsl:choose>
                <xsl:when test="(string-length($LeftRightStr) != 0) and (string-length($InOutStr) != 0)">
                  <xsl:value-of select="concat($LeftRightStr, ', ', $InOutStr)"/>
                </xsl:when>
                <xsl:when test="(string-length($LeftRightStr) != 0) and (string-length($InOutStr) = 0)">
                  <xsl:value-of select="$LeftRightStr"/>
                </xsl:when>
                <xsl:when test="(string-length($LeftRightStr) = 0) and (string-length($InOutStr) != 0)">
                  <xsl:value-of select="$InOutStr"/>
                </xsl:when>
                <xsl:otherwise><xsl:value-of select="''"/></xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
            <xsl:choose>
              <xsl:when test="(string-length($tempStr) != 0) and (string-length($DownUpStr) != 0)">
                <xsl:value-of select="concat($tempStr, ', ', $DownUpStr)"/>
              </xsl:when>
              <xsl:when test="(string-length($tempStr) != 0) and (string-length($DownUpStr) = 0)">
                <xsl:value-of select="$tempStr"/>
              </xsl:when>
              <xsl:when test="(string-length($tempStr) = 0) and (string-length($DownUpStr) != 0)">
                <xsl:value-of select="$DownUpStr"/>
              </xsl:when>
              <xsl:otherwise><xsl:value-of select="''"/></xsl:otherwise>
            </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr"><xsl:text>Distance offset</xsl:text><xsl:value-of select="concat(' (', Name, ')')"/></xsl:with-param>
        <xsl:with-param name="Val" select="$offsetStr"/>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Raw Observation</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:text>HA: </xsl:text>
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="DistanceOffset/RawObservation/HorizontalCircle"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
          <xsl:text> VA: </xsl:text>
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="DistanceOffset/RawObservation/VerticalCircle"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
          <xsl:text> SD: </xsl:text>
          <xsl:value-of select="format-number(DistanceOffset/RawObservation/EDMDistance * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:with-param>
      </xsl:call-template>
    <xsl:call-template name="EndTable"/>
  </xsl:if>

  <xsl:if test="CircularObject"> <!-- Add circular object details -->
    <xsl:call-template name="StartTable"/>
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr"><xsl:text>Circular object</xsl:text><xsl:value-of select="concat(' (', Name, ')')"/></xsl:with-param>
        <xsl:with-param name="Val"><xsl:text>Radius: </xsl:text><xsl:value-of select="format-number(CircularObject/Radius * $DistConvFactor, $DecPl3, 'Standard')"/></xsl:with-param>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">First Raw Observation</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:text>HA: </xsl:text>
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="CircularObject/FirstRawObservation/HorizontalCircle"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
          <xsl:text> VA: </xsl:text>
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="CircularObject/FirstRawObservation/VerticalCircle"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
          <xsl:text> SD: </xsl:text>
          <xsl:value-of select="format-number(CircularObject/FirstRawObservation/EDMDistance * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:with-param>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Second Raw Observation</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:text>HA: </xsl:text>
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="CircularObject/SecondRawObservation/HorizontalCircle"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
          <xsl:text> VA: </xsl:text>
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="CircularObject/SecondRawObservation/VerticalCircle"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    <xsl:call-template name="EndTable"/>
  </xsl:if>

  <xsl:if test="RemoteObject"> <!-- Add Remote object details -->
    <xsl:call-template name="StartTable"/>
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr"><xsl:text>Remote object</xsl:text><xsl:value-of select="concat(' (', Name, ')')"/></xsl:with-param>
          <xsl:with-param name="Val">
            <xsl:variable name="ObjHeight">
              <xsl:if test="RemoteObject/ObjectHeight != ''">
                <xsl:text>Object height</xsl:text>
                <xsl:value-of select="concat(': ', format-number(RemoteObject/ObjectHeight * $DistConvFactor, $DecPl3, 'Standard'), ' ')"/>
              </xsl:if>
            </xsl:variable>
            <xsl:variable name="ObjWidth">
              <xsl:if test="RemoteObject/ObjectWidth != ''">
                <xsl:text>Object width</xsl:text>
                <xsl:value-of select="concat(': ', format-number(RemoteObject/ObjectWidth * $DistConvFactor, $DecPl3, 'Standard'))"/>
              </xsl:if>
            </xsl:variable>
            <xsl:value-of select="concat($ObjHeight, $ObjWidth)"/>
          </xsl:with-param>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Raw Observation</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:text>HA: </xsl:text>
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="RemoteObject/RawObservation/HorizontalCircle"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
          <xsl:text> VA: </xsl:text>
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="RemoteObject/RawObservation/VerticalCircle"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    <xsl:call-template name="EndTable"/>
  </xsl:if>

  <xsl:if test="AngleOffset"> <!-- Add Angle offset details -->
    <xsl:call-template name="StartTable"/>
      <xsl:variable name="offsetStr">
        <xsl:choose>
          <xsl:when test="Method = 'HorizontalAngleOffset'">H. Angle offset</xsl:when>
          <xsl:when test="Method = 'VerticalAngleOffset'">V. Angle offset</xsl:when>
          <xsl:otherwise>Angle offset</xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr" select="concat($offsetStr, ' (', Name, ')')"/>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">First Raw Observation</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:text>HA: </xsl:text>
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="AngleOffset/FirstRawObservation/HorizontalCircle"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
          <xsl:text> VA: </xsl:text>
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="AngleOffset/FirstRawObservation/VerticalCircle"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
          <xsl:text> SD: </xsl:text>
          <xsl:value-of select="format-number(AngleOffset/FirstRawObservation/EDMDistance * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:with-param>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Second Raw Observation</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:text>HA: </xsl:text>
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="AngleOffset/SecondRawObservation/HorizontalCircle"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
          <xsl:text> VA: </xsl:text>
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="AngleOffset/SecondRawObservation/VerticalCircle"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    <xsl:call-template name="EndTable"/>
  </xsl:if>

  <xsl:if test="DualPrismOffset"> <!-- Add Dual prism offset details -->
    <xsl:call-template name="StartTable"/>
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr"><xsl:text>Dual-prism offset</xsl:text><xsl:value-of select="concat(' (', Name, ')')"/></xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:text>Distance BC: </xsl:text>
          <xsl:value-of select="format-number(DualPrismOffset/DistanceBC * $DistConvFactor, $DecPl3, 'Standard')"/>
          <xsl:text> Computed AB: </xsl:text>
          <xsl:value-of select="format-number(DualPrismOffset/CalculatedDistanceAB * $DistConvFactor, $DecPl3, 'Standard')"/>
          <xsl:text> Prism constant A: </xsl:text>
          <xsl:value-of select="format-number(DualPrismOffset/PrismConstantA * 1000, $DecPl3, 'Standard')"/>
          <xsl:text>mm Prism constant B: </xsl:text>
          <xsl:value-of select="format-number(DualPrismOffset/PrismConstantB * 1000, $DecPl3, 'Standard')"/>
          <xsl:text>mm</xsl:text>
        </xsl:with-param>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Prism A observation</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:text>HA: </xsl:text>
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="DualPrismOffset/PrismAObservation/HorizontalCircle"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
          <xsl:text> VA: </xsl:text>
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="DualPrismOffset/PrismAObservation/VerticalCircle"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
          <xsl:text> SD: </xsl:text>
          <xsl:value-of select="format-number(DualPrismOffset/PrismAObservation/EDMDistance * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:with-param>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Prism B observation</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:text>HA: </xsl:text>
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="DualPrismOffset/PrismBObservation/HorizontalCircle"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
          <xsl:text> VA: </xsl:text>
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="DualPrismOffset/PrismBObservation/VerticalCircle"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
          <xsl:text> SD: </xsl:text>
          <xsl:value-of select="format-number(DualPrismOffset/PrismBObservation/EDMDistance * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:with-param>
      </xsl:call-template>
    <xsl:call-template name="EndTable"/>
  </xsl:if>

  <xsl:if test="ObservationPolarDeltas">
    <xsl:call-template name="StartTable"/>

      <xsl:call-template name="OutputTableLine">
        <xsl:with-param name="Hdr1">Deltas</xsl:with-param>
        <xsl:with-param name="Val1" select="Name"/>

        <xsl:with-param name="Hdr2">Azimuth</xsl:with-param>
        <xsl:with-param name="Val2">
          <xsl:call-template name="FormatAzimuth">
            <xsl:with-param name="theAzimuth" select="ObservationPolarDeltas/Azimuth"/>
            <xsl:with-param name="northLbl">N</xsl:with-param>
            <xsl:with-param name="eastLbl">E</xsl:with-param>
            <xsl:with-param name="southLbl">S</xsl:with-param>
            <xsl:with-param name="westLbl">W</xsl:with-param>
          </xsl:call-template>
        </xsl:with-param>
        
        <xsl:with-param name="Hdr3">H.Dist</xsl:with-param>
        <xsl:with-param name="Val3" select="format-number(ObservationPolarDeltas/HorizontalDistance * $DistConvFactor, $DecPl3, 'Standard')"/>

        <xsl:with-param name="Hdr4">V.Dist</xsl:with-param>
        <xsl:with-param name="Val4" select="format-number(ObservationPolarDeltas/VerticalDistance * $DistConvFactor, $DecPl3, 'Standard')"/>

      </xsl:call-template>
    <xsl:call-template name="EndTable"/>
  </xsl:if>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************************* Polar Output ************************* -->
<!-- **************************************************************** -->
<xsl:template name="Polar">
  <xsl:call-template name="StartTable"/>
    <xsl:variable name="AzType">
      <xsl:if test="AzimuthType[.='Grid']">Azimuth (grid)</xsl:if>
      <xsl:if test="AzimuthType[.='Magnetic']">Azimuth (mag)</xsl:if>
      <xsl:if test="AzimuthType[.='Local']">Azimuth (local)</xsl:if>
      <xsl:if test="AzimuthType[.='WGS84']">Azimuth (WGS84)</xsl:if>
      <xsl:if test="AzimuthType[.='Sun']">Azimuth (Sun)</xsl:if>
    </xsl:variable>

    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Polar</xsl:with-param>
      <xsl:with-param name="Val1" select="Name"/>

      <xsl:with-param name="Hdr2" select="$AzType"/>
      <xsl:with-param name="Val2">
        <xsl:call-template name="FormatAzimuth">
          <xsl:with-param name="theAzimuth" select="Polar/Azimuth"/>
          <xsl:with-param name="northLbl">N</xsl:with-param>
          <xsl:with-param name="eastLbl">E</xsl:with-param>
          <xsl:with-param name="southLbl">S</xsl:with-param>
          <xsl:with-param name="westLbl">W</xsl:with-param>
        </xsl:call-template>
      </xsl:with-param>

      <xsl:with-param name="Hdr3">H.Dist</xsl:with-param>
      <xsl:with-param name="Val3" select="format-number(Polar/HorizontalDistance * $DistConvFactor, $DecPl3, 'Standard')"/>

      <xsl:with-param name="Hdr4">V.Dist</xsl:with-param>
      <xsl:with-param name="Val4" select="format-number(Polar/VerticalDistance * $DistConvFactor, $DecPl3, 'Standard')"/>

      <xsl:with-param name="Hdr5">Code</xsl:with-param>
      <xsl:with-param name="Val5" select="Code"/>
    </xsl:call-template>
    <!-- Now output any point descriptions -->
    <xsl:call-template name="OutputDescriptions"/>

    <!-- Add second line providing the source point for the polar data -->
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr2">Source point</xsl:with-param>
      <xsl:with-param name="Val2" select="SourcePoint"/>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>
</xsl:template>



<!-- **************************************************************** -->
<!-- ************** Mean Turned Angle Details Output **************** -->
<!-- **************************************************************** -->
<xsl:template name="MeanTurnedAngle">
  <xsl:call-template name="StartTable"/>
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Point (MTA)</xsl:with-param>
      <xsl:with-param name="Val1" select="Name"/>

      <xsl:with-param name="Hdr2">HA</xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle" select="MTA/HorizontalCircle"/>
          <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
        </xsl:call-template>
      </xsl:with-param>

      <xsl:with-param name="Hdr3">VA</xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle" select="MTA/VerticalCircle"/>
          <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
        </xsl:call-template>
      </xsl:with-param>

      <xsl:with-param name="Hdr4">SD</xsl:with-param>
      <xsl:with-param name="Val4" select="format-number(MTA/EDMDistance * $DistConvFactor, $DecPl3, 'Standard')"/>

      <xsl:with-param name="Hdr5">Code</xsl:with-param>
      <xsl:with-param name="Val5" select="Code"/>
    </xsl:call-template>
    <!-- Now output any point descriptions -->
    <xsl:call-template name="OutputDescriptions"/>

    <xsl:if test="($DetailedRpt = $YesStr) and
                  (MTA/HorizontalCircleStandardError != '' or
                   MTA/HorizontalCircleStandardError != '' or 
                   MTA/EDMDistanceStandardError != '')">
      <!-- Add a second line containing the observation std error values -->
      <xsl:call-template name="OutputTableLine">
        <xsl:with-param name="Hdr1">Std Errors</xsl:with-param> <!-- No value associated with this -->

        <xsl:with-param name="Hdr2">HA</xsl:with-param>
        <xsl:with-param name="Val2">
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="MTA/HorizontalCircleStandardError"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
        </xsl:with-param>

        <xsl:with-param name="Hdr3">VA</xsl:with-param>
        <xsl:with-param name="Val3">
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="MTA/VerticalCircleStandardError"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
        </xsl:with-param>

        <xsl:with-param name="Hdr4">SD</xsl:with-param>
        <xsl:with-param name="Val4" select="format-number(MTA/EDMDistanceStandardError * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <!-- Add a third line containing the target ht and prism constant values -->
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Target height</xsl:with-param>
      <xsl:with-param name="Val1"> 
        <xsl:for-each select="key('tgtID-search', TargetID)[1]">
          <xsl:value-of select="format-number(TargetHeight * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:for-each>
      </xsl:with-param>
      
      <xsl:with-param name="Hdr2">Prism constant</xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:for-each select="key('tgtID-search', TargetID)[1]">
          <xsl:value-of select="format-number(PrismConstant * 1000, $DecPl1, 'Standard')"/><xsl:text>mm</xsl:text>
        </xsl:for-each>
      </xsl:with-param>
    </xsl:call-template>      
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ********************** GPS Deltas Output *********************** -->
<!-- **************************************************************** -->
<xsl:template name="GPSPosOrDeltas">
  <xsl:call-template name="StartTable"/>

    <xsl:variable name="XPrompt">
      <xsl:if test="ECEF">X</xsl:if>
      <xsl:if test="ECEFDeltas">ΔX</xsl:if>
    </xsl:variable>

    <xsl:variable name="YPrompt">
      <xsl:if test="ECEF">Y</xsl:if>
      <xsl:if test="ECEFDeltas">ΔY</xsl:if>
    </xsl:variable>

    <xsl:variable name="ZPrompt">
      <xsl:if test="ECEF">Z</xsl:if>
      <xsl:if test="ECEFDeltas">ΔZ</xsl:if>
    </xsl:variable>

    <xsl:variable name="deltas">
      <xsl:choose>
        <xsl:when test="ECEFDeltas and ($vectorDeltasType = 'Mark to mark')">
          <xsl:call-template name="ComputeMarkToMarkDeltas"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:element name="deltaX">
            <xsl:value-of select="ECEFDeltas/DeltaX"/>
          </xsl:element>

          <xsl:element name="deltaY">
            <xsl:value-of select="ECEFDeltas/DeltaY"/>
          </xsl:element>

          <xsl:element name="deltaZ">
            <xsl:value-of select="ECEFDeltas/DeltaZ"/>
          </xsl:element>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:variable name="XVal">
      <xsl:if test="ECEF"><xsl:value-of select="format-number(ECEF/X * $DistConvFactor, $DecPl3, 'Standard')"/></xsl:if>
      <xsl:if test="ECEFDeltas"><xsl:value-of select="format-number(msxsl:node-set($deltas)/deltaX * $DistConvFactor, $DecPl3, 'Standard')"/></xsl:if>
    </xsl:variable>

    <xsl:variable name="YVal">
      <xsl:if test="ECEF"><xsl:value-of select="format-number(ECEF/Y * $DistConvFactor, $DecPl3, 'Standard')"/></xsl:if>
      <xsl:if test="ECEFDeltas"><xsl:value-of select="format-number(msxsl:node-set($deltas)/deltaY * $DistConvFactor, $DecPl3, 'Standard')"/></xsl:if>
    </xsl:variable>

    <xsl:variable name="ZVal">
      <xsl:if test="ECEF"><xsl:value-of select="format-number(ECEF/Z * $DistConvFactor, $DecPl3, 'Standard')"/></xsl:if>
      <xsl:if test="ECEFDeltas"><xsl:value-of select="format-number(msxsl:node-set($deltas)/deltaZ * $DistConvFactor, $DecPl3, 'Standard')"/></xsl:if>
    </xsl:variable>

    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Point</xsl:with-param>
      <xsl:with-param name="Val1" select="Name"/>

      <xsl:with-param name="Hdr2" select="$XPrompt"/>
      <xsl:with-param name="Val2" select="$XVal"/>

      <xsl:with-param name="Hdr3" select="$YPrompt"/>
      <xsl:with-param name="Val3" select="$YVal"/>

      <xsl:with-param name="Hdr4" select="$ZPrompt"/>
      <xsl:with-param name="Val4" select="$ZVal"/>

      <xsl:with-param name="Hdr5">Code</xsl:with-param>
      <xsl:with-param name="Val5" select="Code"/>
    </xsl:call-template>

    <!-- Now output any point descriptions -->
    <xsl:call-template name="OutputDescriptions"/>

    <!-- Output the survey method details -->
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr2">Method</xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:call-template name="SurveyMethodText"/>
      </xsl:with-param>

      <xsl:with-param name="Hdr3">Type</xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:call-template name="PointCreationTypeText"/>
      </xsl:with-param>

      <xsl:with-param name="Hdr4">Search class</xsl:with-param>
      <xsl:with-param name="Val4">
        <xsl:call-template name="ClassificationText"/>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:variable name="antennaDetails">
      <xsl:for-each select="key('antID-search', AntennaID)[1]">
        <xsl:copy-of select="*"/>
      </xsl:for-each>
    </xsl:variable>

    <!-- Add second line with antenna details (including tilt distance if available) and hz and vt precision values -->
    <xsl:choose>
      <xsl:when test="DeviceAxisOrientation">
        <!-- Compute the tilt distance -->
        <xsl:variable name="tiltDist">
          <xsl:call-template name="ComputeTiltDistance">
            <xsl:with-param name="reducedAntennaTargetHt" select="msxsl:node-set($antennaDetails)/ReducedHeight"/>
          </xsl:call-template>
        </xsl:variable>

        <xsl:call-template name="OutputTableLine">
          <xsl:with-param name="Hdr1">Antenna height</xsl:with-param>
          <xsl:with-param name="Val1" select="format-number(msxsl:node-set($antennaDetails)/MeasuredHeight * $DistConvFactor, $DecPl3, 'Standard')"/>

          <xsl:with-param name="Hdr2">Type</xsl:with-param>
          <xsl:with-param name="Val2">
            <xsl:choose>
              <xsl:when test="msxsl:node-set($antennaDetails)/MeasurementType = 'Corrected'">Corrected</xsl:when>
              <xsl:otherwise>Uncorrected</xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>

          <xsl:with-param name="Hdr3">Tilt distance</xsl:with-param>
          <xsl:with-param name="Val3">
            <xsl:value-of select="format-number($tiltDist * $DistConvFactor, $DecPl3, 'Standard')"/>
            <xsl:if test="Method = 'GpsTiltCompensatedPoint'"> (applied)</xsl:if>
          </xsl:with-param>

          <xsl:with-param name="Hdr4">
            <xsl:text>Hz Prec</xsl:text>
            <xsl:value-of select="$hzPrecisionPrompt"/>
          </xsl:with-param>
          <xsl:with-param name="Val4" select="format-number(Precision/Horizontal * $hzPrecisionScalar * $DistConvFactor, $DecPl3, 'Standard')"/>

          <xsl:with-param name="Hdr5">
            <xsl:text>Vt Prec</xsl:text>
            <xsl:value-of select="$vtPrecisionPrompt"/>
          </xsl:with-param>
          <xsl:with-param name="Val5" select="format-number(Precision/Vertical * $vtPrecisionScalar * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="OutputTableLine">
          <xsl:with-param name="Hdr1">Antenna height</xsl:with-param>
          <xsl:with-param name="Val1" select="format-number(msxsl:node-set($antennaDetails)/MeasuredHeight * $DistConvFactor, $DecPl3, 'Standard')"/>

          <xsl:with-param name="Hdr2">Type</xsl:with-param>
          <xsl:with-param name="Val2">
            <xsl:choose>
              <xsl:when test="msxsl:node-set($antennaDetails)/MeasurementType = 'Corrected'">Corrected</xsl:when>
              <xsl:otherwise>Uncorrected</xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>

          <xsl:with-param name="Hdr3">
            <xsl:text>Hz Prec</xsl:text>
            <xsl:value-of select="$hzPrecisionPrompt"/>
          </xsl:with-param>
          <xsl:with-param name="Val3" select="format-number(Precision/Horizontal * $hzPrecisionScalar * $DistConvFactor, $DecPl3, 'Standard')"/>

          <xsl:with-param name="Hdr4">
            <xsl:text>Vt Prec</xsl:text>
            <xsl:value-of select="$vtPrecisionPrompt"/>
          </xsl:with-param>
          <xsl:with-param name="Val4" select="format-number(Precision/Vertical * $vtPrecisionScalar * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>

    <!-- If the point has an EchoSounding element add a line with the deth details and resultant elevation -->
    <xsl:if test="EchoSounding">
      <xsl:call-template name="OutputTableLine">
        <xsl:with-param name="Hdr1">Depth</xsl:with-param>
        <xsl:with-param name="Val1" select="format-number(EchoSounding/Depth * $DistConvFactor, $DecPl3, 'Standard')"/>

        <xsl:with-param name="Hdr2">Latency</xsl:with-param>
        <xsl:with-param name="Val2">
          <xsl:value-of select="format-number(preceding-sibling::ContinuousParametersRecord[1]/EchoSounderLatency, $DecPl1, 'Standard')"/>
          <xsl:text> Seconds</xsl:text>
        </xsl:with-param>

        <xsl:with-param name="Hdr3">Elevation</xsl:with-param>
        <xsl:with-param name="Val3" select="format-number((ComputedGrid/Elevation - EchoSounding/Depth) * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <!-- If writing a detailed report and there are QC details available - write them out -->
    <xsl:if test="$DetailedRpt = $YesStr">
      <xsl:if test="QualityControl1">
        <xsl:for-each select="QualityControl1">
          <xsl:choose>
            <xsl:when test="not(RMS)">   <!-- This is data from a later receiver that doesn't output the RMS value -->
              <xsl:call-template name="OutputTableLine">
                <xsl:with-param name="Hdr1">QC 1</xsl:with-param>

                <xsl:with-param name="Hdr2">PDOP</xsl:with-param>
                <xsl:with-param name="Val2" select="format-number(PDOP, $DecPl1, 'Standard')"/>

                <xsl:with-param name="Hdr3">GDOP</xsl:with-param>
                <xsl:with-param name="Val3" select="format-number(GDOP, $DecPl1, 'Standard')"/>

                <xsl:with-param name="Hdr4">HDOP</xsl:with-param>
                <xsl:with-param name="Val4" select="format-number(HDOP, $DecPl1, 'Standard')"/>

                <xsl:with-param name="Hdr5">VDOP</xsl:with-param>
                <xsl:with-param name="Val5" select="format-number(VDOP, $DecPl1, 'Standard')"/>
              </xsl:call-template>

              <xsl:choose>
                <xsl:when test="$includeSatelliteDetails = $YesStr">
                  <xsl:call-template name="OutputTableLine">
                    <xsl:with-param name="Hdr2">Base data age</xsl:with-param>
                    <xsl:with-param name="Val2" select="RTCMAge"/>

                    <xsl:with-param name="Hdr3">Positions used</xsl:with-param>
                    <xsl:with-param name="Val3" select="NumberOfPositionsUsed"/>
                  </xsl:call-template>

                  <xsl:call-template name="OutputSatelliteDetails"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:call-template name="OutputTableLine">
                    <xsl:with-param name="Hdr2">Base data age</xsl:with-param>
                    <xsl:with-param name="Val2" select="RTCMAge"/>

                    <xsl:with-param name="Hdr3">Satellites</xsl:with-param>
                    <xsl:with-param name="Val3" select="NumberOfSatellites"/>

                    <xsl:with-param name="Hdr4">Positions used</xsl:with-param>
                    <xsl:with-param name="Val4" select="NumberOfPositionsUsed"/>
                  </xsl:call-template>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:when>

            <xsl:otherwise>
              <xsl:call-template name="OutputTableLine">
                <xsl:with-param name="Hdr1">QC 1</xsl:with-param>

                <xsl:with-param name="Hdr2">Satellites</xsl:with-param>
                <xsl:with-param name="Val2" select="NumberOfSatellites"/>

                <xsl:with-param name="Hdr3">PDOP</xsl:with-param>
                <xsl:with-param name="Val3" select="format-number(PDOP, $DecPl1, 'Standard')"/>

                <xsl:with-param name="Hdr4">HDOP</xsl:with-param>
                <xsl:with-param name="Val4" select="format-number(HDOP, $DecPl1, 'Standard')"/>

                <xsl:with-param name="Hdr5">VDOP</xsl:with-param>
                <xsl:with-param name="Val5" select="format-number(VDOP, $DecPl1, 'Standard')"/>
              </xsl:call-template>

              <xsl:choose>
                <xsl:when test="RTCMAge">   <!-- Include the Base data age if present -->
                  <xsl:call-template name="OutputTableLine">
                    <xsl:with-param name="Hdr2">Base data age</xsl:with-param>
                    <xsl:with-param name="Val2" select="RTCMAge"/>

                    <xsl:with-param name="Hdr3">RMS</xsl:with-param>
                    <xsl:with-param name="Val3" select="format-number(RMS, $DecPl3, 'Standard')"/>

                    <xsl:with-param name="Hdr4">Positions used</xsl:with-param>
                    <xsl:with-param name="Val4" select="NumberOfPositionsUsed"/>
                  </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:call-template name="OutputTableLine">
                    <xsl:with-param name="Hdr2">RMS</xsl:with-param>
                    <xsl:with-param name="Val2" select="format-number(RMS, $DecPl3, 'Standard')"/>

                    <xsl:with-param name="Hdr3">Positions used</xsl:with-param>
                    <xsl:with-param name="Val3" select="NumberOfPositionsUsed"/>
                  </xsl:call-template>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>
      </xsl:if>

      <xsl:if test="QualityControl2">
        <xsl:call-template name="OutputTableLine">
          <xsl:with-param name="Hdr1">QC 2</xsl:with-param>

          <xsl:with-param name="Hdr2">VCV xx (m&#0178;)</xsl:with-param>
          <xsl:with-param name="Val2" select="format-number(QualityControl2/VCVxx, $DecPl6, 'Standard')"/>

          <xsl:with-param name="Hdr3">VCV xy (m&#0178;)</xsl:with-param>
          <xsl:with-param name="Val3" select="format-number(QualityControl2/VCVxy, $DecPl6, 'Standard')"/>

          <xsl:with-param name="Hdr4">VCV xz (m&#0178;)</xsl:with-param>
          <xsl:with-param name="Val4" select="format-number(QualityControl2/VCVxz, $DecPl6, 'Standard')"/>
        </xsl:call-template>

        <xsl:call-template name="OutputTableLine">
          <xsl:with-param name="Hdr3">VCV yy (m&#0178;)</xsl:with-param>
          <xsl:with-param name="Val3" select="format-number(QualityControl2/VCVyy, $DecPl6, 'Standard')"/>

          <xsl:with-param name="Hdr4">VCV yz (m&#0178;)</xsl:with-param>
          <xsl:with-param name="Val4" select="format-number(QualityControl2/VCVyz, $DecPl6, 'Standard')"/>
        </xsl:call-template>

        <xsl:call-template name="OutputTableLine">
          <xsl:with-param name="Hdr4">VCV zz (m&#0178;)</xsl:with-param>
          <xsl:with-param name="Val4" select="format-number(QualityControl2/VCVzz, $DecPl6, 'Standard')"/>
        </xsl:call-template>
      </xsl:if>

      <xsl:if test="QualityControl3">
        <xsl:call-template name="OutputTableLine">
          <xsl:with-param name="Hdr1">QC 3</xsl:with-param>

          <xsl:with-param name="Hdr2">σ North</xsl:with-param>
          <xsl:with-param name="Val2" select="format-number(QualityControl3/SigmaNorth, $DecPl3, 'Standard')"/>

          <xsl:with-param name="Hdr3">σ East</xsl:with-param>
          <xsl:with-param name="Val3" select="format-number(QualityControl3/SigmaEast, $DecPl3, 'Standard')"/>

          <xsl:with-param name="Hdr4">σ Elevation</xsl:with-param>
          <xsl:with-param name="Val4" select="format-number(QualityControl3/SigmaElevation, $DecPl3, 'Standard')"/>

          <xsl:with-param name="Hdr5">Covariance</xsl:with-param>
          <xsl:with-param name="Val5" select="format-number(QualityControl3/CovarianceNorthEast, $DecPl3, 'Standard')"/>
        </xsl:call-template>

        <xsl:call-template name="OutputTableLine">
          <xsl:with-param name="Hdr2">Semi-major axis</xsl:with-param>
          <xsl:with-param name="Val2" select="format-number(QualityControl3/SemiMajorAxis, $DecPl3, 'Standard')"/>

          <xsl:with-param name="Hdr3">Semi-minor axis</xsl:with-param>
          <xsl:with-param name="Val3" select="format-number(QualityControl3/SemiMinorAxis, $DecPl3, 'Standard')"/>

          <xsl:with-param name="Hdr4">Orientation</xsl:with-param>
          <xsl:with-param name="Val4">
            <xsl:call-template name="FormatAngle">
              <xsl:with-param name="theAngle" select="QualityControl2/Orientation"/>
              <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
            </xsl:call-template>
          </xsl:with-param>

          <xsl:with-param name="Hdr5">Unit variance</xsl:with-param>
          <xsl:with-param name="Val5" select="format-number(QualityControl3/UnitVariance, $DecPl3, 'Standard')"/>
        </xsl:call-template>
      </xsl:if>
    </xsl:if>
    
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************** RTX Position Output *********************** -->
<!-- **************************************************************** -->
<xsl:template name="RTXPos">
  <xsl:call-template name="StartTable"/>

    <xsl:variable name="XPrompt">RTX X</xsl:variable>

    <xsl:variable name="YPrompt">RTX Y</xsl:variable>

    <xsl:variable name="ZPrompt">RTX Z</xsl:variable>

    <xsl:variable name="XVal" select="format-number(RTXECEF/X * $DistConvFactor, $DecPl3, 'Standard')"/>

    <xsl:variable name="YVal" select="format-number(RTXECEF/Y * $DistConvFactor, $DecPl3, 'Standard')"/>

    <xsl:variable name="ZVal" select="format-number(RTXECEF/Z * $DistConvFactor, $DecPl3, 'Standard')"/>

    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Point</xsl:with-param>
      <xsl:with-param name="Val1" select="Name"/>

      <xsl:with-param name="Hdr2" select="$XPrompt"/>
      <xsl:with-param name="Val2" select="$XVal"/>

      <xsl:with-param name="Hdr3" select="$YPrompt"/>
      <xsl:with-param name="Val3" select="$YVal"/>

      <xsl:with-param name="Hdr4" select="$ZPrompt"/>
      <xsl:with-param name="Val4" select="$ZVal"/>

      <xsl:with-param name="Hdr5">Code</xsl:with-param>
      <xsl:with-param name="Val5" select="Code"/>
    </xsl:call-template>

    <!-- Now output any point descriptions -->
    <xsl:call-template name="OutputDescriptions"/>

    <!-- Output the survey method details -->
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr2">Method</xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:call-template name="SurveyMethodText"/>
      </xsl:with-param>

      <xsl:with-param name="Hdr3">Type</xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:call-template name="PointCreationTypeText"/>
      </xsl:with-param>

      <xsl:with-param name="Hdr4">Search class</xsl:with-param>
      <xsl:with-param name="Val4">
        <xsl:call-template name="ClassificationText"/>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:variable name="antennaDetails">
      <xsl:for-each select="key('antID-search', AntennaID)[1]">
        <xsl:copy-of select="*"/>
      </xsl:for-each>
    </xsl:variable>

    <!-- Add second line with antenna details (including tilt distance if available) and hz and vt precision values -->
    <xsl:choose>
      <xsl:when test="DeviceAxisOrientation">
        <!-- Compute the tilt distance -->
        <xsl:variable name="tiltDist">
          <xsl:call-template name="ComputeTiltDistance">
            <xsl:with-param name="reducedAntennaTargetHt" select="msxsl:node-set($antennaDetails)/ReducedHeight"/>
          </xsl:call-template>
        </xsl:variable>

        <xsl:call-template name="OutputTableLine">
          <xsl:with-param name="Hdr1">Antenna height</xsl:with-param>
          <xsl:with-param name="Val1" select="format-number(msxsl:node-set($antennaDetails)/MeasuredHeight * $DistConvFactor, $DecPl3, 'Standard')"/>

          <xsl:with-param name="Hdr2">Type</xsl:with-param>
          <xsl:with-param name="Val2">
            <xsl:choose>
              <xsl:when test="msxsl:node-set($antennaDetails)/MeasurementType = 'Corrected'">Corrected</xsl:when>
              <xsl:otherwise>Uncorrected</xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>

          <xsl:with-param name="Hdr3">Tilt distance</xsl:with-param>
          <xsl:with-param name="Val3">
            <xsl:value-of select="format-number($tiltDist * $DistConvFactor, $DecPl3, 'Standard')"/>
            <xsl:if test="Method = 'GpsTiltCompensatedPoint'"> (applied)</xsl:if>
          </xsl:with-param>

          <xsl:with-param name="Hdr4">
            <xsl:text>Hz Prec</xsl:text>
            <xsl:value-of select="$hzPrecisionPrompt"/>
          </xsl:with-param>
          <xsl:with-param name="Val4" select="format-number(Precision/Horizontal * $hzPrecisionScalar * $DistConvFactor, $DecPl3, 'Standard')"/>

          <xsl:with-param name="Hdr5">
            <xsl:text>Vt Prec</xsl:text>
            <xsl:value-of select="$vtPrecisionPrompt"/>
          </xsl:with-param>
          <xsl:with-param name="Val5" select="format-number(Precision/Vertical * $vtPrecisionScalar * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="OutputTableLine">
          <xsl:with-param name="Hdr1">Antenna height</xsl:with-param>
          <xsl:with-param name="Val1" select="format-number(msxsl:node-set($antennaDetails)/MeasuredHeight * $DistConvFactor, $DecPl3, 'Standard')"/>

          <xsl:with-param name="Hdr2">Type</xsl:with-param>
          <xsl:with-param name="Val2">
            <xsl:choose>
              <xsl:when test="msxsl:node-set($antennaDetails)/MeasurementType = 'Corrected'">Corrected</xsl:when>
              <xsl:otherwise>Uncorrected</xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>

          <xsl:with-param name="Hdr3">
            <xsl:text>Hz Prec</xsl:text>
            <xsl:value-of select="$hzPrecisionPrompt"/>
          </xsl:with-param>
          <xsl:with-param name="Val3" select="format-number(Precision/Horizontal * $hzPrecisionScalar * $DistConvFactor, $DecPl3, 'Standard')"/>

          <xsl:with-param name="Hdr4">
            <xsl:text>Vt Prec</xsl:text>
            <xsl:value-of select="$vtPrecisionPrompt"/>
          </xsl:with-param>
          <xsl:with-param name="Val4" select="format-number(Precision/Vertical * $vtPrecisionScalar * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>

    <!-- If the point has an EchoSounding element add a line with the deth details and resultant elevation -->
    <xsl:if test="EchoSounding">
      <xsl:call-template name="OutputTableLine">
        <xsl:with-param name="Hdr1">Depth</xsl:with-param>
        <xsl:with-param name="Val1" select="format-number(EchoSounding/Depth * $DistConvFactor, $DecPl3, 'Standard')"/>

        <xsl:with-param name="Hdr2">Latency</xsl:with-param>
        <xsl:with-param name="Val2">
          <xsl:value-of select="format-number(preceding-sibling::ContinuousParametersRecord[1]/EchoSounderLatency, $DecPl1, 'Standard')"/>
          <xsl:text> Seconds</xsl:text>
        </xsl:with-param>

        <xsl:with-param name="Hdr3">Elevation</xsl:with-param>
        <xsl:with-param name="Val3" select="format-number((ComputedGrid/Elevation - EchoSounding/Depth) * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <!-- If writing a detailed report and there are QC details available - write them out -->
    <xsl:if test="$DetailedRpt = $YesStr">
      <xsl:if test="QualityControl1">
        <xsl:for-each select="QualityControl1">
          <xsl:choose>
            <xsl:when test="not(RMS)">   <!-- This is data from a later receiver that doesn't output the RMS value -->
              <xsl:call-template name="OutputTableLine">
                <xsl:with-param name="Hdr1">QC 1</xsl:with-param>

                <xsl:with-param name="Hdr2">PDOP</xsl:with-param>
                <xsl:with-param name="Val2" select="format-number(PDOP, $DecPl1, 'Standard')"/>

                <xsl:with-param name="Hdr3">GDOP</xsl:with-param>
                <xsl:with-param name="Val3" select="format-number(GDOP, $DecPl1, 'Standard')"/>

                <xsl:with-param name="Hdr4">HDOP</xsl:with-param>
                <xsl:with-param name="Val4" select="format-number(HDOP, $DecPl1, 'Standard')"/>

                <xsl:with-param name="Hdr5">VDOP</xsl:with-param>
                <xsl:with-param name="Val5" select="format-number(VDOP, $DecPl1, 'Standard')"/>
              </xsl:call-template>

              <xsl:choose>
                <xsl:when test="$includeSatelliteDetails = $YesStr">
                  <xsl:call-template name="OutputTableLine">
                    <xsl:with-param name="Hdr2">Base data age</xsl:with-param>
                    <xsl:with-param name="Val2" select="RTCMAge"/>

                    <xsl:with-param name="Hdr3">Positions used</xsl:with-param>
                    <xsl:with-param name="Val3" select="NumberOfPositionsUsed"/>
                  </xsl:call-template>
                  
                  <xsl:call-template name="OutputSatelliteDetails"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:call-template name="OutputTableLine">
                    <xsl:with-param name="Hdr2">Base data age</xsl:with-param>
                    <xsl:with-param name="Val2" select="RTCMAge"/>

                    <xsl:with-param name="Hdr3">Satellites</xsl:with-param>
                    <xsl:with-param name="Val3" select="NumberOfSatellites"/>

                    <xsl:with-param name="Hdr4">Positions used</xsl:with-param>
                    <xsl:with-param name="Val4" select="NumberOfPositionsUsed"/>
                  </xsl:call-template>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:when>

            <xsl:otherwise>
              <xsl:call-template name="OutputTableLine">
                <xsl:with-param name="Hdr1">QC 1</xsl:with-param>

                <xsl:with-param name="Hdr2">Satellites</xsl:with-param>
                <xsl:with-param name="Val2" select="NumberOfSatellites"/>

                <xsl:with-param name="Hdr3">PDOP</xsl:with-param>
                <xsl:with-param name="Val3" select="format-number(PDOP, $DecPl1, 'Standard')"/>

                <xsl:with-param name="Hdr4">HDOP</xsl:with-param>
                <xsl:with-param name="Val4" select="format-number(HDOP, $DecPl1, 'Standard')"/>

                <xsl:with-param name="Hdr5">VDOP</xsl:with-param>
                <xsl:with-param name="Val5" select="format-number(VDOP, $DecPl1, 'Standard')"/>
              </xsl:call-template>

              <xsl:choose>
                <xsl:when test="RTCMAge">   <!-- Include the Base data age if present -->
                  <xsl:call-template name="OutputTableLine">
                    <xsl:with-param name="Hdr2">Base data age</xsl:with-param>
                    <xsl:with-param name="Val2" select="RTCMAge"/>

                    <xsl:with-param name="Hdr3">RMS</xsl:with-param>
                    <xsl:with-param name="Val3" select="format-number(RMS, $DecPl3, 'Standard')"/>

                    <xsl:with-param name="Hdr4">Positions used</xsl:with-param>
                    <xsl:with-param name="Val4" select="NumberOfPositionsUsed"/>
                  </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:call-template name="OutputTableLine">
                    <xsl:with-param name="Hdr2">RMS</xsl:with-param>
                    <xsl:with-param name="Val2" select="format-number(RMS, $DecPl3, 'Standard')"/>

                    <xsl:with-param name="Hdr3">Positions used</xsl:with-param>
                    <xsl:with-param name="Val3" select="NumberOfPositionsUsed"/>
                  </xsl:call-template>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>
      </xsl:if>

      <xsl:if test="QualityControl2">
        <xsl:call-template name="OutputTableLine">
          <xsl:with-param name="Hdr1">QC 2</xsl:with-param>

          <xsl:with-param name="Hdr2">VCV xx (m&#0178;)</xsl:with-param>
          <xsl:with-param name="Val2" select="format-number(QualityControl2/VCVxx, $DecPl6, 'Standard')"/>

          <xsl:with-param name="Hdr3">VCV xy (m&#0178;)</xsl:with-param>
          <xsl:with-param name="Val3" select="format-number(QualityControl2/VCVxy, $DecPl6, 'Standard')"/>

          <xsl:with-param name="Hdr4">VCV xz (m&#0178;)</xsl:with-param>
          <xsl:with-param name="Val4" select="format-number(QualityControl2/VCVxz, $DecPl6, 'Standard')"/>
        </xsl:call-template>

        <xsl:call-template name="OutputTableLine">
          <xsl:with-param name="Hdr3">VCV yy (m&#0178;)</xsl:with-param>
          <xsl:with-param name="Val3" select="format-number(QualityControl2/VCVyy, $DecPl6, 'Standard')"/>

          <xsl:with-param name="Hdr4">VCV yz (m&#0178;)</xsl:with-param>
          <xsl:with-param name="Val4" select="format-number(QualityControl2/VCVyz, $DecPl6, 'Standard')"/>
        </xsl:call-template>

        <xsl:call-template name="OutputTableLine">
          <xsl:with-param name="Hdr4">VCV zz (m&#0178;)</xsl:with-param>
          <xsl:with-param name="Val4" select="format-number(QualityControl2/VCVzz, $DecPl6, 'Standard')"/>
        </xsl:call-template>
      </xsl:if>

      <xsl:if test="QualityControl3">
        <xsl:call-template name="OutputTableLine">
          <xsl:with-param name="Hdr1">QC 3</xsl:with-param>

          <xsl:with-param name="Hdr2">σ North</xsl:with-param>
          <xsl:with-param name="Val2" select="format-number(QualityControl3/SigmaNorth, $DecPl3, 'Standard')"/>

          <xsl:with-param name="Hdr3">σ East</xsl:with-param>
          <xsl:with-param name="Val3" select="format-number(QualityControl3/SigmaEast, $DecPl3, 'Standard')"/>

          <xsl:with-param name="Hdr4">σ Elevation</xsl:with-param>
          <xsl:with-param name="Val4" select="format-number(QualityControl3/SigmaElevation, $DecPl3, 'Standard')"/>

          <xsl:with-param name="Hdr5">Covariance</xsl:with-param>
          <xsl:with-param name="Val5" select="format-number(QualityControl3/CovarianceNorthEast, $DecPl3, 'Standard')"/>
        </xsl:call-template>

        <xsl:call-template name="OutputTableLine">
          <xsl:with-param name="Hdr2">Semi-major axis</xsl:with-param>
          <xsl:with-param name="Val2" select="format-number(QualityControl3/SemiMajorAxis, $DecPl3, 'Standard')"/>

          <xsl:with-param name="Hdr3">Semi-minor axis</xsl:with-param>
          <xsl:with-param name="Val3" select="format-number(QualityControl3/SemiMinorAxis, $DecPl3, 'Standard')"/>

          <xsl:with-param name="Hdr4">Orientation</xsl:with-param>
          <xsl:with-param name="Val4">
            <xsl:call-template name="FormatAngle">
              <xsl:with-param name="theAngle" select="QualityControl2/Orientation"/>
              <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
            </xsl:call-template>
          </xsl:with-param>

          <xsl:with-param name="Hdr5">Unit variance</xsl:with-param>
          <xsl:with-param name="Val5" select="format-number(QualityControl3/UnitVariance, $DecPl3, 'Standard')"/>
        </xsl:call-template>
      </xsl:if>
    </xsl:if>

  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- *********** Return appropriate text for SurveyMethod *********** -->
<!-- **************************************************************** -->
<xsl:template name="SurveyMethodText">

  <xsl:choose>
    <xsl:when test="SurveyMethod = 'KeyedIn'">Keyed in</xsl:when>
    <xsl:when test="SurveyMethod = 'Autonomous'">Autonomous</xsl:when>
    <xsl:when test="SurveyMethod = 'Float'">Float</xsl:when>
    <xsl:when test="SurveyMethod = 'Fix'">Fixed</xsl:when>
    <xsl:when test="SurveyMethod = 'Copied'">Copied</xsl:when>
    <xsl:when test="SurveyMethod = 'Code'">Code</xsl:when>
    <xsl:when test="SurveyMethod = 'WAAS'">SBAS</xsl:when>
    <xsl:when test="SurveyMethod = 'Inertial'">Inertial</xsl:when>
    <xsl:when test="SurveyMethod = 'NetworkFloat'">Float</xsl:when>
    <xsl:when test="SurveyMethod = 'NetworkFix'">Fixed</xsl:when>
    <xsl:when test="SurveyMethod = 'OmniSTARHP'">OmniSTAR HP</xsl:when>
    <xsl:when test="SurveyMethod = 'OmniSTARVBS'">OmniSTAR VBS</xsl:when>
    <xsl:when test="SurveyMethod = 'xFillFloat'">xFill Float</xsl:when>
    <xsl:when test="SurveyMethod = 'xFillFix'">xFill Fixed</xsl:when>
    <xsl:when test="SurveyMethod = 'RTK'">RTK</xsl:when>
    <xsl:when test="SurveyMethod = 'NetworkRTK'">Network RTK</xsl:when>
    <xsl:when test="SurveyMethod = 'RTKxFill'">RTK xFill</xsl:when>
    <xsl:when test="SurveyMethod = 'RTX'">RTX</xsl:when>
    <xsl:when test="SurveyMethod = 'RTXFast'">RTX Fast</xsl:when>
    <xsl:when test="SurveyMethod = 'xFillX'">xFill-RTX</xsl:when>
    <xsl:otherwise>Keyed in</xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************* Output the detailed satellite lines ************** -->
<!-- **************************************************************** -->
<xsl:template name="OutputSatelliteDetails">

  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="StartTable"/>  <!-- Will cause a horizontal line to be drawn in table -->

  <xsl:if test="count(SVList/SV[@System = 'GPS']) != 0">
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">
        <xsl:text>Satellites</xsl:text>
        <xsl:text>: (GPS)</xsl:text>
      </xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:for-each select="SVList/SV[@System = 'GPS']">
          <xsl:value-of select="@Number"/>
          <xsl:if test="position() != last()">, </xsl:if>
        </xsl:for-each>
      </xsl:with-param>
      <xsl:with-param name="spanExtraFields">true</xsl:with-param>
    </xsl:call-template>
  </xsl:if>

  <xsl:if test="count(SVList/SV[@System = 'GLONASS']) != 0">
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">
        <xsl:text>Satellites</xsl:text>
        <xsl:text>: (GLONASS)</xsl:text>
      </xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:for-each select="SVList/SV[@System = 'GLONASS']">
          <xsl:value-of select="@Number"/>
          <xsl:if test="position() != last()">, </xsl:if>
        </xsl:for-each>
      </xsl:with-param>
      <xsl:with-param name="spanExtraFields">true</xsl:with-param>
    </xsl:call-template>
  </xsl:if>

  <xsl:if test="count(SVList/SV[@System = 'GALILEO']) != 0">
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">
        <xsl:text>Satellites</xsl:text>
        <xsl:text>: (GALILEO)</xsl:text>
      </xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:for-each select="SVList/SV[@System = 'GALILEO']">
          <xsl:value-of select="@Number"/>
          <xsl:if test="position() != last()">, </xsl:if>
        </xsl:for-each>
      </xsl:with-param>
      <xsl:with-param name="spanExtraFields">true</xsl:with-param>
    </xsl:call-template>
  </xsl:if>

  <xsl:if test="count(SVList/SV[@System = 'BEIDOU']) != 0">
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">
        <xsl:text>Satellites</xsl:text>
        <xsl:text>: (BEIDOU)</xsl:text>
      </xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:for-each select="SVList/SV[@System = 'BEIDOU']">
          <xsl:value-of select="@Number"/>
          <xsl:if test="position() != last()">, </xsl:if>
        </xsl:for-each>
      </xsl:with-param>
      <xsl:with-param name="spanExtraFields">true</xsl:with-param>
    </xsl:call-template>
  </xsl:if>

  <xsl:if test="count(SVList/SV[@System = 'QZSS']) != 0">
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">
        <xsl:text>Satellites</xsl:text>
        <xsl:text>: (QZSS)</xsl:text>
      </xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:for-each select="SVList/SV[@System = 'QZSS']">
          <xsl:value-of select="@Number"/>
          <xsl:if test="position() != last()">, </xsl:if>
        </xsl:for-each>
      </xsl:with-param>
      <xsl:with-param name="spanExtraFields">true</xsl:with-param>
    </xsl:call-template>
  </xsl:if>

  <xsl:if test="count(SVList/SV[@System = 'IRNSS']) != 0">
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">
        <xsl:text>Satellites</xsl:text>
        <xsl:text>: (IRNSS)</xsl:text>
      </xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:for-each select="SVList/SV[@System = 'IRNSS']">
          <xsl:value-of select="@Number"/>
          <xsl:if test="position() != last()">, </xsl:if>
        </xsl:for-each>
      </xsl:with-param>
      <xsl:with-param name="spanExtraFields">true</xsl:with-param>
    </xsl:call-template>
  </xsl:if>

  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="StartTable"/>  <!-- Will cause a horizontal line to be drawn in table -->
</xsl:template>


<!-- **************************************************************** -->
<!-- *** Return appropriate text for Method (point creation type) *** -->
<!-- **************************************************************** -->
<xsl:template name="PointCreationTypeText">

  <xsl:choose>
    <xsl:when test="Method = 'Unknown'">Unknown</xsl:when>
    <xsl:when test="Method = 'Coordinates'">Coordinates</xsl:when>
    <xsl:when test="Method = 'FourPointIntersection'">Four point intersection</xsl:when>
    <xsl:when test="Method = 'FromABaseline'">From a baseline</xsl:when>
    <xsl:when test="Method = 'BearingAndDistanceFromAPoint'">Brng-dist from a point</xsl:when>
    <xsl:when test="Method = 'BearingDistancePointIntersection'">Brng-dist intersect</xsl:when>
    <xsl:when test="Method = 'BearingVerticalPlaneIntersection'">Vertical plane and angle</xsl:when>
    <xsl:when test="Method = 'BearingDistanceIntersection'">Brng-dist intersect</xsl:when>
    <xsl:when test="Method = 'BearingBearingIntersection'">Brng-brng intersect</xsl:when>
    <xsl:when test="Method = 'DistanceDistanceIntersection'">Dist-dist intersect</xsl:when>
    <xsl:when test="Method = 'ProjectPointToLine'">Project point to line</xsl:when>
    <xsl:when test="Method = 'ProjectPointToArc'">Project point to arc</xsl:when>
    <xsl:when test="Method = 'GpsStaticObservation'">Topo point</xsl:when>
    <xsl:when test="Method = 'GpsFastStaticObservation'">FastStatic point</xsl:when>
    <xsl:when test="Method = 'GpsRTKControlPoint'">Observed control point</xsl:when>
    <xsl:when test="Method = 'GpsRTKQuickMark'">Rapid point</xsl:when>
    <xsl:when test="Method = 'Gps2PointOffset'">2 point offset</xsl:when>
    <xsl:when test="Method = 'Gps4PointIntersection'">4 point intersection</xsl:when>
    <xsl:when test="Method = 'GpsCheckShot'">Check shot</xsl:when>
    <xsl:when test="Method = 'GpsStakeOutObservation'">Stake out point</xsl:when>
    <xsl:when test="Method = 'GpsCalibrationPoint'">Calibration point</xsl:when>
    <xsl:when test="Method = 'GpsSingleAutonomousPoint'">Autonomous</xsl:when>
    <xsl:when test="Method = 'GpsAverageAutonomousPoint'">Averaged autonomous point</xsl:when>
    <xsl:when test="Method = 'GpsTiltCompensatedPoint'">Compensated point</xsl:when>
    <xsl:when test="starts-with(Method, 'GpsContinuous')">Continuous</xsl:when>
    <xsl:when test="Method = 'CogoCalculated'">Cogo point (computed)</xsl:when>
    <xsl:when test="Method = 'AlignmentOffsetCalculated'">Alignment offset (computed)</xsl:when>
    <xsl:when test="Method = 'AzimuthOnly'">Azimuth only</xsl:when>
    <xsl:when test="Method = 'AngleOnly'">Angles only</xsl:when>
    <xsl:when test="Method = 'HorizontalAngleOnly'">H. Angle only</xsl:when>
    <xsl:when test="Method = 'DirectReading'">Angles and distance</xsl:when>
    <xsl:when test="Method = 'AverageMeasurements'">Averaged observations</xsl:when>
    <xsl:when test="Method = 'AngleOffset'">Angle offset</xsl:when>
    <xsl:when test="Method = 'HorizontalAngleOffset'">H. Angle offset</xsl:when>
    <xsl:when test="Method = 'VerticalAngleOffset'">V. Angle offset</xsl:when>
    <xsl:when test="Method = 'DistanceOffset'">Distance offset</xsl:when>
    <xsl:when test="Method = 'DualPrismOffset'">Dual-prism offset</xsl:when>
    <xsl:when test="Method = 'CircularObject'">Circular object</xsl:when>
    <xsl:when test="Method = 'RemoteObject'">Remote object</xsl:when>
    <xsl:when test="Method = 'RemoteObjectBasePoint'">Remote object (base point)</xsl:when>
    <xsl:when test="Method = 'MeanTurnedAngle'">Mean turned angle</xsl:when>
    <xsl:when test="Method = 'Resection'">Resection</xsl:when>
    <xsl:when test="Method = 'LaserOffset'">Laser point</xsl:when>
    <xsl:when test="Method = 'CopiedPoint'">Copied point</xsl:when>
    <xsl:when test="Method = 'FromBase'">Base point</xsl:when>
    <xsl:when test="Method = 'TraverseAdjusted'">Traverse adjusted</xsl:when>
    <xsl:when test="Method = 'TurnedAngleAndDistance'">Turned angle and distance</xsl:when>
    <xsl:when test="Method = 'ReflineSetup'">Refline</xsl:when>
    <xsl:otherwise>Unknown</xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ********** Return appropriate text for Classification ********** -->
<!-- **************************************************************** -->
<xsl:template name="ClassificationText">

  <xsl:choose>
    <xsl:when test="Classification = 'None'">None</xsl:when>
    <xsl:when test="Classification = 'Check'">Check</xsl:when>
    <xsl:when test="Classification = 'BackSight'">Backsight</xsl:when>
    <xsl:when test="Classification = 'Stakeout'">As-staked</xsl:when>
    <xsl:when test="Classification = 'Construction'">Construction</xsl:when>
    <xsl:when test="Classification = 'Normal'">Normal</xsl:when>
    <xsl:when test="Classification = 'MTA'">MTA</xsl:when>
    <xsl:when test="Classification = 'Adjusted'">Adjusted</xsl:when>
    <xsl:when test="Classification = 'Averaged'">Averaged</xsl:when>
    <xsl:when test="Classification = 'Control'">Control</xsl:when>
    <xsl:when test="Classification = 'NetworkAdjusted'">Network adjusted</xsl:when>
    <xsl:otherwise>None</xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******* Output a table showing GNSS warnings if required ******* -->
<!-- **************************************************************** -->
<xsl:template name="OutputGNSSWarningsTable">

  <xsl:variable name="outputWarnings">
    <xsl:for-each select="QualityControl1/Warnings/*">
      <xsl:if test=". = 'Yes'">
        <xsl:element name="item"/>
      </xsl:if>
    </xsl:for-each>
  </xsl:variable>

  <xsl:if test="count(msxsl:node-set($outputWarnings)/*) != 0">
    <xsl:variable name="ptName" select="Name"/>
    <xsl:for-each select="QualityControl1">
    <xsl:variable name="measureWarning">
      <xsl:for-each select="Warnings/*[contains(name(.), 'Warning')]">
        <xsl:if test=". = 'Yes'">
          <xsl:element name="item"/>
        </xsl:if>
      </xsl:for-each>
    </xsl:variable>

    <xsl:variable name="storageWarning">
      <xsl:for-each select="Warnings/*[contains(name(.), 'AtStorage')]">
        <xsl:if test=". = 'Yes'">
          <xsl:element name="item"/>
        </xsl:if>
      </xsl:for-each>
    </xsl:variable>

    <table border="1" cellpadding="2" width="100%">
      <xsl:if test="count(msxsl:node-set($measureWarning)/*) != 0">
        <tr>
          <th align="left" width="25%">
            <xsl:text>Warnings (</xsl:text>
            <xsl:value-of select="$ptName"/>
            <xsl:text>)</xsl:text>
          </th>
          <xsl:for-each select="Warnings/*[contains(name(.), 'Warning')]">
            <xsl:if test=". = 'Yes'">
                <xsl:choose>
                  <xsl:when test="name() = 'ExcessVibrationWarning'">
                    <td>
                      <xsl:text>High vibration</xsl:text>
                    </td>
                  </xsl:when>
                  <xsl:when test="name() = 'ExcessTiltWarning'">
                    <td>
                      <xsl:text>Excess tilt</xsl:text>
                    </td>
                  </xsl:when>
                  <xsl:when test="name() = 'ExcessMovementWarning'">
                    <td>
                      <xsl:text>Excess movement</xsl:text>
                    </td>
                  </xsl:when>
                  <xsl:when test="name() = 'PoorPrecisionsWarning'">
                    <td>
                      <xsl:text>Poor precision</xsl:text>
                    </td>
                  </xsl:when>
                  <xsl:when test="name() = 'MagneticDisturbanceWarning'">
                    <td>
                      <xsl:text>Magnetic disturbance</xsl:text>
                    </td>
                  </xsl:when>
                </xsl:choose>
            </xsl:if>
          </xsl:for-each>
          <xsl:if test="count(msxsl:node-set($storageWarning)/*) &gt; count(msxsl:node-set($measureWarning)/*)">
            <xsl:call-template name="AddBlankCells">
              <xsl:with-param name="count" select="count(msxsl:node-set($storageWarning)/*) - count(msxsl:node-set($measureWarning)/*)"/>
            </xsl:call-template>
          </xsl:if>
        </tr>
      </xsl:if>

      <xsl:if test="count(msxsl:node-set($storageWarning)/*) != 0">
        <tr>
          <th align="left" width="25%">
            <xsl:text>Conditions at storage (</xsl:text>
            <xsl:value-of select="$ptName"/>
            <xsl:text>)</xsl:text>
          </th>
          <xsl:for-each select="Warnings/*[contains(name(.), 'AtStorage')]">
            <xsl:if test=". = 'Yes'">
                <xsl:choose>
                  <xsl:when test="name() = 'ExcessVibrationAtStorage'">
                    <td>
                      <xsl:text>High vibration</xsl:text>
                    </td>
                  </xsl:when>
                  <xsl:when test="name() = 'ExcessTiltAtStorage'">
                    <td>
                      <xsl:text>Excess tilt</xsl:text>
                    </td>
                  </xsl:when>
                  <xsl:when test="name() = 'ExcessMovementAtStorage'">
                    <td>
                      <xsl:text>Excess movement</xsl:text>
                    </td>
                  </xsl:when>
                  <xsl:when test="name() = 'PoorPrecisionsAtStorage'">
                    <td>
                      <xsl:text>Poor precision</xsl:text>
                    </td>
                  </xsl:when>
                  <xsl:when test="name() = 'MagneticDisturbanceAtStorage'">
                    <td>
                      <xsl:text>Magnetic disturbance</xsl:text>
                    </td>
                  </xsl:when>
                </xsl:choose>
            </xsl:if>
          </xsl:for-each>
          <xsl:if test="count(msxsl:node-set($measureWarning)/*) &gt; count(msxsl:node-set($storageWarning)/*)">
            <xsl:call-template name="AddBlankCells">
              <xsl:with-param name="count" select="count(msxsl:node-set($measureWarning)/*) - count(msxsl:node-set($storageWarning)/*)"/>
            </xsl:call-template>
          </xsl:if>
        </tr>
      </xsl:if>
    </table>
    </xsl:for-each>
  </xsl:if>
</xsl:template>


<!-- **************************************************************** -->
<!-- *** Recursive routine to add specified number of blank cells *** -->
<!-- **************************************************************** -->
<xsl:template name="AddBlankCells">
  <xsl:param name="count"/>
  
  <xsl:if test="$count &gt; 0">
    <td>&#160;</td>
    <xsl:call-template name="AddBlankCells">
      <xsl:with-param name="count" select="$count - 1"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>


<!-- **************************************************************** -->
<!-- **************** Compute mark to mark deltas ******************* -->
<!-- **************************************************************** -->
<xsl:template name="ComputeMarkToMarkDeltas">

  <!-- First get the base lat, long, height position -->
  <xsl:variable name="basePos">
    <xsl:variable name="baseName" select="RTK_Base"/>
    <xsl:for-each select="/JOBFile/Reductions/Point[Name = $baseName]">
      <xsl:copy-of select="WGS84"/>
    </xsl:for-each>
  </xsl:variable>
  
  <!-- Compute the ECEF position for the base -->
  <xsl:variable name="baseECEF">
    <xsl:element name="X">
      <xsl:call-template name="ecefX">
        <xsl:with-param name="latitude" select="msxsl:node-set($basePos)/WGS84/Latitude"/>
        <xsl:with-param name="longitude" select="msxsl:node-set($basePos)/WGS84/Longitude"/>
        <xsl:with-param name="height" select="msxsl:node-set($basePos)/WGS84/Height"/>
      </xsl:call-template>
    </xsl:element>
    
    <xsl:element name="Y">
      <xsl:call-template name="ecefY">
        <xsl:with-param name="latitude" select="msxsl:node-set($basePos)/WGS84/Latitude"/>
        <xsl:with-param name="longitude" select="msxsl:node-set($basePos)/WGS84/Longitude"/>
        <xsl:with-param name="height" select="msxsl:node-set($basePos)/WGS84/Height"/>
      </xsl:call-template>
    </xsl:element>

    <xsl:element name="Z">
      <xsl:call-template name="ecefZ">
        <xsl:with-param name="latitude" select="msxsl:node-set($basePos)/WGS84/Latitude"/>
        <xsl:with-param name="height" select="msxsl:node-set($basePos)/WGS84/Height"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:variable>
  
  <!-- Apply the antenna height to the base height and compute the base APC ECEF position -->
  <xsl:variable name="baseAntHt">
    <xsl:for-each select="key('refID-search', ReferenceRecordID)">
      <xsl:value-of select="key('antID-search', AntennaID)[1]/ReducedHeight"/>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="baseAPCHeight" select="msxsl:node-set($basePos)/WGS84/Height + $baseAntHt"/>

  <xsl:variable name="baseAPC_ECEF">
    <xsl:element name="X">
      <xsl:call-template name="ecefX">
        <xsl:with-param name="latitude" select="msxsl:node-set($basePos)/WGS84/Latitude"/>
        <xsl:with-param name="longitude" select="msxsl:node-set($basePos)/WGS84/Longitude"/>
        <xsl:with-param name="height" select="$baseAPCHeight"/>
      </xsl:call-template>
    </xsl:element>

    <xsl:element name="Y">
      <xsl:call-template name="ecefY">
        <xsl:with-param name="latitude" select="msxsl:node-set($basePos)/WGS84/Latitude"/>
        <xsl:with-param name="longitude" select="msxsl:node-set($basePos)/WGS84/Longitude"/>
        <xsl:with-param name="height" select="$baseAPCHeight"/>
      </xsl:call-template>
    </xsl:element>

    <xsl:element name="Z">
      <xsl:call-template name="ecefZ">
        <xsl:with-param name="latitude" select="msxsl:node-set($basePos)/WGS84/Latitude"/>
        <xsl:with-param name="height" select="$baseAPCHeight"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:variable>

  <!-- Now apply the ECEF deltas to the base APC position to get the rover APC position -->
  <xsl:variable name="roverAPC_ECEF">
    <xsl:element name="X">
      <xsl:value-of select="msxsl:node-set($baseAPC_ECEF)/X + ECEFDeltas/DeltaX"/>
    </xsl:element>

    <xsl:element name="Y">
      <xsl:value-of select="msxsl:node-set($baseAPC_ECEF)/Y + ECEFDeltas/DeltaY"/>
    </xsl:element>

    <xsl:element name="Z">
      <xsl:value-of select="msxsl:node-set($baseAPC_ECEF)/Z + ECEFDeltas/DeltaZ"/>
    </xsl:element>
  </xsl:variable>
  
  <!-- Convert the rover APC position to lat, long, height -->
  <xsl:variable name="roverAPC">
    <xsl:variable name="latitude">
      <xsl:call-template name="LatFromXYZ">
        <xsl:with-param name="X" select="msxsl:node-set($roverAPC_ECEF)/X"/>
        <xsl:with-param name="Y" select="msxsl:node-set($roverAPC_ECEF)/Y"/>
        <xsl:with-param name="Z" select="msxsl:node-set($roverAPC_ECEF)/Z"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:element name="latitude">
      <xsl:value-of select="$latitude"/>
    </xsl:element>

    <xsl:element name="longitude">
      <xsl:call-template name="LongFromXYZ">
        <xsl:with-param name="X" select="msxsl:node-set($roverAPC_ECEF)/X"/>
        <xsl:with-param name="Y" select="msxsl:node-set($roverAPC_ECEF)/Y"/>
      </xsl:call-template>
    </xsl:element>

    <xsl:element name="height">
      <xsl:call-template name="HeightFromXYZ">
        <xsl:with-param name="X" select="msxsl:node-set($roverAPC_ECEF)/X"/>
        <xsl:with-param name="Y" select="msxsl:node-set($roverAPC_ECEF)/Y"/>
        <xsl:with-param name="Z" select="msxsl:node-set($roverAPC_ECEF)/Z"/>
        <xsl:with-param name="lat" select="$latitude"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:variable>
  
  <!-- Apply the rover antenna height to the APC position height -->
  <xsl:variable name="roverAntHt" select="key('antID-search', AntennaID)[1]/ReducedHeight"/>
  <xsl:variable name="roverHeight" select="msxsl:node-set($roverAPC)/height - $roverAntHt"/>

  <!-- Compute the ECEF position for the rover -->
  <xsl:variable name="roverECEF">
    <xsl:element name="X">
      <xsl:call-template name="ecefX">
        <xsl:with-param name="latitude" select="msxsl:node-set($roverAPC)/latitude * 180.0 div $Pi"/>
        <xsl:with-param name="longitude" select="msxsl:node-set($roverAPC)/longitude * 180.0 div $Pi"/>
        <xsl:with-param name="height" select="$roverHeight"/>
      </xsl:call-template>
    </xsl:element>

    <xsl:element name="Y">
      <xsl:call-template name="ecefY">
        <xsl:with-param name="latitude" select="msxsl:node-set($roverAPC)/latitude * 180.0 div $Pi"/>
        <xsl:with-param name="longitude" select="msxsl:node-set($roverAPC)/longitude * 180.0 div $Pi"/>
        <xsl:with-param name="height" select="$roverHeight"/>
      </xsl:call-template>
    </xsl:element>

    <xsl:element name="Z">
      <xsl:call-template name="ecefZ">
        <xsl:with-param name="latitude" select="msxsl:node-set($roverAPC)/latitude * 180.0 div $Pi"/>
        <xsl:with-param name="height" select="$roverHeight"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:variable>
  
  <!-- Return the mark to mark deltas -->
  <xsl:element name="deltaX">
    <xsl:value-of select="msxsl:node-set($roverECEF)/X - msxsl:node-set($baseECEF)/X"/>
  </xsl:element>

  <xsl:element name="deltaY">
    <xsl:value-of select="msxsl:node-set($roverECEF)/Y - msxsl:node-set($baseECEF)/Y"/>
  </xsl:element>

  <xsl:element name="deltaZ">
    <xsl:value-of select="msxsl:node-set($roverECEF)/Z - msxsl:node-set($baseECEF)/Z"/>
  </xsl:element>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************** Compute tilt distance ********************* -->
<!-- **************************************************************** -->
<xsl:template name="ComputeTiltDistance">
  <xsl:param name="reducedAntennaTargetHt"/>

  <xsl:variable name="sineTiltAngle">
    <xsl:choose>
      <xsl:when test="DeviceAxisOrientation/BiVector">
        <xsl:for-each select="DeviceAxisOrientation/BiVector">
          <xsl:variable name="sineXX">
            <xsl:call-template name="Sine">
              <xsl:with-param name="theAngle" select="XX"/>
            </xsl:call-template>
          </xsl:variable>
          <xsl:variable name="sineYY">
            <xsl:call-template name="Sine">
              <xsl:with-param name="theAngle" select="YY"/>
            </xsl:call-template>
          </xsl:variable>
          <xsl:call-template name="Sqrt">  <!-- Return hypotenuse sine component -->
            <xsl:with-param name="num" select="$sineXX * $sineXX + $sineYY * $sineYY"/>
          </xsl:call-template>
        </xsl:for-each>
      </xsl:when>

      <xsl:otherwise>
        <xsl:for-each select="DeviceAxisOrientation/Attitude">
          <xsl:variable name="sinePitch">
            <xsl:call-template name="Sine">
              <xsl:with-param name="theAngle" select="Pitch * $Pi div 180.0"/>
            </xsl:call-template>
          </xsl:variable>
          <xsl:variable name="cosinePitch">
            <xsl:call-template name="Cosine">
              <xsl:with-param name="theAngle" select="Pitch * $Pi div 180.0"/>
            </xsl:call-template>
          </xsl:variable>
          <xsl:variable name="sineRoll">
            <xsl:call-template name="Sine">
              <xsl:with-param name="theAngle" select="Roll * $Pi div 180.0"/>
            </xsl:call-template>
          </xsl:variable>
          <xsl:variable name="adjSineRoll" select="$sineRoll * $cosinePitch"/>
          <xsl:call-template name="Sqrt">  <!-- Return hypotenuse sine component -->
            <xsl:with-param name="num" select="$sinePitch * $sinePitch + $adjSineRoll * $adjSineRoll"/>
          </xsl:call-template>
        </xsl:for-each>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:value-of select="$sineTiltAngle * $reducedAntennaTargetHt"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ********************* Laser Offset Output ********************** -->
<!-- **************************************************************** -->
<xsl:template name="LaserOffset">
  <xsl:call-template name="StartTable"/>

    <xsl:variable name="AzType">
      <xsl:if test="AzimuthType[.='Grid']">Grid</xsl:if>
      <xsl:if test="AzimuthType[.='Magnetic']">Mag</xsl:if>
      <xsl:if test="AzimuthType[.='Local']">Local</xsl:if>
      <xsl:if test="AzimuthType[.='WGS84']">WGS84</xsl:if>
      <xsl:if test="AzimuthType[.='Sun']">Sun</xsl:if>
    </xsl:variable>

    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Laser point</xsl:with-param>

      <xsl:with-param name="Hdr2">Base point</xsl:with-param>
      <xsl:with-param name="Val2" select="BasePoint"/>

      <xsl:with-param name="Hdr3">Laser height</xsl:with-param>
      <xsl:with-param name="Val3" select="format-number(LaserHeight * $DistConvFactor, $DecPl3, 'Standard')"/>

      <xsl:with-param name="Hdr4">Azimuth</xsl:with-param>
      <xsl:with-param name="Val4" select="$AzType"/>

      <xsl:with-param name="Hdr5">
        <xsl:if test="MagneticDeclination">Magnetic declination</xsl:if>
      </xsl:with-param>
      <xsl:with-param name="Val5">
        <xsl:if test="MagneticDeclination">
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="MagneticDeclination"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
        </xsl:if>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Point</xsl:with-param>
      <xsl:with-param name="Val1" select="Name"/>

      <xsl:with-param name="Hdr2">Azimuth</xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:call-template name="FormatAzimuth">
          <xsl:with-param name="theAzimuth" select="LaserOffset/Azimuth"/>
          <xsl:with-param name="northLbl">N</xsl:with-param>
          <xsl:with-param name="eastLbl">E</xsl:with-param>
          <xsl:with-param name="southLbl">S</xsl:with-param>
          <xsl:with-param name="westLbl">W</xsl:with-param>
        </xsl:call-template>
      </xsl:with-param>

      <xsl:with-param name="Hdr3">VA</xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle" select="LaserOffset/VerticalAngle"/>
          <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
        </xsl:call-template>
      </xsl:with-param>

      <xsl:with-param name="Hdr4">SD</xsl:with-param>
      <xsl:with-param name="Val4" select="format-number(LaserOffset/SlopeDistance * $DistConvFactor, $DecPl3, 'Standard')"/>

      <xsl:with-param name="Hdr5">Target height</xsl:with-param>
      <xsl:with-param name="Val5" select="format-number(LaserTargetHeight * $DistConvFactor, $DecPl3, 'Standard')"/>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- **************** Resection Standard Errors Output ************** -->
<!-- **************************************************************** -->
<xsl:template name="ResectionStandardErrors">
  <xsl:call-template name="StartTable"/>
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Resection</xsl:with-param>
      <xsl:with-param name="Val1" select="Name"/>

      <xsl:with-param name="Hdr2">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'">Std Error (N)</xsl:when>
          <xsl:otherwise>Std Error (E)</xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'">
            <xsl:value-of select="format-number(ResectionStandardErrors/NorthStandardError * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number(ResectionStandardErrors/EastStandardError * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>

      <xsl:with-param name="Hdr3">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'">Std Error (E)</xsl:when>
          <xsl:otherwise>Std Error (N)</xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'">
            <xsl:value-of select="format-number(ResectionStandardErrors/EastStandardError * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number(ResectionStandardErrors/NorthStandardError * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>

      <xsl:with-param name="Hdr4">Std Error (El)</xsl:with-param>
      <xsl:with-param name="Val4" select="format-number(ResectionStandardErrors/ElevationStandardError * $ElevConvFactor, $DecPl3, 'Standard')"/>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** Entered Data Record Output ******************* -->
<!-- **************************************************************** -->
<xsl:template name="EnteredData">

  <xsl:choose>
    <xsl:when test="EnteredData/Method[.='BearingDistanceIntersection'] or 
                    EnteredData/Method[.='BearingBearingIntersection'] or
                    EnteredData/Method[.='DistanceDistanceIntersection']">
      <xsl:call-template name="COGOStdIntersection"/>
    </xsl:when>

    <xsl:when test="EnteredData/Method[.='FourPointIntersection']">
      <xsl:call-template name="FourPointIntersection"/>
    </xsl:when>

    <xsl:when test="EnteredData/Method[.='BearingAndDistanceFromAPoint'] or 
                    (EnteredData/Method[.='BearingDistancePointIntersection'] and
                     EnteredData/AzimuthType[.!='Sun'])">
      <xsl:call-template name="BearingDistanceFromPoint"/>
    </xsl:when>

    <xsl:when test="EnteredData/Method[.='TurnedAngleAndDistance'] or
                    (EnteredData/Method[.='BearingDistancePointIntersection'] and
                     EnteredData/AzimuthType[.='Sun'])">
      <xsl:call-template name="TurnedAngleAndDistance"/>
    </xsl:when>

    <xsl:when test="EnteredData/Method[.='FromABaseline']">
      <xsl:call-template name="FromABaseline"/>
    </xsl:when>

    <xsl:when test="EnteredData/Method[.='BearngVerticalPlaneIntersection']">
      <xsl:call-template name="BearngVerticalPlaneIntersection"/>
    </xsl:when>
  </xsl:choose>

  <xsl:call-template name="BlankLine"/>

</xsl:template>


<!-- **************************************************************** -->
<!-- ******************** 3D Axes Record Output ********************* -->
<!-- **************************************************************** -->
<xsl:template name="Measure3DAxes">
  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Measure 3D axes</p></caption>
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Point</xsl:with-param>
      <xsl:with-param name="Val1">
        <xsl:value-of select="Name"/>
      </xsl:with-param>
      <xsl:with-param name="Hdr2">Point 1</xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:value-of select="Measure3DAxes/Point1/Name"/>
      </xsl:with-param>
      <xsl:with-param name="Hdr3">Point 2</xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:value-of select="Measure3DAxes/Point2/Name"/>
      </xsl:with-param>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>

  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Orthogonal deltas to 3D axis</p></caption>
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">H.Offset</xsl:with-param>
      <xsl:with-param name="Val1">
        <xsl:value-of select="format-number(Measure3DAxes/OrthogonalIntersectionPointDetails/HorizontalOffset * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:with-param>
      <xsl:with-param name="Hdr2">Perpendicular offset</xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:value-of select="format-number(Measure3DAxes/OrthogonalIntersectionPointDetails/PerpendicularOffset * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:with-param>
      <xsl:with-param name="Hdr3">Radial offset</xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:value-of select="format-number(Measure3DAxes/OrthogonalIntersectionPointDetails/RadialOffset * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:with-param>
      <xsl:with-param name="Hdr4">Distance to point 1</xsl:with-param>
      <xsl:with-param name="Val4">
        <xsl:value-of select="format-number(Measure3DAxes/OrthogonalIntersectionPointDetails/DistanceToPoint1 * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:with-param>
      <xsl:with-param name="Hdr5">Distance to point 2</xsl:with-param>
      <xsl:with-param name="Val5">
        <xsl:value-of select="format-number(Measure3DAxes/OrthogonalIntersectionPointDetails/DistanceToPoint2 * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:with-param>
    </xsl:call-template>

    <!-- Output the intersection point -->
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Intersect point</xsl:with-param>

      <xsl:with-param name="Hdr2">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'"><xsl:value-of select="$northPrompt"/></xsl:when>
          <xsl:otherwise><xsl:value-of select="$eastPrompt"/></xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'">
            <xsl:value-of select="format-number(Measure3DAxes/OrthogonalIntersectionPointDetails/IntersectionPoint/North * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number(Measure3DAxes/OrthogonalIntersectionPointDetails/IntersectionPoint/East * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>

      <xsl:with-param name="Hdr3">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'"><xsl:value-of select="$eastPrompt"/></xsl:when>
          <xsl:otherwise><xsl:value-of select="$northPrompt"/></xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'">
            <xsl:value-of select="format-number(Measure3DAxes/OrthogonalIntersectionPointDetails/IntersectionPoint/East * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number(Measure3DAxes/OrthogonalIntersectionPointDetails/IntersectionPoint/North * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>

      <xsl:with-param name="Hdr4"><xsl:value-of select="$elevPrompt"/></xsl:with-param>
      <xsl:with-param name="Val4">
        <xsl:value-of select="format-number(Measure3DAxes/OrthogonalIntersectionPointDetails/IntersectionPoint/Elevation * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:with-param>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>

  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Vertical deltas to 3D axis</p></caption>
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">H.Offset</xsl:with-param>
      <xsl:with-param name="Val1">
        <xsl:value-of select="format-number(Measure3DAxes/VerticalIntersectionPointDetails/HorizontalOffset * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:with-param>
      <xsl:with-param name="Hdr2">Perpendicular offset</xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:value-of select="format-number(Measure3DAxes/VerticalIntersectionPointDetails/VerticalOffset * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:with-param>
      <xsl:with-param name="Hdr3">Radial offset</xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:value-of select="format-number(Measure3DAxes/VerticalIntersectionPointDetails/RadialOffset * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:with-param>
      <xsl:with-param name="Hdr4">Distance to point 1</xsl:with-param>
      <xsl:with-param name="Val4">
        <xsl:value-of select="format-number(Measure3DAxes/VerticalIntersectionPointDetails/DistanceToPoint1 * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:with-param>
      <xsl:with-param name="Hdr5">Distance to point 2</xsl:with-param>
      <xsl:with-param name="Val5">
        <xsl:value-of select="format-number(Measure3DAxes/VerticalIntersectionPointDetails/DistanceToPoint2 * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:with-param>
    </xsl:call-template>

    <!-- Output the intersection point -->
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Intersect point</xsl:with-param>

      <xsl:with-param name="Hdr2">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'"><xsl:value-of select="$northPrompt"/></xsl:when>
          <xsl:otherwise><xsl:value-of select="$eastPrompt"/></xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'">
            <xsl:value-of select="format-number(Measure3DAxes/VerticalIntersectionPointDetails/IntersectionPoint/North * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number(Measure3DAxes/VerticalIntersectionPointDetails/IntersectionPoint/East * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>

      <xsl:with-param name="Hdr3">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'"><xsl:value-of select="$eastPrompt"/></xsl:when>
          <xsl:otherwise><xsl:value-of select="$northPrompt"/></xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'">
            <xsl:value-of select="format-number(Measure3DAxes/VerticalIntersectionPointDetails/IntersectionPoint/East * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number(Measure3DAxes/VerticalIntersectionPointDetails/IntersectionPoint/North * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>

      <xsl:with-param name="Hdr4"><xsl:value-of select="$elevPrompt"/></xsl:with-param>
      <xsl:with-param name="Val4">
        <xsl:value-of select="format-number(Measure3DAxes/VerticalIntersectionPointDetails/IntersectionPoint/Elevation * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:with-param>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************ Measure to Surface Point Record Output ************ -->
<!-- **************************************************************** -->
<xsl:template name="MeasureToSurfacePoint">

  <xsl:call-template name="StartTable"/>
    <!--<caption align="top"><p align="left">Measure to surface</p></caption>-->
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Measure to surface</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:value-of select="MeasureToSurfacePoint/SurfaceName"/>
      </xsl:with-param>
      <xsl:with-param name="spanExtraFields">true</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Distance to surface</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:value-of select="format-number(MeasureToSurfacePoint/DistanceToSurface * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:with-param>
      <xsl:with-param name="spanExtraFields">true</xsl:with-param>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>

  <xsl:call-template name="StartTable"/>
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Closest point on surface</xsl:with-param>
      <xsl:with-param name="Val1"/>
      <xsl:with-param name="Hdr2">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'"><xsl:value-of select="$northPrompt"/></xsl:when>
          <xsl:otherwise><xsl:value-of select="$eastPrompt"/></xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'">
            <xsl:value-of select="format-number(MeasureToSurfacePoint/PointOnSurface/North * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number(MeasureToSurfacePoint/PointOnSurface/East * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Hdr3">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'"><xsl:value-of select="$eastPrompt"/></xsl:when>
          <xsl:otherwise><xsl:value-of select="$northPrompt"/></xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'">
            <xsl:value-of select="format-number(MeasureToSurfacePoint/PointOnSurface/East * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number(MeasureToSurfacePoint/PointOnSurface/North * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Hdr4"><xsl:value-of select="$elevPrompt"/></xsl:with-param>
      <xsl:with-param name="Val4">
        <xsl:value-of select="format-number(MeasureToSurfacePoint/PointOnSurface/Elevation * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:with-param>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>

  <xsl:call-template name="BlankLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ****** Handle Brg-Brg, Brg-Dist and Dist-Dist Int Output ******* -->
<!-- **************************************************************** -->
<xsl:template name="COGOStdIntersection">

  <xsl:variable name="Az1Type">
    <xsl:choose>
      <xsl:when test="EnteredData/AzimuthType[.='Grid']">Azimuth 1 (grid)</xsl:when>
      <xsl:when test="EnteredData/AzimuthType[.='Local']">Azimuth 1 (local)</xsl:when>
      <xsl:when test="EnteredData/AzimuthType[.='WGS84']">Azimuth 1 (WGS84)</xsl:when>
      <xsl:otherwise>Azimuth 1</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="Az2Type">
    <xsl:choose>
      <xsl:when test="EnteredData/AzimuthType[.='Grid']">Azimuth 2 (grid)</xsl:when>
      <xsl:when test="EnteredData/AzimuthType[.='Local']">Azimuth 2 (local)</xsl:when>
      <xsl:when test="EnteredData/AzimuthType[.='WGS84']">Azimuth 2 (WGS84)</xsl:when>
      <xsl:otherwise>Azimuth 2</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="Dist1Type">
    <xsl:if test="EnteredData/DistanceType[.='Grid']">Distance 1 (grid)</xsl:if>
    <xsl:if test="EnteredData/DistanceType[.='Ground']">Distance 1 (grnd)</xsl:if>
    <xsl:if test="EnteredData/DistanceType[.='Ellipsoid']">Distance 1 (ell)</xsl:if>
  </xsl:variable>

  <xsl:variable name="Dist2Type">
    <xsl:if test="EnteredData/DistanceType[.='Grid']">Distance 2 (grid)</xsl:if>
    <xsl:if test="EnteredData/DistanceType[.='Ground']">Distance 2 (grnd)</xsl:if>
    <xsl:if test="EnteredData/DistanceType[.='Ellipsoid']">Distance 2 (ell)</xsl:if>
  </xsl:variable>

  <xsl:call-template name="StartTable"/>
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Calc details</xsl:with-param>
      <xsl:with-param name="Val1">
        <xsl:if test="EnteredData/Method[.='BearingDistanceIntersection']">Brng-dist intersect</xsl:if>
        <xsl:if test="EnteredData/Method[.='BearingBearingIntersection']">Brng-brng intersect</xsl:if>
        <xsl:if test="EnteredData/Method[.='DistanceDistanceIntersection']">Dist-dist intersect</xsl:if>
      </xsl:with-param>

      <xsl:with-param name="Hdr2">Point 1</xsl:with-param>
      <xsl:with-param name="Val2" select="EnteredData/PointOne"/>

      <xsl:with-param name="Hdr3">Point 2</xsl:with-param>
      <xsl:with-param name="Val3" select="EnteredData/PointTwo"/>

      <xsl:with-param name="Hdr4">
        <xsl:choose>
          <xsl:when test="EnteredData/AzimuthOne != ''">
            <xsl:value-of select="$Az1Type"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$Dist1Type"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Val4">
        <xsl:choose>
          <xsl:when test="EnteredData/AzimuthOne != ''">
            <xsl:call-template name="FormatAzimuth">
              <xsl:with-param name="theAzimuth" select="EnteredData/AzimuthOne"/>
              <xsl:with-param name="northLbl">N</xsl:with-param>
              <xsl:with-param name="eastLbl">E</xsl:with-param>
              <xsl:with-param name="southLbl">S</xsl:with-param>
              <xsl:with-param name="westLbl">W</xsl:with-param>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number(EnteredData/DistanceOne * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>

      <xsl:with-param name="Hdr5">
        <xsl:choose>
          <xsl:when test="EnteredData/AzimuthTwo != ''">
            <xsl:value-of select="$Az2Type"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$Dist2Type"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Val5">
        <xsl:choose>
          <xsl:when test="EnteredData/AzimuthTwo != ''">
            <xsl:call-template name="FormatAzimuth">
              <xsl:with-param name="theAzimuth" select="EnteredData/AzimuthTwo"/>
              <xsl:with-param name="northLbl">N</xsl:with-param>
              <xsl:with-param name="eastLbl">E</xsl:with-param>
              <xsl:with-param name="southLbl">S</xsl:with-param>
              <xsl:with-param name="westLbl">W</xsl:with-param>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number(EnteredData/DistanceTwo * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************* Handle Four Point Intersection Output ************ -->
<!-- **************************************************************** -->
<xsl:template name="FourPointIntersection">

  <xsl:call-template name="StartTable"/>
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Calc details</xsl:with-param>
      <xsl:with-param name="Val1">Four point intersection</xsl:with-param>

      <xsl:with-param name="Hdr2">Start point (line 1)</xsl:with-param>
      <xsl:with-param name="Val2" select="EnteredData/PointOne"/>

      <xsl:with-param name="Hdr3">End point (line 1)</xsl:with-param>
      <xsl:with-param name="Val3" select="EnteredData/PointTwo"/>

      <xsl:with-param name="Hdr4">Start point (line 2)</xsl:with-param>
      <xsl:with-param name="Val4" select="EnteredData/PointThree"/>

      <xsl:with-param name="Hdr5">End point (line 2)</xsl:with-param>
      <xsl:with-param name="Val5" select="EnteredData/PointFour"/>
    </xsl:call-template>

  <!-- Add a second line with the vertical offset value -->
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr2">Vertical offset</xsl:with-param>
      <xsl:with-param name="Val2" select="format-number(EnteredData/VerticalOffset * $DistConvFactor, $DecPl3, 'Standard')"/>
    </xsl:call-template>

  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******** Handle Bearing and Distance from Point Output ********* -->
<!-- **************************************************************** -->
<xsl:template name="BearingDistanceFromPoint">

  <xsl:variable name="AzType">
    <xsl:choose>
      <xsl:when test="EnteredData/AzimuthType[.='Grid']">Azimuth (grid)</xsl:when>
      <xsl:when test="EnteredData/AzimuthType[.='Magnetic']">Azimuth (mag)</xsl:when>
      <xsl:when test="EnteredData/AzimuthType[.='Local']">Azimuth (local)</xsl:when>
      <xsl:when test="EnteredData/AzimuthType[.='WGS84']">Azimuth (WGS84)</xsl:when>
      <xsl:otherwise>Azimuth</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="DistType">
    <xsl:if test="EnteredData/DistanceType[.='Grid']">Distance (grid)</xsl:if>
    <xsl:if test="EnteredData/DistanceType[.='Ground']">Distance (grnd)</xsl:if>
    <xsl:if test="EnteredData/DistanceType[.='Ellipsoid']">Distance (ell)</xsl:if>
  </xsl:variable>

  <xsl:call-template name="StartTable"/>
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Calc details</xsl:with-param>
      <xsl:with-param name="Val1">Brng-dist from a point</xsl:with-param>

      <xsl:with-param name="Hdr2">Start point</xsl:with-param>
      <xsl:with-param name="Val2" select="EnteredData/PointOne"/>

      <xsl:with-param name="Hdr3" select="$AzType"/>
      <xsl:with-param name="Val3">
        <xsl:call-template name="FormatAzimuth">
          <xsl:with-param name="theAzimuth" select="EnteredData/AzimuthOne"/>
          <xsl:with-param name="northLbl">N</xsl:with-param>
          <xsl:with-param name="eastLbl">E</xsl:with-param>
          <xsl:with-param name="southLbl">S</xsl:with-param>
          <xsl:with-param name="westLbl">W</xsl:with-param>
        </xsl:call-template>
      </xsl:with-param>
      
      <xsl:with-param name="Hdr4" select="$DistType"/>
      <xsl:with-param name="Val4" select="format-number(EnteredData/DistanceOne * $DistConvFactor, $DecPl3, 'Standard')"/>

      <xsl:with-param name="Hdr5">Vertical offset</xsl:with-param>
      <xsl:with-param name="Val5" select="format-number(EnteredData/VerticalOffset * $DistConvFactor, $DecPl3, 'Standard')"/>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- *********** Handle Turned Angle and Distance Output ************ -->
<!-- **************************************************************** -->
<xsl:template name="TurnedAngleAndDistance">

  <xsl:variable name="AzType">
    <xsl:choose>
      <xsl:when test="EnteredData/AzimuthType[.='Grid']">Azimuth (grid)</xsl:when>
      <xsl:when test="EnteredData/AzimuthType[.='Magnetic']">Azimuth (mag)</xsl:when>
      <xsl:when test="EnteredData/AzimuthType[.='Local']">Azimuth (local)</xsl:when>
      <xsl:when test="EnteredData/AzimuthType[.='WGS84']">Azimuth (WGS84)</xsl:when>
      <xsl:otherwise>Azimuth</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="DistType">
    <xsl:if test="EnteredData/DistanceType[.='Grid']">Distance (grid)</xsl:if>
    <xsl:if test="EnteredData/DistanceType[.='Ground']">Distance (grnd)</xsl:if>
    <xsl:if test="EnteredData/DistanceType[.='Ellipsoid']">Distance (ell)</xsl:if>
  </xsl:variable>

  <xsl:call-template name="StartTable"/>
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Calc details</xsl:with-param>
      <xsl:with-param name="Val1">
        <xsl:choose>
        <xsl:when test="EnteredData/AzimuthType = 'Sun'">Brng-dist from a point</xsl:when>
        <xsl:otherwise>Turned angle and distance</xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>

      <xsl:with-param name="Hdr2">Start point</xsl:with-param>
      <xsl:with-param name="Val2" select="EnteredData/PointOne"/>

      <xsl:with-param name="Hdr3">
        <xsl:choose>
          <xsl:when test="EnteredData/PointTwo != ''">End point</xsl:when>
          <xsl:otherwise><xsl:value-of select="$AzType"/></xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:choose>
          <xsl:when test="EnteredData/PointTwo != ''">
            <xsl:value-of select="EnteredData/PointTwo"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="FormatAzimuth">
              <xsl:with-param name="theAzimuth" select="EnteredData/AzimuthOne"/>
              <xsl:with-param name="northLbl">N</xsl:with-param>
              <xsl:with-param name="eastLbl">E</xsl:with-param>
              <xsl:with-param name="southLbl">S</xsl:with-param>
              <xsl:with-param name="westLbl">W</xsl:with-param>
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>

      <xsl:with-param name="Hdr4">
        <xsl:choose>
          <xsl:when test="EnteredData/AzimuthType = 'Sun'">Angle from sun</xsl:when>
          <xsl:otherwise>Turned angle</xsl:otherwise>
         </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Val4">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle" select="EnteredData/TurnedAngle"/>
          <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
        </xsl:call-template>
      </xsl:with-param>      
    </xsl:call-template>

  <!-- Add a second line with the dist and vertical offset values -->
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr2" select="$DistType"/>
      <xsl:with-param name="Val2" select="format-number(EnteredData/DistanceOne * $DistConvFactor, $DecPl3, 'Standard')"/>

      <xsl:with-param name="Hdr3">Vertical offset</xsl:with-param>
      <xsl:with-param name="Val3" select="format-number(EnteredData/VerticalOffset * $DistConvFactor, $DecPl3, 'Standard')"/>
    </xsl:call-template>

  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- **************** Handle From a Baseline Output ***************** -->
<!-- **************************************************************** -->
<xsl:template name="FromABaseline">

  <xsl:call-template name="StartTable"/>
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Calc details</xsl:with-param>
      <xsl:with-param name="Val1">From a baseline</xsl:with-param>

      <xsl:with-param name="Hdr2">Start point</xsl:with-param>
      <xsl:with-param name="Val2" select="EnteredData/PointOne"/>

      <xsl:with-param name="Hdr3">End point</xsl:with-param>
      <xsl:with-param name="Val3" select="EnteredData/PointTwo"/>

      <xsl:with-param name="Hdr4">Distance</xsl:with-param>
      <xsl:with-param name="Val4" select="format-number(EnteredData/DistanceOne * $DistConvFactor, $DecPl3, 'Standard')"/>

      <xsl:with-param name="Hdr5">Direction</xsl:with-param>
      <xsl:with-param name="Val5">
        <xsl:if test="EnteredData/DirectionAlongLine[.='InFromStart']">In from start</xsl:if>
        <xsl:if test="EnteredData/DirectionAlongLine[.='OutFromStart']">Out from start</xsl:if>
        <xsl:if test="EnteredData/DirectionAlongLine[.='InFromEnd']">In from end</xsl:if>
        <xsl:if test="EnteredData/DirectionAlongLine[.='OutFromEnd']">Out from end</xsl:if>
      </xsl:with-param>
    </xsl:call-template>

    <!-- Add a second line with the offset value and distance type -->
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr2">Offset</xsl:with-param>
      <xsl:with-param name="Val2" select="format-number(EnteredData/DistanceTwo * $DistConvFactor, $DecPl3, 'Standard')"/>

      <xsl:with-param name="Hdr3">Distances</xsl:with-param>
      <xsl:with-param name="Val3" select="EnteredData/DistanceType"/>
    </xsl:call-template>
    
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- *********** Handle Vertical Plane Intersection Output ********** -->
<!-- **************************************************************** -->
<xsl:template name="BearngVerticalPlaneIntersection">

  <xsl:call-template name="StartTable"/>
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Calc details</xsl:with-param>
      <xsl:with-param name="Val1">Vertical plane and angle</xsl:with-param>

      <xsl:with-param name="Hdr2">Point 1</xsl:with-param>
      <xsl:with-param name="Val2" select="EnteredData/PointOne"/>

      <xsl:with-param name="Hdr3">Point 2</xsl:with-param>
      <xsl:with-param name="Val3" select="EnteredData/PointTwo"/>

      <xsl:with-param name="Hdr4">HA</xsl:with-param>
      <xsl:with-param name="Val4">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle" select="EnteredData/VerticalPlaneIntersectionRay/HorizontalCircle"/>
          <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
        </xsl:call-template>
      </xsl:with-param>      

      <xsl:with-param name="Hdr5">VA</xsl:with-param>
      <xsl:with-param name="Val5">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle" select="EnteredData/VerticalPlaneIntersectionRay/VerticalCircle"/>
          <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
        </xsl:call-template>
      </xsl:with-param>      
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- *************** Features and Attributes Output ***************** -->
<!-- **************************************************************** -->
<xsl:template name="FeatureAndAttributes">
  <xsl:for-each select="Features/Feature">
    <xsl:call-template name="StartTable"/>
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr"><xsl:text>Feature</xsl:text><xsl:value-of select="concat(' (', ../../Name, ')')"/></xsl:with-param>
      <xsl:with-param name="Val" select="@Name"/>
    </xsl:call-template>

    <xsl:for-each select="Attribute">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr" select="Name"/>
        <xsl:with-param name="Val" select="Value"/>
      </xsl:call-template>
    </xsl:for-each>

    <xsl:call-template name="EndTable"/>
  </xsl:for-each>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************** Point/Line/Arc Descriptions Output ************** -->
<!-- **************************************************************** -->
<xsl:template name="OutputDescriptions">
  <xsl:param name="lineArc" select="'false'"/>
  
  <xsl:if test="Description1 or Description2">  <!-- One or other of the elements is present -->
    <xsl:choose>
      <xsl:when test="$lineArc = 'true'">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr" select="Description1/@Name"/>
          <xsl:with-param name="Val" select="Description1"/>
        </xsl:call-template>

        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr" select="Description2/@Name"/>
          <xsl:with-param name="Val" select="Description2"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="OutputTableLine">
          <xsl:with-param name="Hdr2" select="Description1/@Name"/>
          <xsl:with-param name="Val2" select="Description1"/>

          <xsl:with-param name="Hdr3" select="Description2/@Name"/>
          <xsl:with-param name="Val3" select="Description2"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:if>
</xsl:template>


<!-- **************************************************************** -->
<!-- ********************* Stakeout Data Output ********************* -->
<!-- **************************************************************** -->
<xsl:template name="Stakeout">
  <xsl:if test="Stakeout/PointDesign">
    <xsl:call-template name="PointDesign"/>
  </xsl:if>

  <xsl:if test="Stakeout/LineDesign">
    <xsl:call-template name="LineDesign"/>
  </xsl:if>

  <xsl:if test="Stakeout/PolylineDesign">
    <xsl:call-template name="PolylineDesign"/>
  </xsl:if>

  <xsl:if test="Stakeout/ArcDesign">
    <xsl:call-template name="ArcDesign"/>
  </xsl:if>

  <xsl:if test="Stakeout/AlignmentDesign">
    <xsl:call-template name="AlignmentDesign"/>
  </xsl:if>

  <xsl:if test="Stakeout/SlopeDesign">
    <xsl:call-template name="SlopeDesign"/>
  </xsl:if>

  <xsl:if test="Stakeout/DTMDesign">
    <xsl:call-template name="DTMDesign"/>
  </xsl:if>

  <xsl:if test="Stakeout/RoadDesign">
    <xsl:call-template name="RoadDesign"/>
  </xsl:if>

  <xsl:if test="Stakeout/GridDeltas">
    <xsl:call-template name="GridDeltas"/>
  </xsl:if>

  <xsl:if test="Stakeout/LinearDeltas">
    <xsl:call-template name="LinearDeltas"/>
  </xsl:if>

  <xsl:if test="Stakeout/DTMDeltas">
    <xsl:call-template name="DTMDeltas"/>
  </xsl:if>

  <xsl:if test="Stakeout/PolarDeltas">
    <xsl:call-template name="PolarDeltas"/>
  </xsl:if>

  <xsl:if test="Stakeout/CatchPoint">
    <xsl:call-template name="CatchPoint"/>
  </xsl:if>

  <xsl:if test="Stakeout/CatchPointTemplateReport">
    <xsl:call-template name="CatchPointTemplateRpt"/>
  </xsl:if>

  <xsl:call-template name="SeparatingLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** Point Stakeout Data Output ****************** -->
<!-- **************************************************************** -->
<xsl:template name="PointDesign">
  <xsl:call-template name="StartTable"/>
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">
        <xsl:text>Stake out point</xsl:text>
        <xsl:value-of select="concat(' (', Name, ')')"/>
      </xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:text>Design point: </xsl:text><xsl:value-of select="Stakeout/PointDesign/Name"/>
        <xsl:text>Code: </xsl:text><xsl:value-of select="Stakeout/PointDesign/Code"/>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Method</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:call-template name="StakeoutMethodText">
          <xsl:with-param name="Method" select="Stakeout/PointDesign/StakeoutMethod"/>
        </xsl:call-template>
      </xsl:with-param>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************* Line Stakeout Data Output ****************** -->
<!-- **************************************************************** -->
<xsl:template name="LineDesign">
  <xsl:call-template name="StartTable"/>
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">
        <xsl:text>Stake out line</xsl:text>
        <xsl:value-of select="concat(' (', Name, ')')"/>
      </xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:text>Line name: </xsl:text>
        <xsl:value-of select="Stakeout/LineDesign/Name"/>
        <xsl:text> Code: </xsl:text>
        <xsl:value-of select="Stakeout/LineDesign/Code"/>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Method</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:call-template name="StakeoutMethodText">
          <xsl:with-param name="Method" select="Stakeout/LineDesign/StakeoutMethod"/>
        </xsl:call-template>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:if test="Stakeout/LineDesign/Station != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">
          <xsl:choose>
            <xsl:when test="$useChainageTerminology = 'true'">Chainage</xsl:when>
            <xsl:otherwise>Station</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:call-template name="FormatStationVal">
            <xsl:with-param name="stationVal" select="Stakeout/LineDesign/Station"/>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
    
    <xsl:if test="Stakeout/LineDesign/Offset != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">
          <xsl:choose>
            <xsl:when test="Stakeout/LineDesign/StakeoutMethod = 'PointSkewOffsetFromLine'">Skew offset</xsl:when>
            <xsl:otherwise>Offset</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Stakeout/LineDesign/Offset * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/LineDesign/Elevation != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr" select="$elevPrompt"/>
        <xsl:with-param name="Val" select="format-number(Stakeout/LineDesign/Elevation * $ElevConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>
    
    <xsl:if test="Stakeout/LineDesign/VerticalOffset != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">V.Offset</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Stakeout/LineDesign/VerticalOffset * $ElevConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/LineDesign/OriginalDesignElevation and (string(number(Stakeout/LineDesign/OriginalDesignElevation)) != 'NaN')">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Original design elev</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Stakeout/LineDesign/OriginalDesignElevation * $ElevConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/LineDesign/StakeoutMethod = 'PointSkewOffsetFromLine'">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">
          <xsl:text>Skew (</xsl:text>
          <xsl:choose>
            <xsl:when test="Stakeout/LineDesign/SkewMethod = 'SkewByAngleForward'">Forward</xsl:when>
            <xsl:when test="Stakeout/LineDesign/SkewMethod = 'SkewByAngleBackward'">Backward</xsl:when>
            <xsl:otherwise>
              <xsl:choose>
                <xsl:when test="/JOBFile/Environment/DisplaySettings/AzimuthFormat = 'QuadrantBearings'">Bearing</xsl:when>
                <xsl:otherwise>Azimuth</xsl:otherwise>
              </xsl:choose>
            </xsl:otherwise>
          </xsl:choose>
          <xsl:text>)</xsl:text>
        </xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:choose>
            <xsl:when test="(Stakeout/LineDesign/SkewMethod = 'SkewByAngleForward') or (Stakeout/LineDesign/SkewMethod = 'SkewByAngleBackward')">
              <xsl:call-template name="FormatAngle">
                <xsl:with-param name="theAngle" select="Stakeout/LineDesign/SkewAngle"/>
                <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name="FormatAzimuth">
                <xsl:with-param name="theAzimuth" select="Stakeout/LineDesign/SkewAngle"/>
                <xsl:with-param name="northLbl">N</xsl:with-param>
                <xsl:with-param name="eastLbl">E</xsl:with-param>
                <xsl:with-param name="southLbl">S</xsl:with-param>
                <xsl:with-param name="westLbl">W</xsl:with-param>
              </xsl:call-template>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************* Polyline Stakeout Data Output ****************** -->
<!-- **************************************************************** -->
<xsl:template name="PolylineDesign">
  <xsl:call-template name="StartTable"/>
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">
        <xsl:text>Stake out polyline</xsl:text>
        <xsl:value-of select="concat(' (', Name, ')')"/>
      </xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:text>Polyline name: </xsl:text>
        <xsl:value-of select="Stakeout/PolylineDesign/Name"/>
        <xsl:text> Code: </xsl:text>
        <xsl:value-of select="Stakeout/PolylineDesign/Code"/>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Method</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:call-template name="StakeoutMethodText">
          <xsl:with-param name="Method" select="Stakeout/PolylineDesign/StakeoutMethod"/>
        </xsl:call-template>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:if test="Stakeout/PolylineDesign/Station != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">
          <xsl:choose>
            <xsl:when test="$useChainageTerminology = 'true'">Chainage</xsl:when>
            <xsl:otherwise>Station</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:call-template name="FormatStationVal">
            <xsl:with-param name="stationVal" select="Stakeout/PolylineDesign/Station"/>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/PolylineDesign/Offset != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">
          <xsl:choose>
            <xsl:when test="Stakeout/PolylineDesign/StakeoutMethod = 'PointsSkewOffsetFromPolyline'">Skew offset</xsl:when>
            <xsl:otherwise>Offset</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Stakeout/PolylineDesign/Offset * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/PolylineDesign/Elevation != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr" select="$elevPrompt"/>
        <xsl:with-param name="Val" select="format-number(Stakeout/PolylineDesign/Elevation * $ElevConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/PolylineDesign/VerticalOffset != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">V.Offset</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Stakeout/PolylineDesign/VerticalOffset * $ElevConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/PolylineDesign/OriginalDesignElevation and (string(number(Stakeout/PolylineDesign/OriginalDesignElevation)) != 'NaN')">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Original design elev</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Stakeout/PolylineDesign/OriginalDesignElevation * $ElevConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>
    
    <xsl:if test="Stakeout/PolylineDesign/StakeoutMethod = 'PointsSkewOffsetFromPolyline'">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">
          <xsl:text>Skew (</xsl:text>
          <xsl:choose>
            <xsl:when test="Stakeout/PolylineDesign/SkewMethod = 'SkewByAngleForward'">Forward</xsl:when>
            <xsl:when test="Stakeout/PolylineDesign/SkewMethod = 'SkewByAngleBackward'">Backward</xsl:when>
            <xsl:otherwise>
              <xsl:choose>
                <xsl:when test="/JOBFile/Environment/DisplaySettings/AzimuthFormat = 'QuadrantBearings'">Bearing</xsl:when>
                <xsl:otherwise>Azimuth</xsl:otherwise>
              </xsl:choose>
            </xsl:otherwise>
          </xsl:choose>
          <xsl:text>)</xsl:text>
        </xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:choose>
            <xsl:when test="(Stakeout/PolylineDesign/SkewMethod = 'SkewByAngleForward') or (Stakeout/PolylineDesign/SkewMethod = 'SkewByAngleBackward')">
              <xsl:call-template name="FormatAngle">
                <xsl:with-param name="theAngle" select="Stakeout/PolylineDesign/SkewAngle"/>
                <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name="FormatAzimuth">
                <xsl:with-param name="theAzimuth" select="Stakeout/PolylineDesign/SkewAngle"/>
                <xsl:with-param name="northLbl">N</xsl:with-param>
                <xsl:with-param name="eastLbl">E</xsl:with-param>
                <xsl:with-param name="southLbl">S</xsl:with-param>
                <xsl:with-param name="westLbl">W</xsl:with-param>
              </xsl:call-template>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************* Arc Stakeout Data Output ******************* -->
<!-- **************************************************************** -->
<xsl:template name="ArcDesign">
  <xsl:call-template name="StartTable"/>
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">
        <xsl:text>Stake out arc</xsl:text>
        <xsl:value-of select="concat(' (', Name, ')')"/>
      </xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:text>Arc name: </xsl:text><xsl:value-of select="Stakeout/ArcDesign/Name"/>
        <xsl:text> Code: </xsl:text><xsl:value-of select="Stakeout/ArcDesign/Code"/>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Method</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:call-template name="StakeoutMethodText">
          <xsl:with-param name="Method" select="Stakeout/ArcDesign/StakeoutMethod"/>
        </xsl:call-template>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:if test="Stakeout/ArcDesign/Station != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">
          <xsl:choose>
            <xsl:when test="$useChainageTerminology = 'true'">Chainage</xsl:when>
            <xsl:otherwise>Station</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:call-template name="FormatStationVal">
            <xsl:with-param name="stationVal" select="Stakeout/ArcDesign/Station"/>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/ArcDesign/Offset != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">
          <xsl:choose>
            <xsl:when test="Stakeout/ArcDesign/StakeoutMethod = 'PointSkewOffsetFromArc'">Skew offset</xsl:when>
            <xsl:otherwise>Offset</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Stakeout/ArcDesign/Offset * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/ArcDesign/Elevation != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr" select="$elevPrompt"/>
        <xsl:with-param name="Val" select="format-number(Stakeout/ArcDesign/Elevation * $ElevConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>
    
    <xsl:if test="Stakeout/ArcDesign/VerticalOffset != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">V.Offset</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Stakeout/ArcDesign/VerticalOffset * $ElevConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/ArcDesign/OriginalDesignElevation and (string(number(Stakeout/ArcDesign/OriginalDesignElevation)) != 'NaN')">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Original design elev</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Stakeout/ArcDesign/OriginalDesignElevation * $ElevConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/ArcDesign/StakeoutMethod = 'PointSkewOffsetFromArc'">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">
          <xsl:text>Skew (</xsl:text>
          <xsl:choose>
            <xsl:when test="Stakeout/ArcDesign/SkewMethod = 'SkewByAngleForward'">Forward</xsl:when>
            <xsl:when test="Stakeout/ArcDesign/SkewMethod = 'SkewByAngleBackward'">Backward</xsl:when>
            <xsl:otherwise>
              <xsl:choose>
                <xsl:when test="/JOBFile/Environment/DisplaySettings/AzimuthFormat = 'QuadrantBearings'">Bearing</xsl:when>
                <xsl:otherwise>Azimuth</xsl:otherwise>
              </xsl:choose>
            </xsl:otherwise>
          </xsl:choose>
          <xsl:text>)</xsl:text>
        </xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:choose>
            <xsl:when test="(Stakeout/ArcDesign/SkewMethod = 'SkewByAngleForward') or (Stakeout/ArcDesign/SkewMethod = 'SkewByAngleBackward')">
              <xsl:call-template name="FormatAngle">
                <xsl:with-param name="theAngle" select="Stakeout/ArcDesign/SkewAngle"/>
                <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name="FormatAzimuth">
                <xsl:with-param name="theAzimuth" select="Stakeout/ArcDesign/SkewAngle"/>
                <xsl:with-param name="northLbl">N</xsl:with-param>
                <xsl:with-param name="eastLbl">E</xsl:with-param>
                <xsl:with-param name="southLbl">S</xsl:with-param>
                <xsl:with-param name="westLbl">W</xsl:with-param>
              </xsl:call-template>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- **************** Alignment Stakeout Data Output **************** -->
<!-- **************************************************************** -->
<xsl:template name="AlignmentDesign">
  <xsl:call-template name="StartTable"/>
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">
        <xsl:text>Stake out alignment</xsl:text>
        <xsl:value-of select="concat(' (', Name, ')')"/>
      </xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:text>Alignment name: </xsl:text>
        <xsl:value-of select="Stakeout/AlignmentDesign/Name"/>
        <xsl:text> Code: </xsl:text>
        <xsl:value-of select="Stakeout/AlignmentDesign/Code"/>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Method</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:call-template name="StakeoutMethodText">
          <xsl:with-param name="Method" select="Stakeout/AlignmentDesign/StakeoutMethod"/>
        </xsl:call-template>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:if test="Stakeout/AlignmentDesign/Station != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">
          <xsl:choose>
            <xsl:when test="$useChainageTerminology = 'true'">Chainage</xsl:when>
            <xsl:otherwise>Station</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:call-template name="FormatStationVal">
            <xsl:with-param name="stationVal" select="Stakeout/AlignmentDesign/Station"/>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/AlignmentDesign/Offset != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">
          <xsl:choose>
            <xsl:when test="Stakeout/AlignmentDesign/StakeoutMethod = 'PointSkewOffsetFromLine'">Skew offset</xsl:when>
            <xsl:otherwise>Offset</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Stakeout/AlignmentDesign/Offset * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/AlignmentDesign/Elevation != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr" select="$elevPrompt"/>
        <xsl:with-param name="Val" select="format-number(Stakeout/AlignmentDesign/Elevation * $ElevConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/AlignmentDesign/VerticalOffset != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">V.Offset</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Stakeout/AlignmentDesign/VerticalOffset * $ElevConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/AlignmentDesign/OriginalDesignElevation and (string(number(Stakeout/AlignmentDesign/OriginalDesignElevation)) != 'NaN')">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Original design elev</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Stakeout/AlignmentDesign/OriginalDesignElevation * $ElevConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/AlignmentDesign/StakeoutMethod = 'StationSkewOffsetFromAlignment'">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">
          <xsl:text>Skew (</xsl:text>
          <xsl:choose>
            <xsl:when test="Stakeout/AlignmentDesign/SkewMethod = 'SkewByAngleForward'">Forward</xsl:when>
            <xsl:when test="Stakeout/AlignmentDesign/SkewMethod = 'SkewByAngleBackward'">Backward</xsl:when>
            <xsl:otherwise>
              <xsl:choose>
                <xsl:when test="/JOBFile/Environment/DisplaySettings/AzimuthFormat = 'QuadrantBearings'">Bearing</xsl:when>
                <xsl:otherwise>Azimuth</xsl:otherwise>
              </xsl:choose>
            </xsl:otherwise>
          </xsl:choose>
          <xsl:text>)</xsl:text>
        </xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:choose>
            <xsl:when test="(Stakeout/AlignmentDesign/SkewMethod = 'SkewByAngleForward') or (Stakeout/AlignmentDesign/SkewMethod = 'SkewByAngleBackward')">
              <xsl:call-template name="FormatAngle">
                <xsl:with-param name="theAngle" select="Stakeout/AlignmentDesign/SkewAngle"/>
                <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name="FormatAzimuth">
                <xsl:with-param name="theAzimuth" select="Stakeout/AlignmentDesign/SkewAngle"/>
                <xsl:with-param name="northLbl">N</xsl:with-param>
                <xsl:with-param name="eastLbl">E</xsl:with-param>
                <xsl:with-param name="southLbl">S</xsl:with-param>
                <xsl:with-param name="westLbl">W</xsl:with-param>
              </xsl:call-template>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/AlignmentDesign/HorizontalConstructionOffset and
                  Stakeout/AlignmentDesign/HorizontalConstructionOffset != 0 and
                  Stakeout/AlignmentDesign/HorizontalConstructionOffset != ''">
      <xsl:variable name="offsetAppliedAs">
        <xsl:choose>
          <xsl:when test="Stakeout/AlignmentDesign/HorizontalConstructionOffsetApplied = 'Horizontal'">Horizontal</xsl:when>
          <xsl:otherwise>Slope</xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Construction H.Offset</xsl:with-param>
        <xsl:with-param name="Val" select="concat(format-number(Stakeout/AlignmentDesign/HorizontalConstructionOffset * $DistConvFactor, $DecPl3, 'Standard'),
                                                  ' (', $offsetAppliedAs, ')')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/AlignmentDesign/VerticalConstructionOffset and
                  Stakeout/AlignmentDesign/VerticalConstructionOffset != 0 and
                  Stakeout/AlignmentDesign/VerticalConstructionOffset != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Construction V.Offset</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Stakeout/AlignmentDesign/VerticalConstructionOffset * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/AlignmentDesign/StationConstructionOffset and
                  Stakeout/AlignmentDesign/StationConstructionOffset != 0 and
                  Stakeout/AlignmentDesign/StationConstructionOffset != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">
          <xsl:choose>
            <xsl:when test="$useChainageTerminology = 'true'">Construction chainage offset</xsl:when>
            <xsl:otherwise>Construction station offset</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Stakeout/AlignmentDesign/StationConstructionOffset * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/AlignmentDesign/StationClass != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">
          <xsl:choose>
            <xsl:when test="$useChainageTerminology = 'true'">Chainage class</xsl:when>
            <xsl:otherwise>Station class</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:call-template name="StationClassString">
            <xsl:with-param name="ClassStr" select="Stakeout/AlignmentDesign/StationClass"/>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** Slope Stakeout Data Output ******************* -->
<!-- **************************************************************** -->
<xsl:template name="SlopeDesign">
  <xsl:if test="Stakeout/SlopeDesign/SlopeLeft">
    <xsl:call-template name="StartTable"/>
      <caption align="top">
        <p align="left">
        <xsl:text>Stakeout - Slope left: </xsl:text>
        <xsl:call-template name="SlopeMethodText">
          <xsl:with-param name="Method" select="Stakeout/SlopeDesign/SlopeLeft/Type"/>
        </xsl:call-template>
        </p>
      </caption>

      <xsl:call-template name="OutputTableLine">
        <xsl:with-param name="Hdr1">Slope</xsl:with-param>

        <xsl:with-param name="Hdr2">H.Dist</xsl:with-param>
        <xsl:with-param name="Val2" select="format-number(Stakeout/SlopeDesign/SlopeLeft/HorizontalDistance  * $DistConvFactor, $DecPl3, 'Standard')"/>

        <xsl:with-param name="Hdr3">V.Dist</xsl:with-param>
        <xsl:with-param name="Val3" select="format-number(Stakeout/SlopeDesign/SlopeLeft/VerticalDistance  * $DistConvFactor, $DecPl3, 'Standard')"/>

        <xsl:with-param name="Hdr4">Slope dist</xsl:with-param>
        <xsl:with-param name="Val4" select="format-number(Stakeout/SlopeDesign/SlopeLeft/SlopeDistance  * $DistConvFactor, $DecPl3, 'Standard')"/>

        <xsl:with-param name="Hdr5">Grade</xsl:with-param>
        <xsl:with-param name="Val5" select="concat(format-number(Stakeout/SlopeDesign/SlopeLeft/Grade, $DecPl2, 'Standard'), '%')"/>
      </xsl:call-template>
    <xsl:call-template name="EndTable"/>
  </xsl:if>

  <xsl:if test="Stakeout/SlopeDesign/SlopeRight">
    <xsl:call-template name="StartTable"/>
      <caption align="top">
        <p align="left">
        <xsl:text>Stakeout - Slope right: </xsl:text>
        <xsl:call-template name="SlopeMethodText">
          <xsl:with-param name="Method" select="Stakeout/SlopeDesign/SlopeRight/Type"/>
        </xsl:call-template>
        </p>
      </caption>

      <xsl:call-template name="OutputTableLine">
        <xsl:with-param name="Hdr1">Slope</xsl:with-param>

        <xsl:with-param name="Hdr2">H.Dist</xsl:with-param>
        <xsl:with-param name="Val2" select="format-number(Stakeout/SlopeDesign/SlopeRight/HorizontalDistance  * $DistConvFactor, $DecPl3, 'Standard')"/>

        <xsl:with-param name="Hdr3">V.Dist</xsl:with-param>
        <xsl:with-param name="Val3" select="format-number(Stakeout/SlopeDesign/SlopeRight/VerticalDistance  * $DistConvFactor, $DecPl3, 'Standard')"/>

        <xsl:with-param name="Hdr4">Slope dist</xsl:with-param>
        <xsl:with-param name="Val4" select="format-number(Stakeout/SlopeDesign/SlopeRight/SlopeDistance  * $DistConvFactor, $DecPl3, 'Standard')"/>

        <xsl:with-param name="Hdr5">Grade</xsl:with-param>
        <xsl:with-param name="Val5" select="concat(format-number(Stakeout/SlopeDesign/SlopeRight/Grade, $DecPl2, 'Standard'), '%')"/>
      </xsl:call-template>
    <xsl:call-template name="EndTable"/>
  </xsl:if>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************* DTM Stakeout Data Output ******************* -->
<!-- **************************************************************** -->
<xsl:template name="DTMDesign">
  <xsl:call-template name="StartTable"/>
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">
        <xsl:text>Stake out DTM</xsl:text><xsl:value-of select="concat(' (', Name, ')')"/>
      </xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:text>Surface: </xsl:text><xsl:value-of select="Stakeout/DTMDesign/Name"/>
        <xsl:text> Vertical offset: </xsl:text><xsl:value-of select="format-number(Stakeout/DTMDesign/VerticalOffset * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:with-param>
    </xsl:call-template>

  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- *************** Road Design Stakeout Data Output *************** -->
<!-- **************************************************************** -->
<xsl:template name="RoadDesign">
  <xsl:call-template name="StartTable"/>
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">
        <xsl:choose>
          <xsl:when test="$useRailTerminology = 'true'">Stake out railroad</xsl:when>
          <xsl:otherwise>Stake out road</xsl:otherwise>
        </xsl:choose>
        <xsl:value-of select="concat(' (', Name, ')')"/>
      </xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:choose>
          <xsl:when test="$useRailTerminology = 'true'">Railroad name: </xsl:when>
          <xsl:otherwise>Road name: </xsl:otherwise>
        </xsl:choose>
        <xsl:value-of select="Stakeout/RoadDesign/Name"/>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Design code</xsl:with-param>
      <xsl:with-param name="Val" select="Stakeout/RoadDesign/Code"/>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Method</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:call-template name="StakeoutMethodText">
          <xsl:with-param name="Method" select="Stakeout/RoadDesign/StakeoutMethod"/>
        </xsl:call-template>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:if test="Stakeout/RoadDesign/Station != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">
          <xsl:choose>
            <xsl:when test="$useChainageTerminology = 'true'">Chainage</xsl:when>
            <xsl:otherwise>Station</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:choose>
            <xsl:when test="Stakeout/RoadDesign/EquatedStation"> 
              <xsl:call-template name="FormatStationVal">
                <xsl:with-param name="stationVal" select="Stakeout/RoadDesign/EquatedStation/Station"/>
                <xsl:with-param name="zoneVal" select="Stakeout/RoadDesign/EquatedStation/Zone"/>
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name="FormatStationVal">
                <xsl:with-param name="stationVal" select="Stakeout/RoadDesign/Station"/>
              </xsl:call-template>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
    
    <xsl:if test="Stakeout/RoadDesign/Offset != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">
          <xsl:choose>
            <xsl:when test="(Stakeout/RoadDesign/StakeoutMethod = 'StationSkewOffsetFromRoad') or
                            (Stakeout/RoadDesign/StakeoutMethod = 'StationSkewOffsetFromString')">Skew offset</xsl:when>
            <xsl:otherwise>Offset</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Stakeout/RoadDesign/Offset * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/RoadDesign/Elevation != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Design elevation</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Stakeout/RoadDesign/Elevation * $ElevConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="(Stakeout/RoadDesign/StakeoutMethod = 'StationSkewOffsetFromRoad') or (Stakeout/RoadDesign/StakeoutMethod = 'StationSkewOffsetFromString')">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">
          <xsl:text>Skew (</xsl:text>
          <xsl:choose>
            <xsl:when test="Stakeout/RoadDesign/SkewMethod = 'SkewByAngleForward'">Forward</xsl:when>
            <xsl:when test="Stakeout/RoadDesign/SkewMethod = 'SkewByAngleBackward'">Backward</xsl:when>
            <xsl:otherwise>
              <xsl:choose>
                <xsl:when test="/JOBFile/Environment/DisplaySettings/AzimuthFormat = 'QuadrantBearings'">Bearing</xsl:when>
                <xsl:otherwise>Azimuth</xsl:otherwise>
              </xsl:choose>
            </xsl:otherwise>
          </xsl:choose>
          <xsl:text>)</xsl:text>
        </xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:choose>
            <xsl:when test="(Stakeout/RoadDesign/SkewMethod = 'SkewByAngleForward') or (Stakeout/RoadDesign/SkewMethod = 'SkewByAngleBackward')">
              <xsl:call-template name="FormatAngle">
                <xsl:with-param name="theAngle" select="Stakeout/RoadDesign/SkewAngle"/>
                <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name="FormatAzimuth">
                <xsl:with-param name="theAzimuth" select="Stakeout/RoadDesign/SkewAngle"/>
                <xsl:with-param name="northLbl">N</xsl:with-param>
                <xsl:with-param name="eastLbl">E</xsl:with-param>
                <xsl:with-param name="southLbl">S</xsl:with-param>
                <xsl:with-param name="westLbl">W</xsl:with-param>
              </xsl:call-template>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/RoadDesign/HorizontalConstructionOffset and 
                  Stakeout/RoadDesign/HorizontalConstructionOffset != 0 and
                  Stakeout/RoadDesign/HorizontalConstructionOffset != ''">
      <xsl:variable name="offsetAppliedAs">
        <xsl:choose>
          <xsl:when test="Stakeout/RoadDesign/HorizontalConstructionOffsetApplied = 'Horizontal'">Horizontal</xsl:when>
          <xsl:otherwise>Slope</xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Construction H.Offset</xsl:with-param>
        <xsl:with-param name="Val" select="concat(format-number(Stakeout/RoadDesign/HorizontalConstructionOffset * $DistConvFactor, $DecPl3, 'Standard'),
                                                  ' (', $offsetAppliedAs, ')')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/RoadDesign/VerticalConstructionOffset and 
                  Stakeout/RoadDesign/VerticalConstructionOffset != 0 and
                  Stakeout/RoadDesign/VerticalConstructionOffset != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Construction V.Offset</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Stakeout/RoadDesign/VerticalConstructionOffset * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/RoadDesign/StationConstructionOffset and 
                  Stakeout/RoadDesign/StationConstructionOffset != 0 and
                  Stakeout/RoadDesign/StationConstructionOffset != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">
          <xsl:choose>
            <xsl:when test="$useChainageTerminology = 'true'">Construction chainage offset</xsl:when>
            <xsl:otherwise>Construction station offset</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Stakeout/RoadDesign/StationConstructionOffset * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>
    
    <xsl:if test="Stakeout/RoadDesign/StationClass != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">
          <xsl:choose>
            <xsl:when test="$useChainageTerminology = 'true'">Chainage class</xsl:when>
            <xsl:otherwise>Station class</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:call-template name="StationClassString">
            <xsl:with-param name="ClassStr" select="Stakeout/RoadDesign/StationClass"/>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Stakeout/RoadDesign/SideSlopeGrade and 
                  Stakeout/RoadDesign/SideSlopeGrade != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Design side slope grade</xsl:with-param>
        <xsl:with-param name="Val" select="concat(format-number(Stakeout/RoadDesign/SideSlopeGrade, $DecPl2, 'Standard'), '%')"/>
      </xsl:call-template>
    </xsl:if>
    
    <xsl:if test="Stakeout/RoadDesign/DitchOffset and 
                  Stakeout/RoadDesign/DitchOffset != 0 and
                  Stakeout/RoadDesign/DitchOffset != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Cut ditch width</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Stakeout/RoadDesign/DitchOffset * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>
    
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** Stakeout Grid Deltas Output ****************** -->
<!-- **************************************************************** -->
<xsl:template name="GridDeltas">
  <xsl:if test="Stakeout/GridDeltas/DeltaNorth != '' or
                Stakeout/GridDeltas/DeltaEast != '' or 
                Stakeout/GridDeltas/DeltaElevation != ''">
    <xsl:call-template name="StartTable"/>

      <xsl:call-template name="OutputTableLine">
        <xsl:with-param name="Hdr1">Stakeout</xsl:with-param>
        <xsl:with-param name="Val1">Deltas: Grid</xsl:with-param>

        <xsl:with-param name="Hdr2">
          <xsl:choose>
            <xsl:when test="$NECoords = 'true'"><xsl:value-of select="$dNorthPrompt"/></xsl:when>
            <xsl:otherwise><xsl:value-of select="$dEastPrompt"/></xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Val2">
          <xsl:choose>
            <xsl:when test="$NECoords = 'true'">
              <xsl:value-of select="format-number(Stakeout/GridDeltas/DeltaNorth * $DistConvFactor, $DecPl3, 'Standard')"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="format-number(Stakeout/GridDeltas/DeltaEast * $DistConvFactor, $DecPl3, 'Standard')"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>

        <xsl:with-param name="Hdr3">
          <xsl:choose>
            <xsl:when test="$NECoords = 'true'"><xsl:value-of select="$dEastPrompt"/></xsl:when>
            <xsl:otherwise><xsl:value-of select="$dNorthPrompt"/></xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Val3">
          <xsl:choose>
            <xsl:when test="$NECoords = 'true'">
              <xsl:value-of select="format-number(Stakeout/GridDeltas/DeltaEast * $DistConvFactor, $DecPl3, 'Standard')"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="format-number(Stakeout/GridDeltas/DeltaNorth * $DistConvFactor, $DecPl3, 'Standard')"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>

        <xsl:with-param name="Hdr4" select="$dElevPrompt"/>
        <xsl:with-param name="Val4" select="format-number(Stakeout/GridDeltas/DeltaElevation * $ElevConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    <xsl:call-template name="EndTable"/>
  </xsl:if>
</xsl:template>


<!-- **************************************************************** -->
<!-- **************** Stakeout Linear Deltas Output ***************** -->
<!-- **************************************************************** -->
<xsl:template name="LinearDeltas">
  <xsl:if test="Stakeout/LinearDeltas/DeltaStation != '' or
                Stakeout/LinearDeltas/DeltaOffset != '' or 
                Stakeout/LinearDeltas/DeltaElevation != ''">
    <xsl:call-template name="StartTable"/>

      <xsl:call-template name="OutputTableLine">
        <xsl:with-param name="Hdr1">Stakeout</xsl:with-param>
        <xsl:with-param name="Val1">Deltas: Linear</xsl:with-param>

        <xsl:with-param name="Hdr2">
          <xsl:choose>
            <xsl:when test="$useChainageTerminology = 'true'">Δ Chainage</xsl:when>
            <xsl:otherwise>Δ Station</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Val2" select="format-number(Stakeout/LinearDeltas/DeltaStation * $DistConvFactor, $DecPl3, 'Standard')"/>

        <xsl:with-param name="Hdr3">ΔOffset</xsl:with-param>
        <xsl:with-param name="Val3" select="format-number(Stakeout/LinearDeltas/DeltaOffset * $DistConvFactor, $DecPl3, 'Standard')"/>

        <xsl:with-param name="Hdr4" select="$dElevPrompt"/>
        <xsl:with-param name="Val4" select="format-number(Stakeout/LinearDeltas/DeltaElevation * $ElevConvFactor, $DecPl3, 'Standard')"/>

        <xsl:with-param name="Hdr5">
          <xsl:if test="Stakeout/LinearDeltas/GradeToLine != ''">Grade to line</xsl:if>
        </xsl:with-param>
        <xsl:with-param name="Val5">
          <xsl:if test="Stakeout/LinearDeltas/GradeToLine != ''">
            <xsl:value-of select="concat(format-number(Stakeout/LinearDeltas/GradeToLine, $DecPl2, 'Standard'), '%')"/>
          </xsl:if>
        </xsl:with-param>
      </xsl:call-template>
    <xsl:call-template name="EndTable"/>
  </xsl:if>
</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** Stakeout Polar Deltas Output ***************** -->
<!-- **************************************************************** -->
<xsl:template name="PolarDeltas">
  <xsl:if test="Stakeout/PolarDeltas/Azimuth != '' or
                Stakeout/PolarDeltas/HorizontalDistance != '' or 
                Stakeout/PolarDeltas/VerticalDistance != ''">
    <xsl:call-template name="StartTable"/>

      <xsl:call-template name="OutputTableLine">
        <xsl:with-param name="Hdr1">Stakeout</xsl:with-param>
        <xsl:with-param name="Val1">Deltas: Polar</xsl:with-param>

        <xsl:with-param name="Hdr2">Azimuth</xsl:with-param>
        <xsl:with-param name="Val2">
          <xsl:call-template name="FormatAzimuth">
            <xsl:with-param name="theAzimuth" select="Stakeout/PolarDeltas/Azimuth"/>
            <xsl:with-param name="northLbl">N</xsl:with-param>
            <xsl:with-param name="eastLbl">E</xsl:with-param>
            <xsl:with-param name="southLbl">S</xsl:with-param>
            <xsl:with-param name="westLbl">W</xsl:with-param>
          </xsl:call-template>
        </xsl:with-param>
        
        <xsl:with-param name="Hdr3">H.Dist</xsl:with-param>
        <xsl:with-param name="Val3" select="format-number(Stakeout/PolarDeltas/HorizontalDistance * $DistConvFactor, $DecPl3, 'Standard')"/>

        <xsl:with-param name="Hdr4">V.Dist</xsl:with-param>
        <xsl:with-param name="Val4" select="format-number(Stakeout/PolarDeltas/VerticalDistance * $DistConvFactor, $DecPl3, 'Standard')"/>

      </xsl:call-template>
    <xsl:call-template name="EndTable"/>
  </xsl:if>
</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** Stakeout Catch Point Output ****************** -->
<!-- **************************************************************** -->
<xsl:template name="CatchPoint">
  <xsl:call-template name="StartTable"/>

    <xsl:variable name="catchPtLbl"/>
    <xsl:choose>
      <xsl:when test="Stakeout/CatchPoint/VerticalDistanceToHingePoint &gt; 0">Catch point (Fill)</xsl:when>
      <xsl:otherwise>Catch point (Cut)</xsl:otherwise>
    </xsl:choose>

    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1" select="$catchPtLbl"/>

      <xsl:with-param name="Hdr2">Offset</xsl:with-param>
      <xsl:with-param name="Val2" select="format-number(Stakeout/CatchPoint/Offset * $DistConvFactor, $DecPl3, 'Standard')"/>

      <xsl:with-param name="Hdr3" select="$elevPrompt"/>
      <xsl:with-param name="Val3" select="format-number(Stakeout/CatchPoint/Elevation * $ElevConvFactor, $DecPl3, 'Standard')"/>

      <xsl:with-param name="Hdr4">H.Dist to hinge point</xsl:with-param>
      <xsl:with-param name="Val4" select="format-number(Stakeout/CatchPoint/HorizontalDistanceToHingePoint * $DistConvFactor, $DecPl3, 'Standard')"/>

      <xsl:with-param name="Hdr5">
        <xsl:choose>
          <xsl:when test="$useRailTerminology = 'true'">Lift to hinge point</xsl:when>
          <xsl:otherwise>V.Dist to hinge point</xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Val5" select="format-number(Stakeout/CatchPoint/VerticalDistanceToHingePoint * $DistConvFactor, $DecPl3, 'Standard')"/>
    </xsl:call-template>

    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr2">Grade</xsl:with-param>
      <xsl:with-param name="Val2" select="concat(format-number(Stakeout/CatchPoint/AsStakedSideSlopeGrade, $DecPl2, 'Standard'), '%')"/>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************** Catch Point Template Report Output ************** -->
<!-- **************************************************************** -->
<xsl:template name="CatchPointTemplateRpt">
  <xsl:call-template name="StartTable"/>
    <xsl:for-each select="Stakeout/CatchPointTemplateReport">
      <xsl:call-template name="OutputTableLine">
        <xsl:with-param name="Hdr1">Catch</xsl:with-param>

        <xsl:with-param name="Hdr2">To point</xsl:with-param>
        <xsl:with-param name="Val2" select="Code"/>

        <xsl:with-param name="Hdr3">H.Dist</xsl:with-param>
        <xsl:with-param name="Val3" select="format-number(HorizontalDistance * $DistConvFactor, $DecPl3, 'Standard')"/>

        <xsl:with-param name="Hdr4">V.Dist</xsl:with-param>
        <xsl:with-param name="Val4" select="format-number(DeltaElevation * $ElevConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:for-each>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** DTM Stakeout Deltas Output ****************** -->
<!-- **************************************************************** -->
<xsl:template name="DTMDeltas">
  <xsl:call-template name="StartTable"/>
      <xsl:call-template name="OutputTableLine">
        <xsl:with-param name="Hdr1">Stakeout</xsl:with-param>
        <xsl:with-param name="Val1">DTM</xsl:with-param>

        <xsl:with-param name="Hdr2" select="$dElevPrompt"/>
        <xsl:with-param name="Val2">
          <xsl:choose>
            <xsl:when test="$schemaVersion &gt;= 5.2">
              <xsl:value-of select="format-number(Stakeout/DTMDeltas/DeltaElevation * $ElevConvFactor, $DecPl3, 'Standard')"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="format-number(Stakeout/DTMDeltas/DeltaElevation * -1 * $ElevConvFactor, $DecPl3, 'Standard')"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
    </xsl:call-template>

  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** Stakeout Method Text Output ****************** -->
<!-- **************************************************************** -->
<xsl:template name="StakeoutMethodText">
  <xsl:param name="Method"/>
  
  <xsl:choose>
    <xsl:when test="$Method = 'ToThePoint'">To the point</xsl:when>
    <xsl:when test="$Method = 'FromAPoint'">From fixed point</xsl:when>
    <xsl:when test="$Method = 'FromStartPosition'">From start position</xsl:when>
    <xsl:when test="$Method = 'FromLastPointStaked'">From last point staked</xsl:when>
    <xsl:when test="$Method = 'ToTheLine'">To the line</xsl:when>
    <xsl:when test="$Method = 'ToThePolyline'">To the polyline</xsl:when>
    <xsl:when test="$Method = 'ToTheArc'">To the arc</xsl:when>
    <xsl:when test="$Method = 'PointOffsetFromLine'">
      <xsl:choose>
        <xsl:when test="$useChainageTerminology = 'true'">Chainage/offset from line</xsl:when>
        <xsl:otherwise>Station/offset from line</xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="$Method = 'PointsOffsetFromPolyline'">
      <xsl:choose>
        <xsl:when test="$useChainageTerminology = 'true'">Chainage/offset from polyline</xsl:when>
        <xsl:otherwise>Station/offset from polyline</xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="$Method = 'PointOffsetFromArc'">
      <xsl:choose>
        <xsl:when test="$useChainageTerminology = 'true'">Chainage/offset from arc</xsl:when>
        <xsl:otherwise>Station/offset from arc</xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="$Method = 'OffsetLine'">Offset line</xsl:when>
    <xsl:when test="$Method = 'OffsetArc'">Offset arc</xsl:when>
    <xsl:when test="$Method = 'PointOnLine'">To the line</xsl:when>
    <xsl:when test="$Method = 'PointsOnPolyline'">To the polyline</xsl:when>
    <xsl:when test="$Method = 'PointOnArc'">To the arc</xsl:when>
    <xsl:when test="$Method = 'IntersectionPointOfArc'">Intersect point of arc</xsl:when>
    <xsl:when test="$Method = 'CentrePointOfArc'">Center point of arc</xsl:when>
    <xsl:when test="$Method = 'SlopeFromLine'">Slope from line</xsl:when>
    <xsl:when test="$Method = 'SlopeFromPolyline'">Slope from polyline</xsl:when>
    <xsl:when test="$Method = 'SlopeFromArc'">Slope from arc</xsl:when>
    <xsl:when test="$Method = 'StationAndOffset'">
      <xsl:choose>
        <xsl:when test="$useChainageTerminology = 'true'">Chainage and offset</xsl:when>
        <xsl:otherwise>Station and offset</xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="$Method = 'PositionOnRoad'">
      <xsl:choose>
        <xsl:when test="$useRailTerminology = 'true'">Position on railroad</xsl:when>
        <xsl:otherwise>Position on road</xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="$Method = 'SideSlopeFromAlignment'">Side slope from alignment</xsl:when>
    <xsl:when test="$Method = 'StationOnString'">
      <xsl:choose>
        <xsl:when test="$useChainageTerminology = 'true'">Chainage on string</xsl:when>
        <xsl:otherwise>Station on string</xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="$Method = 'PointSkewOffsetFromLine'">
      <xsl:choose>
        <xsl:when test="$useChainageTerminology = 'true'">Chainage/skew offset from line</xsl:when>
        <xsl:otherwise>Skew offset</xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="$Method = 'PointsSkewOffsetFromPolyline'">
      <xsl:choose>
        <xsl:when test="$useChainageTerminology = 'true'">Chainage/skew offset from polyline</xsl:when>
        <xsl:otherwise>Skew offset</xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="$Method = 'PointsSkewOffsetFromArc'">
      <xsl:choose>
        <xsl:when test="$useChainageTerminology = 'true'">Chainage/skew offset from arc</xsl:when>
        <xsl:otherwise>Skew offset</xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="$Method = 'StationSkewOffsetFromAlignment'">
      <xsl:choose>
        <xsl:when test="$useChainageTerminology = 'true'">Chainage/skew offset from alignment</xsl:when>
        <xsl:otherwise>Station/skew offset from alignment</xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="$Method = 'StationSkewOffsetFromRoad'">
      <xsl:choose>
        <xsl:when test="$useChainageTerminology = 'true'">Chainage/skew offset from road</xsl:when>
        <xsl:otherwise>Station/skew offset from road</xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="$Method = 'StationSkewOffsetFromString'">
      <xsl:choose>
        <xsl:when test="$useChainageTerminology = 'true'">Chainage/skew offset from string</xsl:when>
        <xsl:otherwise>Station/skew offset from string</xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="$Method = 'ToTheDTM'">Stake out DTM</xsl:when>
    <xsl:when test="$Method = 'DistanceAlongLine'">Distance along line</xsl:when>
    <xsl:when test="$Method = 'DistanceAlongPolyline'">Distance along polyline</xsl:when>
    <xsl:otherwise>Unknown</xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************* Line Method Text Output ******************** -->
<!-- **************************************************************** -->
<xsl:template name="LineMethodText">
  <xsl:param name="Method"/>
  
  <xsl:choose>
    <xsl:when test="$Method = 'TwoPoints'">Two points</xsl:when>
    <xsl:when test="$Method = 'BearingAndDistance'">Bearing and distance</xsl:when>
    <xsl:otherwise>Unknown</xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************** Arc Method Text Output ******************** -->
<!-- **************************************************************** -->
<xsl:template name="ArcMethodText">
  <xsl:param name="Method"/>
  
  <xsl:choose>
    <xsl:when test="$Method = 'TwoPointsAndRadius'">Two points and radius</xsl:when>
    <xsl:when test="$Method = 'ArcLengthAndRadius'">Arc length and radius</xsl:when>
    <xsl:when test="$Method = 'DeltaAngleAndRadius'">Delta angle and radius</xsl:when>
    <xsl:when test="$Method = 'IntersectionPointAndTangents'">Intersect point and tangents</xsl:when>
    <xsl:otherwise>Unknown</xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************* Slope Method Text Output ******************* -->
<!-- **************************************************************** -->
<xsl:template name="SlopeMethodText">
  <xsl:param name="Method"/>
  
  <xsl:choose>
    <xsl:when test="$Method = 'HorizontalAndVerticalDistance'">Horz. and vert. dist</xsl:when>
    <xsl:when test="$Method = 'GradeAndSlopeDistance'">Grade and slope dist</xsl:when>
    <xsl:when test="$Method = 'GradeAndHorizontalDistance'">Grade and horz. dist</xsl:when>
    <xsl:otherwise>Unknown</xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** Station Class Text Output ******************* -->
<!-- **************************************************************** -->
<xsl:template name="StationClassText">
  <xsl:param name="Class"/>
  
  <xsl:choose>
    <xsl:when test="$Class = 'StraightToSpiral'">(TS)</xsl:when>
    <xsl:when test="$Class = 'SpiralToArc'">(SC)</xsl:when>
    <xsl:when test="$Class = 'ArcToSpiral'">(CS)</xsl:when>
    <xsl:when test="$Class = 'SpiralToStraight'">(ST)</xsl:when>
    <xsl:when test="$Class = 'SpiralToSpiral'">(SS)</xsl:when>
    <xsl:when test="$Class = 'StraightToArc'">(PC)</xsl:when>
    <xsl:when test="$Class = 'ArcToStraight'">(PT)</xsl:when>
    <xsl:when test="$Class = 'IntersectionPoint'">(PI)</xsl:when>
    <xsl:when test="$Class = 'CrossSection'">(XS)</xsl:when>
    <xsl:when test="$Class = 'VerticalCurveStart'">(VCS)</xsl:when>
    <xsl:when test="$Class = 'VerticalCurveEnd'">(VCE)</xsl:when>
    <xsl:when test="$Class = 'VerticalPoint'">(VPI)</xsl:when>
    <xsl:when test="$Class = 'RoadStart'">(S)</xsl:when>
    <xsl:when test="$Class = 'RoadEnd'">(E)</xsl:when>
    <xsl:when test="$Class = 'TemplateAssignment'">(T)</xsl:when>
    <xsl:when test="$Class = 'SuperelevationStart'">(SES)</xsl:when>
    <xsl:when test="$Class = 'WideningStart'">(WS)</xsl:when>
    <xsl:when test="$Class = 'WideningEnd'">(WE)</xsl:when>
    <xsl:when test="$Class = 'SuperelevationEnd'">(SEE)</xsl:when>
    <xsl:when test="$Class = 'WideningMaxium'">(WM)</xsl:when>
    <xsl:when test="$Class = 'WideningMaximum'">(WM)</xsl:when>
    <xsl:when test="$Class = 'SuperelevationMaximum'">(SEM)</xsl:when>
    <xsl:when test="$Class = 'VerticalSagPoint'">(LO)</xsl:when>
    <xsl:when test="$Class = 'VerticalSummitPoint'">(HI)</xsl:when>
    <xsl:when test="$Class = 'SuperelevationEqual'">(SEQ)</xsl:when>
    <xsl:when test="$Class = 'SuperelevationInterest'">(SE)</xsl:when>
    <xsl:when test="$Class = 'StationEquation'">
      <xsl:choose>
        <xsl:when test="$useChainageTerminology = 'true'">(CHEQ)</xsl:when>
        <xsl:otherwise>(STEQ)</xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>Unknown</xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** Station Class Text Output ******************* -->
<!-- **************************************************************** -->
<xsl:template name="StationClassString">
  <xsl:param name="ClassStr"/>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'StraightToSpiral'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'SpiralToArc'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'ArcToSpiral'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'SpiralToStraight'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'SpiralToSpiral'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'StraightToArc'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'ArcToStraight'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'IntersectionPoint'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'CrossSection'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'VerticalCurveStart'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'VerticalCurveEnd'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'VerticalPoint'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'RoadStart'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'RoadEnd'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'TemplateAssignment'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'SuperelevationStart'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'WideningStart'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'WideningEnd'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'SuperelevationEnd'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'WideningMaxium'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'WideningMaximum'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'SuperelevationMaximum'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'VerticalSagPoint'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'VerticalSummitPoint'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'SuperelevationEqual'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'SuperelevationInterest'"/>
  </xsl:call-template>

  <xsl:call-template name="OutputStationClassItem">
    <xsl:with-param name="FullString" select="$ClassStr"/>
    <xsl:with-param name="ItemString" select="'StationEquation'"/>
  </xsl:call-template>

</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** Output Station Class Item ******************* -->
<!-- **************************************************************** -->
<xsl:template name="OutputStationClassItem">
  <xsl:param name="FullString"/>
  <xsl:param name="ItemString"/>

  <xsl:if test="contains($FullString, $ItemString)">
    <xsl:if test="not(starts-with($FullString, $ItemString))">
      <xsl:value-of select="', '"/>
    </xsl:if>
    <xsl:call-template name="StationClassText">
      <xsl:with-param name="Class" select="$ItemString"/>
    </xsl:call-template>
  </xsl:if>
  
</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** GPSEquipmentRecord Output ******************* -->
<!-- **************************************************************** -->
<xsl:template match="GPSEquipmentRecord">
  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">GNSS receiver</p></caption>
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Receiver type</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:choose>
          <xsl:when test="ReceiverType = ''">Unknown</xsl:when>
          <xsl:otherwise><xsl:value-of select="ReceiverType"/></xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Serial number</xsl:with-param>
      <xsl:with-param name="Val" select="ReceiverSerialNumber"/>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Firmware version</xsl:with-param>
      <xsl:with-param name="Val" select="ReceiverFirmwareVersion"/>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Antenna type</xsl:with-param>
      <xsl:with-param name="Val" select="AntennaType"/>
    </xsl:call-template>

    <xsl:if test="AntennaSerialNumber != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Antenna serial number</xsl:with-param>
        <xsl:with-param name="Val" select="AntennaSerialNumber"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Measurement method</xsl:with-param>
      <xsl:with-param name="Val" select="AntennaMeasurementMethod"/>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Tape adjustment</xsl:with-param>
      <xsl:with-param name="Val" select="format-number(AntennaTapeAdjustment * $DistConvFactor, $DecPl3, 'Standard')"/>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Horizontal offset</xsl:with-param>
      <xsl:with-param name="Val" select="format-number(AntennaHorizontalOffset * $DistConvFactor, $DecPl3, 'Standard')"/>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Vertical offset</xsl:with-param>
      <xsl:with-param name="Val" select="format-number(AntennaVerticalOffset * $DistConvFactor, $DecPl3, 'Standard')"/>
    </xsl:call-template>

  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- **************** CalibrationPointRecord Output ***************** -->
<!-- **************************************************************** -->
<xsl:template match="CalibrationPointRecord">
  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Calibration point</p></caption>

    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Grid</xsl:with-param>
      <xsl:with-param name="Val1" select="GridPointName"/>

      <xsl:with-param name="Hdr2">WGS84</xsl:with-param>
      <xsl:with-param name="Val2" select="WGS84PointName"/>

      <xsl:with-param name="Hdr3">Dimensions</xsl:with-param>
      <xsl:with-param name="Val3" select="Dimension"/>

      <xsl:with-param name="Hdr4">H.Resid</xsl:with-param>
      <xsl:with-param name="Val4" select="format-number(HorizontalResidual * $DistConvFactor, $DecPl3, 'Standard')"/>

      <xsl:with-param name="Hdr5">V.Resid</xsl:with-param>
      <xsl:with-param name="Val5" select="format-number(VerticalResidual * $DistConvFactor, $DecPl3, 'Standard')"/>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- *************** BaseSurveyOptionsRecord Output ***************** -->
<!-- **************************************************************** -->
<xsl:template match="BaseSurveyOptionsRecord">
  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Base options</p></caption>

    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Elevation mask</xsl:with-param>
      <xsl:with-param name="Val1" select="ElevationMask"/>

      <xsl:with-param name="Hdr2">PDOP mask</xsl:with-param>
      <xsl:with-param name="Val2" select="PDOPMask"/>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************** RoverSurveyOptionsRecord Output ***************** -->
<!-- **************************************************************** -->
<xsl:template match="RoverSurveyOptionsRecord">
  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Rover options</p></caption>

    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Elevation mask</xsl:with-param>
      <xsl:with-param name="Val1" select="ElevationMask"/>

      <xsl:with-param name="Hdr2">PDOP mask</xsl:with-param>
      <xsl:with-param name="Val2" select="PDOPMask"/>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** SurveyEventRecord Output ********************* -->
<!-- **************************************************************** -->
<xsl:template match="SurveyEventRecord">
  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Survey event</p></caption>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Survey event</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:call-template name="SurveyEventText">
          <xsl:with-param name="Event" select="Event"/>
        </xsl:call-template>
      </xsl:with-param> 
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************* Survey Event Text Output ******************* -->
<!-- **************************************************************** -->
<xsl:template name="SurveyEventText">
  <xsl:param name="Event"/>
  
  <xsl:choose>
    <xsl:when test="$Event = 'StartBaseSurvey'">Base started</xsl:when>
    <xsl:when test="$Event = 'StartRoverSurvey'">Rover started</xsl:when>
    <xsl:when test="$Event = 'AbortSurvey'">Aborting survey</xsl:when>
    <xsl:when test="$Event = 'EndSurvey'">End survey</xsl:when>
    <xsl:when test="$Event = 'BaseStarted'">Base started</xsl:when>
    <xsl:when test="$Event = 'ReceiverModeSetError'">Receiver mode setting error</xsl:when>
    <xsl:when test="$Event = 'CommunicationsError'">Communications error</xsl:when>
    <xsl:when test="$Event = 'InfillSystemAdopted'">Infill started</xsl:when>
    <xsl:when test="$Event = 'RealtimeSystemAdopted'">Infill stopped</xsl:when>
    <xsl:when test="$Event = 'StartContinuous'">Continuous topo (Start)</xsl:when>
    <xsl:when test="$Event = 'EndContinuous'">Continuous topo (End)</xsl:when>
    <xsl:otherwise>Unknown</xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************* ReferenceRecord Output ********************* -->
<!-- **************************************************************** -->
<xsl:template match="ReferenceRecord">
  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Base point</p></caption>

    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Point</xsl:with-param>
      <xsl:with-param name="Val1" select="PointName"/>

      <xsl:with-param name="Hdr2">Antenna height</xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:for-each select="key('antID-search', AntennaID)[1]">
          <xsl:value-of select="format-number(MeasuredHeight * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:for-each>
      </xsl:with-param>

      <xsl:with-param name="Hdr3">Type</xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:for-each select="key('antID-search', AntennaID)[1]">
          <xsl:choose>
            <xsl:when test="MeasurementType = 'Corrected'">Corrected</xsl:when>
            <xsl:otherwise>Uncorrected</xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>
      </xsl:with-param>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************* PostProcessRecord Output ******************* -->
<!-- **************************************************************** -->
<xsl:template match="PostProcessRecord">
  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Postprocessing file</p></caption>

    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">File name</xsl:with-param>
      <xsl:with-param name="Val1" select="FileName"/>

      <xsl:with-param name="Hdr2">Started</xsl:with-param>

      <xsl:with-param name="Hdr3">GPS week</xsl:with-param>
      <xsl:with-param name="Val3" select="StartTime/GPSWeek"/>

      <xsl:with-param name="Hdr4">Seconds</xsl:with-param>
      <xsl:with-param name="Val4" select="StartTime/Seconds"/>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** InitialisationRecord Output ****************** -->
<!-- **************************************************************** -->
<xsl:template match="InitialisationRecord">
  <xsl:call-template name="StartTable"/>
    <caption align="top">
      <p align="left">
      <xsl:text>Initialization event: </xsl:text>
      <xsl:call-template name="InitialisationEventText">
        <xsl:with-param name="Event" select="InitialisationEvent"/>
      </xsl:call-template>
      </p>
    </caption>

    <xsl:choose>
      <xsl:when test="InitialisationCounter != ''">
        <xsl:call-template name="OutputTableLine">
          <xsl:with-param name="Hdr1">GPS week</xsl:with-param>
          <xsl:with-param name="Val1" select="Time/GPSWeek"/>

          <xsl:with-param name="Hdr2">Seconds</xsl:with-param>
          <xsl:with-param name="Val2" select="Time/Seconds"/>

          <xsl:with-param name="Hdr3">Initialization type</xsl:with-param>
          <xsl:with-param name="Val3">
            <xsl:call-template name="InitialisationTypeText">
              <xsl:with-param name="Type" select="InitialisationType"/>
            </xsl:call-template>
          </xsl:with-param>

          <xsl:with-param name="Hdr4">Survey type</xsl:with-param>
          <xsl:with-param name="Val4">
            <xsl:choose>
              <xsl:when test="SurveyType = 'RealTime'">Real-time</xsl:when>
              <xsl:when test="SurveyType = 'PostProcess'">Postprocess</xsl:when>
              <xsl:otherwise>Infill</xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>

          <xsl:with-param name="Hdr5">Initialization count</xsl:with-param>
          <xsl:with-param name="Val5" select="InitialisationCounter"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="OutputTableLine">
          <xsl:with-param name="Hdr1">GPS week</xsl:with-param>
          <xsl:with-param name="Val1" select="Time/GPSWeek"/>

          <xsl:with-param name="Hdr2">Seconds</xsl:with-param>
          <xsl:with-param name="Val2" select="Time/Seconds"/>

          <xsl:with-param name="Hdr3">Initialization type</xsl:with-param>
          <xsl:with-param name="Val3">
            <xsl:call-template name="InitialisationTypeText">
              <xsl:with-param name="Type" select="InitialisationType"/>
            </xsl:call-template>
          </xsl:with-param>

          <xsl:with-param name="Hdr4">Survey type</xsl:with-param>
          <xsl:with-param name="Val4">
            <xsl:choose>
              <xsl:when test="SurveyType = 'RealTime'">Real-time</xsl:when>
              <xsl:when test="SurveyType = 'PostProcess'">Postprocess</xsl:when>
              <xsl:otherwise>Infill</xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- *************** Initialisation Event Text Output *************** -->
<!-- **************************************************************** -->
<xsl:template name="InitialisationEventText">
  <xsl:param name="Event"/>
  
  <xsl:choose>
    <xsl:when test="$Event = 'GainedInitialisation'">Gained</xsl:when>
    <xsl:when test="$Event = 'LostInitialisation'">Lost</xsl:when>
    <xsl:when test="$Event = 'FailedInitialisation'">Failed</xsl:when>
    <xsl:when test="$Event = 'BadInitialisation'">Failed</xsl:when>
    <xsl:when test="$Event = 'HighRMS'">High RMS</xsl:when>
    <xsl:when test="$Event = 'GoodRMS'">Good RMS</xsl:when>
    <xsl:when test="$Event = 'UserCancelled'">User cancelled</xsl:when>
    <xsl:when test="$Event = 'RTKNotInitialised'">RTK not initialized</xsl:when>
    <xsl:when test="$Event = 'RTKInitialised'">RTK initialized</xsl:when>
    <xsl:when test="$Event = 'RTXConverged'">RTX converged</xsl:when>
    <xsl:when test="$Event = 'RTXNotConverged'">RTX not converged</xsl:when>
    <xsl:otherwise>Unknown</xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- *************** Initialisation Type Text Output **************** -->
<!-- **************************************************************** -->
<xsl:template name="InitialisationTypeText">
  <xsl:param name="Type"/>
  
  <xsl:choose>
    <xsl:when test="$Type = 'Plate'">Plate</xsl:when>
    <xsl:when test="$Type = 'KnownPoint'">Known point</xsl:when>
    <xsl:when test="$Type = 'NewPoint'">New point</xsl:when>
    <xsl:when test="$Type = 'OnTheFly'">On the fly</xsl:when>
    <xsl:when test="$Type = 'ResetRTK'">Reset RTK</xsl:when>
    <xsl:when test="$Type = 'ResetSVTracking'">Reset SV tracking</xsl:when>
    <xsl:when test="$Type = 'Null'">Null</xsl:when>
    <xsl:otherwise>Unknown</xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** AtmosphereRecord Output ********************* -->
<!-- **************************************************************** -->
<xsl:template match="AtmosphereRecord">
  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Atmosphere</p></caption>

    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Pressure</xsl:with-param>
      <xsl:with-param name="Val1">
        <xsl:variable name="Press" select="Pressure" />
        <xsl:choose>
          <xsl:when test="string(number($Press))='NaN'"><xsl:value-of select="string('?')"/></xsl:when> <!-- could use &#160; for space -->
          <xsl:otherwise>
            <xsl:variable name="PUnit">
              <xsl:choose>
                <xsl:when test="$PressUnit='mmHg'">mmHg</xsl:when>
                <xsl:when test="$PressUnit='InchHg'">inHg</xsl:when>
                <xsl:otherwise>mbar</xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
          <xsl:value-of select="concat(format-number($Press * $PressConvFactor, $DecPl2, 'Standard'), $PUnit)"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>

      <xsl:with-param name="Hdr2">Temperature</xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:variable name="Temp" select="Temperature" />
        <xsl:choose>
          <xsl:when test="string(number($Temp))='NaN'"><xsl:value-of select="string('?')"/></xsl:when> <!-- could use &#160; for space -->
          <xsl:otherwise>
            <xsl:variable name="TUnit">
              <xsl:choose>
                <xsl:when test="$TempUnit='Fahrenheit'">&#0176;F</xsl:when>
                <xsl:otherwise>&#0176;C</xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
            <xsl:choose>
              <xsl:when test="$TempUnit='Fahrenheit'">
                <xsl:value-of select="concat(format-number($Temp * 1.8 + 32, $DecPl1, 'Standard'), $TUnit)"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="concat(format-number($Temp, $DecPl1, 'Standard'), $TUnit)"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>

      <xsl:with-param name="Hdr3">ppm</xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:choose>
          <xsl:when test="PPM = ''">&#160;</xsl:when>
          <xsl:otherwise><xsl:value-of select="format-number(PPM, $DecPl1, 'Standard')"/></xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Curvature correction</xsl:with-param>
      <xsl:with-param name="Val1">
        <xsl:choose>
          <xsl:when test="ApplyEarthCurvatureCorrection = 'true'">Yes</xsl:when>
          <xsl:otherwise>No</xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Hdr2">Refraction correction</xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:choose>
          <xsl:when test="ApplyRefractionCorrection = 'true'">Yes</xsl:when>
          <xsl:otherwise>No</xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Hdr3">Refraction const.</xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:choose>
          <xsl:when test="(ApplyRefractionCorrection = 'false') or (RefractionCoefficient = '')">&#160;</xsl:when>
          <xsl:otherwise><xsl:value-of select="RefractionCoefficient"/></xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************** StationRecord Output ********************** -->
<!-- **************************************************************** -->
<xsl:template match="StationRecord">
  <!-- Only output the station details if it is not a Scan station setup (no station position or orientation) -->
  <xsl:if test="StationType != 'ScanStation'">
    <xsl:call-template name="StartTable"/>
      <caption align="top"><p align="left">Station setup</p></caption>

      <xsl:call-template name="OutputTableLine">
        <xsl:with-param name="Hdr1">Station</xsl:with-param>
        <xsl:with-param name="Val1" select="StationName"/>
        <xsl:with-param name="Hdr2">Instrument height</xsl:with-param>
        <xsl:with-param name="Val2" select="format-number(TheodoliteHeight * $DistConvFactor, $DecPl3, 'Standard')"/>
        <xsl:with-param name="Hdr3">Station type</xsl:with-param>
        <xsl:with-param name="Val3">
          <xsl:if test="StationType[.='StationSetup']">Station setup</xsl:if>
          <xsl:if test="StationType[.='StationSetupPlus']">Station setup plus</xsl:if>
          <xsl:if test="StationType[.='StandardResection']">Resection (Standard)</xsl:if>
          <xsl:if test="StationType[.='HelmertResection']">Resection (Helmert)</xsl:if>
          <xsl:if test="StationType[.='ReflineStationSetup']">Refline</xsl:if>
          <xsl:if test="StationType[.='StationElevationSetup']">Station elevation</xsl:if>
        </xsl:with-param>
        <xsl:with-param name="Hdr4">Scale factor</xsl:with-param>
        <xsl:with-param name="Val4" select="format-number(ScaleFactor, $DecPl8, 'Standard')"/>
        <xsl:with-param name="Hdr5">Std Error</xsl:with-param>
        <xsl:with-param name="Val5" select="format-number(ScaleFactorStandardError, $DecPl8, 'Standard')"/>
      </xsl:call-template>
    <xsl:call-template name="EndTable"/>
    <xsl:call-template name="SeparatingLine"/>
  </xsl:if>
</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** BackBearingRecord Output ********************* -->
<!-- **************************************************************** -->
<xsl:template match="BackBearingRecord">
  <!-- Only output the orientation details if it is not a Scan station setup (no station position or orientation) -->
  <xsl:variable name="stnID" select="StationRecordID"/>
  <xsl:if test="/JOBFile/FieldBook/StationRecord[@ID = $stnID]/StationType != 'ScanStation'">
    <xsl:call-template name="StartTable"/>
      <caption align="top"><p align="left">Orientation</p></caption>

      <xsl:variable name="F2ObsAsF1">  <!-- Convert the F2 observation (if present) to the equivalent F1 value -->
        <xsl:if test="string(number(Face2HorizontalCircle)) != 'NaN'">
          <xsl:variable name="initialVal">
            <xsl:call-template name="NormalisedAngle">
              <xsl:with-param name="angle" select="Face2HorizontalCircle - 180.0"/>
            </xsl:call-template>
          </xsl:variable>

          <xsl:choose>  <!-- Check for the situation where the resulting 2 'F1' values straddle 360°/0° value -->
            <xsl:when test="(string(number(Face1HorizontalCircle)) != 'NaN') and
                            concat(substring('-',2 - (($initialVal - Face1HorizontalCircle) &lt; 0)), '1') * ($initialVal - Face1HorizontalCircle) &gt; 180.0">
              <xsl:choose>
                <xsl:when test="$initialVal &gt; Face1HorizontalCircle">
                  <xsl:value-of select="$initialVal - 360.0"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="$initialVal + 360.0"/>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$initialVal"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:if>
      </xsl:variable>

      <xsl:variable name="halfDeltaAngleBetweenFaces">
        <xsl:choose>
          <xsl:when test="(string(number(Face1HorizontalCircle)) != 'NaN') and
                        (string(number($F2ObsAsF1)) != 'NaN')">
            <xsl:value-of select="(Face1HorizontalCircle - $F2ObsAsF1) div 2.0"/>
          </xsl:when>
          <xsl:otherwise>0.0</xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <xsl:call-template name="OutputTableLine">
        <xsl:with-param name="Hdr1">Station</xsl:with-param>
        <xsl:with-param name="Val1" select="Station"/>

        <xsl:with-param name="Hdr2">Backsight point</xsl:with-param>
        <xsl:with-param name="Val2" select="BackSight"/>

        <xsl:with-param name="Hdr3">F1 Orientation correction</xsl:with-param>
        <xsl:with-param name="Val3">
          <xsl:choose>
            <xsl:when test="string(number(Face1HorizontalCircle)) != 'NaN'">
              <xsl:call-template name="FormatAngle">
                <xsl:with-param name="theAngle">
                  <xsl:choose>   <!-- Avoid writing out negative zero values -->
                    <xsl:when test="concat(substring('-',2 - (OrientationCorrection - $halfDeltaAngleBetweenFaces &lt; 0)), '1') * (OrientationCorrection - $halfDeltaAngleBetweenFaces) &lt; 0.000000001">0.0</xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="OrientationCorrection - $halfDeltaAngleBetweenFaces"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:with-param>
                <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name="FormatAngle">
                <xsl:with-param name="theAngle" select="'NaN'"/>
              </xsl:call-template>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>

        <xsl:with-param name="Hdr4">F2 Orientation correction</xsl:with-param>
        <xsl:with-param name="Val4">
          <xsl:choose>
            <xsl:when test="string(number($F2ObsAsF1)) != 'NaN'">
              <xsl:call-template name="FormatAngle">
                <xsl:with-param name="theAngle">
                  <xsl:choose>   <!-- Avoid writing out negative zero values -->
                    <xsl:when test="concat(substring('-',2 - (OrientationCorrection + $halfDeltaAngleBetweenFaces &lt; 0)), '1') * (OrientationCorrection + $halfDeltaAngleBetweenFaces) &lt; 0.000000001">0.0</xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="OrientationCorrection + $halfDeltaAngleBetweenFaces"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:with-param>
                <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name="FormatAngle">
                <xsl:with-param name="theAngle" select="'NaN'"/>
              </xsl:call-template>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>

        <xsl:with-param name="Hdr5">Orient. Std Err</xsl:with-param>
        <xsl:with-param name="Val5">
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="OrientationCorrectionStandardError"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    <xsl:call-template name="EndTable"/>
    <xsl:call-template name="SeparatingLine"/>
  </xsl:if>
</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** Station Residuals Output ******************** -->
<!-- **************************************************************** -->
<xsl:template match="StationResiduals">
  <xsl:call-template name="SeparatingLine"/>
  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Residuals (Station)</p></caption>
  <xsl:call-template name="EndTable"/>
  <xsl:apply-templates select="ResidualsRecord"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** Residual Details Output ********************* -->
<!-- **************************************************************** -->
<xsl:template match="ResidualsRecord">
  <xsl:call-template name="StartTable"/>
    <!-- First line contains the grid residuals -->
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Point</xsl:with-param>
      <xsl:with-param name="Val1" select="PointName"/>

      <xsl:with-param name="Hdr2">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'">ΔN</xsl:when>
          <xsl:otherwise>ΔE</xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'">
            <xsl:value-of select="format-number(GridResidual/DeltaNorth * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number(GridResidual/DeltaEast * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>

      <xsl:with-param name="Hdr3">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'">ΔE</xsl:when>
          <xsl:otherwise>ΔN</xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'">
            <xsl:value-of select="format-number(GridResidual/DeltaEast * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number(GridResidual/DeltaNorth * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>

      <xsl:with-param name="Hdr4" select="$dElevPrompt"/>
      <xsl:with-param name="Val4" select="format-number(GridResidual/DeltaElevation * $ElevConvFactor, $DecPl3, 'Standard')"/>

      <xsl:with-param name="Hdr5">Used for</xsl:with-param>
      <xsl:with-param name="Val5">
        <xsl:choose>
          <xsl:when test="ObservationUsedInSolutionFor = 'VerticalOnly'">Vertical only</xsl:when>
          <xsl:when test="ObservationUsedInSolutionFor = 'HorizontalOnly'">Horizontal only</xsl:when>
          <xsl:when test="ObservationUsedInSolutionFor = 'HorizontalAndVertical'">Horizontal+Vertical</xsl:when>
          <xsl:otherwise>Unused</xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
    </xsl:call-template>

    <!-- Second line has the angular and distance residuals -->
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr2">ΔHA</xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle" select="AngleResidual/HorizontalCircle"/>
          <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
        </xsl:call-template>
      </xsl:with-param>

      <xsl:with-param name="Hdr3">ΔVA</xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle" select="AngleResidual/VerticalCircle"/>
          <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
        </xsl:call-template>
      </xsl:with-param>

      <xsl:with-param name="Hdr4">ΔSD</xsl:with-param>
      <xsl:with-param name="Val4">
        <xsl:value-of select="format-number(AngleResidual/SlopeDistance * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:with-param>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************* Rounds Residuals Output ******************** -->
<!-- **************************************************************** -->
<xsl:template match="RoundsMeanTurnedAngleResiduals">
  <xsl:variable name="angleFactor">
    <xsl:choose> <!-- Residual angle values in JobXML files from Survey Controllers v12.10 and earlier were incorrectly in radians -->
      <xsl:when test="contains($prodVersion, 'Trimble Survey Controller') and
                      (number(substring($prodVersion, string-length($prodVersion) - 4, 5)) &lt;= 12.10)">
        <xsl:value-of select="180.0 div $Pi"/>
      </xsl:when>
      <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:call-template name="SeparatingLine"/>
  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Residuals (Rounds)</p></caption>
  <xsl:for-each select="MeanTurnedAngleResiduals">
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Point</xsl:with-param>
      <xsl:with-param name="Val1" select="PointName"/>
    
      <xsl:with-param name="Hdr2">Round</xsl:with-param>
      <xsl:with-param name="Val2" select="Round"/>

      <xsl:with-param name="Hdr3">ΔHA</xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle" select="HorizontalAngleResidual * $angleFactor"/>
          <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
        </xsl:call-template>
      </xsl:with-param>

      <xsl:with-param name="Hdr4">ΔVA</xsl:with-param>
      <xsl:with-param name="Val4">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle" select="VerticalAngleResidual * $angleFactor"/>
          <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
        </xsl:call-template>
      </xsl:with-param>

      <xsl:with-param name="Hdr5">ΔSD</xsl:with-param>
      <xsl:with-param name="Val5">
        <xsl:value-of select="format-number(SlopeDistanceResidual * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:for-each>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ********************* Road Record Output *********************** -->
<!-- **************************************************************** -->
<xsl:template match="RoadRecord">
  <xsl:if test="Deleted = 'false'">  <!-- Road definition has not been deleted -->
    <xsl:call-template name="StartTable"/>
      <caption align="top"><p align="left">Road</p></caption>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">
          <xsl:choose>
            <xsl:when test="$useRailTerminology = 'true'">Railroad name</xsl:when>
            <xsl:otherwise>Road name</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Val" select="Name"/>
      </xsl:call-template>
    <xsl:call-template name="EndTable"/>
    <xsl:call-template name="SeparatingLine"/>
  </xsl:if>  
</xsl:template>


<!-- **************************************************************** -->
<!-- ********************* Note Record Output *********************** -->
<!-- **************************************************************** -->
<xsl:template match="NoteRecord">
  <xsl:call-template name="StartTable"/>

  <xsl:for-each select="Notes/Note">
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Note</xsl:with-param>
      <xsl:with-param name="Val" select="."/>
    </xsl:call-template>
  </xsl:for-each>

  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ********************* Line Record Output *********************** -->
<!-- **************************************************************** -->
<xsl:template match="LineRecord">
  <xsl:if test="Deleted = 'false'">  <!-- Line definition has not been deleted -->
    <xsl:call-template name="StartTable"/>
      <caption align="top"><p align="left">Line</p></caption>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Line</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:text>Name: </xsl:text><xsl:value-of select="Name"/>
          <xsl:text> Code: </xsl:text><xsl:value-of select="Code"/>
        </xsl:with-param>
      </xsl:call-template>

      <!-- Output any descriptions assigned to the line -->
      <xsl:call-template name="OutputDescriptions">
        <xsl:with-param name="lineArc" select="'true'"/>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Definition</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:call-template name="LineMethodText">
            <xsl:with-param name="Method" select="Method"/>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>

      <xsl:if test="StartPoint and StartPoint != ''">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Start point</xsl:with-param>
          <xsl:with-param name="Val" select="StartPoint"/>
        </xsl:call-template>
      </xsl:if>

      <xsl:if test="EndPoint and EndPoint != ''">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">End point</xsl:with-param>
          <xsl:with-param name="Val" select="EndPoint"/>
        </xsl:call-template>
      </xsl:if>

      <xsl:if test="Azimuth and Azimuth != ''">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Azimuth</xsl:with-param>
          <xsl:with-param name="Val">
            <xsl:call-template name="FormatAzimuth">
              <xsl:with-param name="theAzimuth" select="Azimuth"/>
              <xsl:with-param name="northLbl">N</xsl:with-param>
              <xsl:with-param name="eastLbl">E</xsl:with-param>
              <xsl:with-param name="southLbl">S</xsl:with-param>
              <xsl:with-param name="westLbl">W</xsl:with-param>
            </xsl:call-template>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:if>

      <xsl:if test="Length and Length != ''">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Length</xsl:with-param>
          <xsl:with-param name="Val" select="format-number(Length * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:call-template>
      </xsl:if>

      <xsl:if test="Grade and Grade != ''">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Grade</xsl:with-param>
          <xsl:with-param name="Val" select="concat(format-number(Grade, $DecPl2, 'Standard'), '%')"/>
        </xsl:call-template>
      </xsl:if>

      <xsl:if test="StartStation and StartStation != ''">
        <xsl:variable name="startStn">
          <xsl:call-template name="FormatStationVal">
            <xsl:with-param name="stationVal" select="StartStation"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">
            <xsl:choose>
              <xsl:when test="$useChainageTerminology = 'true'">Chainage</xsl:when>
              <xsl:otherwise>Station details</xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>
          <xsl:with-param name="Val">
            <xsl:choose>
              <xsl:when test="$useChainageTerminology = 'true'">Start chainage: </xsl:when>
              <xsl:otherwise>Start station: </xsl:otherwise>
            </xsl:choose>
            <xsl:value-of select="$startStn"/>
            <xsl:choose>
              <xsl:when test="$useChainageTerminology = 'true'"> Chainage interval: </xsl:when>
              <xsl:otherwise> Station interval: </xsl:otherwise>
            </xsl:choose>
            <xsl:value-of select="format-number(StationInterval * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:if>
    <xsl:call-template name="EndTable"/>
  </xsl:if>  
</xsl:template>


<!-- **************************************************************** -->
<!-- ********************** Arc Record Output *********************** -->
<!-- **************************************************************** -->
<xsl:template match="ArcRecord">
  <xsl:if test="Deleted = 'false'">  <!-- Arc definition has not been deleted -->
    <xsl:call-template name="StartTable"/>
      <caption align="top"><p align="left">Arc</p></caption>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Arc</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:text>Name: </xsl:text><xsl:value-of select="Name"/>
          <xsl:text> Code: </xsl:text><xsl:value-of select="Code"/>
        </xsl:with-param>
      </xsl:call-template>

      <!-- Output any descriptions assigned to the arc -->
      <xsl:call-template name="OutputDescriptions">
        <xsl:with-param name="lineArc" select="'true'"/>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Definition</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:call-template name="ArcMethodText">
            <xsl:with-param name="Method" select="Method"/>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>

      <xsl:if test="StartPoint and StartPoint != ''">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Start point</xsl:with-param>
          <xsl:with-param name="Val" select="StartPoint"/>
        </xsl:call-template>
      </xsl:if>

      <xsl:if test="EndPoint and EndPoint != ''">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">End point</xsl:with-param>
          <xsl:with-param name="Val" select="EndPoint"/>
        </xsl:call-template>
      </xsl:if>

      <xsl:if test="Direction and Direction != ''">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Direction</xsl:with-param>
          <xsl:with-param name="Val" select="Direction"/>
        </xsl:call-template>
      </xsl:if>

      <xsl:if test="Radius and Radius != ''">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Radius</xsl:with-param>
          <xsl:with-param name="Val" select="format-number(Radius * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:call-template>
      </xsl:if>

      <xsl:if test="StartAzimuth and StartAzimuth != ''">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Start azimuth</xsl:with-param>
          <xsl:with-param name="Val">
            <xsl:call-template name="FormatAzimuth">
              <xsl:with-param name="theAzimuth" select="StartAzimuth"/>
              <xsl:with-param name="northLbl">N</xsl:with-param>
              <xsl:with-param name="eastLbl">E</xsl:with-param>
              <xsl:with-param name="southLbl">S</xsl:with-param>
              <xsl:with-param name="westLbl">W</xsl:with-param>
            </xsl:call-template>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:if>

      <xsl:if test="Length and Length != ''">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Length</xsl:with-param>
          <xsl:with-param name="Val" select="format-number(Length * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:call-template>
      </xsl:if>

      <xsl:if test="Grade and Grade != ''">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Grade</xsl:with-param>
          <xsl:with-param name="Val" select="concat(format-number(Grade, $DecPl2, 'Standard'), '%')"/>
        </xsl:call-template>
      </xsl:if>

      <xsl:if test="DeltaAngle and DeltaAngle != ''">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Delta angle</xsl:with-param>
          <xsl:with-param name="Val">
            <xsl:call-template name="FormatAngle">
              <xsl:with-param name="theAngle" select="DeltaAngle"/>
              <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
            </xsl:call-template>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:if>

      <xsl:if test="IntersectionPoint and IntersectionPoint != ''">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Intersect point</xsl:with-param>
          <xsl:with-param name="Val" select="IntersectionPoint"/>
        </xsl:call-template>
      </xsl:if>

      <xsl:if test="EndAzimuth and EndAzimuth != ''">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">End azimuth</xsl:with-param>
          <xsl:with-param name="Val">
            <xsl:call-template name="FormatAzimuth">
              <xsl:with-param name="theAzimuth" select="EndAzimuth"/>
              <xsl:with-param name="northLbl">N</xsl:with-param>
              <xsl:with-param name="eastLbl">E</xsl:with-param>
              <xsl:with-param name="southLbl">S</xsl:with-param>
              <xsl:with-param name="westLbl">W</xsl:with-param>
            </xsl:call-template>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:if>

      <xsl:if test="StartStation and StartStation != ''">
        <xsl:variable name="startStn">
          <xsl:call-template name="FormatStationVal">
            <xsl:with-param name="stationVal" select="StartStation"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">
            <xsl:choose>
              <xsl:when test="$useChainageTerminology = 'true'">Chainage</xsl:when>
              <xsl:otherwise>Station details</xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>
          <xsl:with-param name="Val">
            <xsl:choose>
              <xsl:when test="$useChainageTerminology = 'true'">Start chainage: </xsl:when>
              <xsl:otherwise>Start station: </xsl:otherwise>
            </xsl:choose>
            <xsl:value-of select="$startStn"/>
            <xsl:choose>
              <xsl:when test="$useChainageTerminology = 'true'"> Chainage interval: </xsl:when>
              <xsl:otherwise> Station interval: </xsl:otherwise>
            </xsl:choose>
            <xsl:value-of select="format-number(StationInterval * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:if>
    <xsl:call-template name="EndTable"/>
  </xsl:if>  
</xsl:template>


<!-- **************************************************************** -->
<!-- ********************* Inverse Record Output ******************** -->
<!-- **************************************************************** -->
<xsl:template match="InverseRecord">
  <xsl:variable name="AzLbl">
    <xsl:choose>
      <xsl:when test="AzimuthType[.='Grid']">Azimuth (grid)</xsl:when>
      <xsl:when test="AzimuthType[.='Magnetic']">Azimuth (mag)</xsl:when>
      <xsl:when test="AzimuthType[.='Local']">Azimuth (local)</xsl:when>
      <xsl:when test="AzimuthType[.='WGS84']">Azimuth (WGS84)</xsl:when>
      <xsl:otherwise>Azimuth</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="DistType">
    <xsl:choose>
      <xsl:when test="DistanceType[.='Grid']">Grid</xsl:when>
      <xsl:when test="DistanceType[.='Ground']">Ground</xsl:when>
      <xsl:when test="DistanceType[.='Ellipsoid']">Ellipsoid</xsl:when>
      <xsl:otherwise>Grid</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Inverse</p></caption>
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">From point</xsl:with-param>
      <xsl:with-param name="Val1" select="FromPoint"/>

      <xsl:with-param name="Hdr2">To point</xsl:with-param>
      <xsl:with-param name="Val2" select="ToPoint"/>

      <xsl:with-param name="Hdr3"><xsl:value-of select="$AzLbl"/></xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:call-template name="FormatAzimuth">
          <xsl:with-param name="theAzimuth" select="Azimuth"/>
          <xsl:with-param name="northLbl">N</xsl:with-param>
          <xsl:with-param name="eastLbl">E</xsl:with-param>
          <xsl:with-param name="southLbl">S</xsl:with-param>
          <xsl:with-param name="westLbl">W</xsl:with-param>
        </xsl:call-template>
      </xsl:with-param>

      <xsl:with-param name="Hdr4">H.Dist</xsl:with-param>
      <xsl:with-param name="Val4" select="format-number(HorizontalDistance * $DistConvFactor, $DecPl3, 'Standard')"/>

      <xsl:with-param name="Hdr5">V.Dist</xsl:with-param>
      <xsl:with-param name="Val5" select="format-number(VerticalDistance * $DistConvFactor, $DecPl3, 'Standard')"/>
    </xsl:call-template>

    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr3">Slope dist</xsl:with-param>
      <xsl:with-param name="Val3" select="format-number(SlopeDistance * $DistConvFactor, $DecPl3, 'Standard')"/>

      <xsl:with-param name="Hdr4">Grade</xsl:with-param>
      <xsl:with-param name="Val4" select="concat(format-number(Grade, $DecPl2, 'Standard'), '%')"/>

      <xsl:with-param name="Hdr5">Distance</xsl:with-param>
      <xsl:with-param name="Val5"><xsl:value-of select="$DistType"/></xsl:with-param>

    </xsl:call-template>

  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** ProjectionRecord Output ********************* -->
<!-- **************************************************************** -->
<xsl:template match="ProjectionRecord">
  
  <xsl:call-template name="ProjDetailsOutput"/>

  <xsl:call-template name="LocalSiteOutput"/>
 
</xsl:template>


<!-- **************************************************************** -->
<!-- *************** CoordinateSystem Details Output **************** -->
<!-- **************************************************************** -->
<xsl:template match="CoordinateSystem">

  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Coordinate system (Job)</p></caption>
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">System</xsl:with-param>
        <xsl:with-param name="Val" select="SystemName"/>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Zone</xsl:with-param>
        <xsl:with-param name="Val" select="ZoneName"/>
      </xsl:call-template>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Datum</xsl:with-param>
        <xsl:with-param name="Val" select="DatumName"/>
      </xsl:call-template>
  <xsl:call-template name="EndTable"/>

  <xsl:call-template name="ProjDetailsOutput"/>

  <xsl:call-template name="LocalSiteOutput"/>
  
  <xsl:apply-templates select="Datum"/>

  <xsl:apply-templates select="HorizontalAdjustment"/>

  <xsl:apply-templates select="VerticalAdjustment"/>

  <xsl:call-template name="SeparatingLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************** Datum Details Output ********************** -->
<!-- **************************************************************** -->
<xsl:template match="Datum">

  <xsl:call-template name="DatumTransformationOutput"/>

</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** DatumRecord Details Output ******************* -->
<!-- **************************************************************** -->
<xsl:template match="DatumRecord">

  <xsl:call-template name="DatumTransformationOutput"/>

</xsl:template>


<!-- **************************************************************** -->
<!-- ************* HorizontalAdjustment Details Output ************** -->
<!-- **************************************************************** -->
<xsl:template match="HorizontalAdjustment">

  <xsl:call-template name="HorizontalAdjustmentOutput"/>

</xsl:template>


<!-- **************************************************************** -->
<!-- ********** HorizontalAdjustmentRecord Details Output *********** -->
<!-- **************************************************************** -->
<xsl:template match="HorizontalAdjustmentRecord">

  <xsl:call-template name="HorizontalAdjustmentOutput"/>

</xsl:template>


<!-- **************************************************************** -->
<!-- ************** VerticalAdjustment Details Output *************** -->
<!-- **************************************************************** -->
<xsl:template match="VerticalAdjustment">

  <xsl:call-template name="VerticalAdjustmentOutput"/>

</xsl:template>


<!-- **************************************************************** -->
<!-- *********** VerticalAdjustmentRecord Details Output ************ -->
<!-- **************************************************************** -->
<xsl:template match="VerticalAdjustmentRecord">

  <xsl:call-template name="VerticalAdjustmentOutput"/>

</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** ProjectionRecord Output ********************* -->
<!-- **************************************************************** -->
<xsl:template name="ProjDetailsOutput">
  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Projection</p></caption>
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Projection</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:call-template name="ProjectionTypeText">
          <xsl:with-param name="Type" select="Projection/Type"/>
        </xsl:call-template>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:if test="Projection/CentralLatitude">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Origin lat</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:call-template name="FormatLatLong">
            <xsl:with-param name="theAngle" select="Projection/CentralLatitude"/>
            <xsl:with-param name="northLbl">N</xsl:with-param>
            <xsl:with-param name="eastLbl">E</xsl:with-param>
            <xsl:with-param name="southLbl">S</xsl:with-param>
            <xsl:with-param name="westLbl">W</xsl:with-param>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
    
    <xsl:if test="Projection/CentralLongitude">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Origin long</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:call-template name="FormatLatLong">
            <xsl:with-param name="theAngle" select="Projection/CentralLongitude"/>
            <xsl:with-param name="isLat" select="'false'"/>
            <xsl:with-param name="northLbl">N</xsl:with-param>
            <xsl:with-param name="eastLbl">E</xsl:with-param>
            <xsl:with-param name="southLbl">S</xsl:with-param>
            <xsl:with-param name="westLbl">W</xsl:with-param>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
    
    <xsl:if test="Projection/OriginHeight">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Origin height</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Projection/OriginHeight * $ElevConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:choose>
      <xsl:when test="$NECoords = 'true'">
        <xsl:if test="Projection/FalseNorthing">
          <xsl:call-template name="OutputSingleElementTableLine">
            <xsl:with-param name="Hdr">False northing</xsl:with-param>
            <xsl:with-param name="Val" select="format-number(Projection/FalseNorthing * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:call-template>
        </xsl:if>

        <xsl:if test="Projection/FalseEasting">
          <xsl:call-template name="OutputSingleElementTableLine">
            <xsl:with-param name="Hdr">False easting</xsl:with-param>
            <xsl:with-param name="Val" select="format-number(Projection/FalseEasting * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:call-template>
        </xsl:if>
      </xsl:when>
      <xsl:otherwise>
        <xsl:if test="Projection/FalseEasting">
          <xsl:call-template name="OutputSingleElementTableLine">
            <xsl:with-param name="Hdr">False easting</xsl:with-param>
            <xsl:with-param name="Val" select="format-number(Projection/FalseEasting * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:call-template>
        </xsl:if>

        <xsl:if test="Projection/FalseNorthing">
          <xsl:call-template name="OutputSingleElementTableLine">
            <xsl:with-param name="Hdr">False northing</xsl:with-param>
            <xsl:with-param name="Val" select="format-number(Projection/FalseNorthing * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:call-template>
        </xsl:if>
      </xsl:otherwise>
    </xsl:choose>
    
    <xsl:if test="Projection/OriginElevation">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">False elevation</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Projection/OriginElevation * $ElevConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Projection/Parallel1">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Parallel 1</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:call-template name="FormatLatLong">
            <xsl:with-param name="theAngle" select="Projection/Parallel1"/>
            <xsl:with-param name="northLbl">N</xsl:with-param>
            <xsl:with-param name="eastLbl">E</xsl:with-param>
            <xsl:with-param name="southLbl">S</xsl:with-param>
            <xsl:with-param name="westLbl">W</xsl:with-param>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
    
    <xsl:if test="Projection/Parallel2">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Parallel 2</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:call-template name="FormatLatLong">
            <xsl:with-param name="theAngle" select="Projection/Parallel2"/>
            <xsl:with-param name="northLbl">N</xsl:with-param>
            <xsl:with-param name="eastLbl">E</xsl:with-param>
            <xsl:with-param name="southLbl">S</xsl:with-param>
            <xsl:with-param name="westLbl">W</xsl:with-param>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
    
    <xsl:if test="Projection/Scale">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Scale</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(Projection/Scale, $DecPl8, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Projection/Rotation">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Rotation</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="Projection/Rotation"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
    
    <xsl:if test="Projection/Azimuth">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Azimuth</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:call-template name="FormatAzimuth">
            <xsl:with-param name="theAzimuth" select="Projection/Azimuth"/>
            <xsl:with-param name="northLbl">N</xsl:with-param>
            <xsl:with-param name="eastLbl">E</xsl:with-param>
            <xsl:with-param name="southLbl">S</xsl:with-param>
            <xsl:with-param name="westLbl">W</xsl:with-param>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
    
    <xsl:if test="Projection/AzimuthAt">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Azimuth at</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:if test="Projection/AzimuthAt[.='Equator']">Equator</xsl:if>
          <xsl:if test="Projection/AzimuthAt[.='ProjectionCentre']">Center of projection</xsl:if>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Projection/OriginAt">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Origin at</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:if test="Projection/OriginAt[.='Equator']">Equator</xsl:if>
          <xsl:if test="Projection/OriginAt[.='ProjectionCentre']">Center of projection</xsl:if>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Projection/Rectify">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Rectify coordinates</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:if test="Projection/Rectify[.='true']">Yes</xsl:if>
          <xsl:if test="Projection/Rectify[.='false']">No</xsl:if>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Projection/FerroConstant">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Ferro constant</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:call-template name="FormatLatLong">
            <xsl:with-param name="theAngle" select="Projection/FerroConstant"/>
            <xsl:with-param name="isLat" select="'false'"/>
            <xsl:with-param name="northLbl">N</xsl:with-param>
            <xsl:with-param name="eastLbl">E</xsl:with-param>
            <xsl:with-param name="southLbl">S</xsl:with-param>
            <xsl:with-param name="westLbl">W</xsl:with-param>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Projection/Area">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Denmark</xsl:with-param>
        <xsl:with-param name="Val" select="Projection/Area"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Projection/ShiftGridFileName and 
                  Projection/ShiftGridFileName != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Shift grid file</xsl:with-param>
        <xsl:with-param name="Val" select="Projection/ShiftGridFileName"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Projection/ProjectionGridFile and
                  Projection/ProjectionGridFile != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Projection grid file</xsl:with-param>
        <xsl:with-param name="Val" select="Projection/ProjectionGridFile"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Projection/NorthGridFile">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Shift grid file (North)</xsl:with-param>
        <xsl:with-param name="Val" select="Projection/NorthGridFile"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Projection/EastGridFile">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Shift grid file (East)</xsl:with-param>
        <xsl:with-param name="Val" select="Projection/EastGridFile"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Projection/SouthAzimuth">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">South azimuth (grid)</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:if test="Projection/SouthAzimuth[.='true']">Yes</xsl:if>
          <xsl:if test="Projection/SouthAzimuth[.='false']">No</xsl:if>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Projection/GridOrientation">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Grid coords</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:if test="Projection/GridOrientation[.='IncreasingNorthEast']">Increase North-East</xsl:if>
          <xsl:if test="Projection/GridOrientation[.='IncreasingSouthWest']">Increase South-West</xsl:if>
          <xsl:if test="Projection/GridOrientation[.='IncreasingNorthWest']">Increase North-West</xsl:if>
          <xsl:if test="Projection/GridOrientation[.='IncreasingSouthEast']">Increase South-East</xsl:if>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>

    <!-- Include ellipsoid details -->
    <xsl:variable name="semiMajorAxis">
      <xsl:choose>
        <xsl:when test="Ellipsoid">
          <xsl:value-of select="Ellipsoid/EarthRadius"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="preceding-sibling::EllipsoidRecord[1]/EarthRadius"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:variable name="flattening">
      <xsl:choose>
        <xsl:when test="Ellipsoid">
          <xsl:value-of select="Ellipsoid/Flattening"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="preceding-sibling::EllipsoidRecord[1]/Flattening"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:if test="Projection/Type != 'ScaleOnly'">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Ellipsoid</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:text>Semi-major axis</xsl:text>
          <xsl:value-of select="': '"/>
          <xsl:value-of select="format-number($semiMajorAxis * $DistConvFactor, $DecPl3, 'Standard')"/>
          <xsl:value-of select="'  '"/>
          <xsl:text>Flattening</xsl:text>
          <xsl:value-of select="': '"/>
          <xsl:value-of select="format-number(1 div $flattening, $DecPl8, 'Standard')"/>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>

  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** Projection Type Text Output ****************** -->
<!-- **************************************************************** -->
<xsl:template name="ProjectionTypeText">
  <xsl:param name="Type"/>
  
  <xsl:choose>
    <xsl:when test="$Type = 'NoProjection'">No projection</xsl:when>
    <xsl:when test="$Type = 'ScaleOnly'">Scale factor only</xsl:when>
    <xsl:when test="$Type = 'Plane'">Plane</xsl:when>
    <xsl:when test="$Type = 'Mercator'">Mercator</xsl:when>
    <xsl:when test="$Type = 'TransverseMercator'">Transverse Mercator</xsl:when>
    <xsl:when test="$Type = 'UniversalTransverseMercator'">Universal Transverse Mercator</xsl:when>
    <xsl:when test="$Type = 'Lambert1Parallel'">Lambert Conformal Conic 1 Parallel</xsl:when>
    <xsl:when test="$Type = 'Lambert2Parallel'">Lambert Conformal Conic 2 Parallel</xsl:when>
    <xsl:when test="$Type = 'ObliqueMercatorAngle'">Oblique Mercator Angle</xsl:when>
    <xsl:when test="$Type = 'ObliqueConformalCylindrical'">Oblique Conformal Cylindrical</xsl:when>
    <xsl:when test="$Type = 'PolarStereographic'">Polar Stereographic</xsl:when>
    <xsl:when test="$Type = 'RDStereographic'">R.D. Stereographic</xsl:when>
    <xsl:when test="$Type = 'AlbersEqualAreaConic'">Albers Equal Area Conic</xsl:when>
    <xsl:when test="$Type = 'CassiniSoldner'">Cassini-Soldner</xsl:when>
    <xsl:when test="$Type = 'Krovak'">Krovak</xsl:when>
    <xsl:when test="$Type = 'NewZealandMapGrid'">New Zealand Map Grid</xsl:when>
    <xsl:when test="$Type = 'UnitedKingdomNationalGrid'">United Kingdom National Grid</xsl:when>
    <xsl:when test="$Type = 'Denmark'">Denmark</xsl:when>
    <xsl:when test="$Type = 'HungarianEOV'">Hungarian EOV</xsl:when>
    <xsl:when test="$Type = 'StereographicDouble'">Stereographic Double</xsl:when>
    <xsl:when test="$Type = 'RSO'">RSO</xsl:when>
    <xsl:when test="$Type = 'UPSNorth'">UPS north</xsl:when>
    <xsl:when test="$Type = 'UPSSouth'">UPS south</xsl:when>
    <xsl:when test="$Type = 'ProjectionGrid'">Projection grid</xsl:when>
    <xsl:otherwise>Unknown</xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************* LocalSite Details Output ******************* -->
<!-- **************************************************************** -->
<xsl:template name="LocalSiteOutput">

  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Local site</p></caption>
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Type</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:if test="LocalSite/Type[.='Grid']">Grid</xsl:if>
        <xsl:if test="LocalSite/Type[.='KeyedInGroundScaleFactor']">Ground (keyed in scale factor)</xsl:if>
        <xsl:if test="LocalSite/Type[.='CalculatedGroundScaleFactor']">Ground (calculated scale factor)</xsl:if>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:if test="LocalSite/ProjectLocationLatitude and
                  LocalSite/ProjectLocationLatitude != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Project location (Latitude)</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:call-template name="FormatLatLong">
            <xsl:with-param name="theAngle" select="LocalSite/ProjectLocationLatitude"/>
            <xsl:with-param name="northLbl">N</xsl:with-param>
            <xsl:with-param name="eastLbl">E</xsl:with-param>
            <xsl:with-param name="southLbl">S</xsl:with-param>
            <xsl:with-param name="westLbl">W</xsl:with-param>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="LocalSite/ProjectLocationLongitude and
                  LocalSite/ProjectLocationLongitude != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Project location (Longitude)</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:call-template name="FormatLatLong">
            <xsl:with-param name="theAngle" select="LocalSite/ProjectLocationLongitude"/>
            <xsl:with-param name="isLat" select="'false'"/>
            <xsl:with-param name="northLbl">N</xsl:with-param>
            <xsl:with-param name="eastLbl">E</xsl:with-param>
            <xsl:with-param name="southLbl">S</xsl:with-param>
            <xsl:with-param name="westLbl">W</xsl:with-param>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="LocalSite/ProjectLocationLongitude and
                  LocalSite/ProjectLocationLongitude != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Project location (Height)</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(LocalSite/ProjectLocationHeight * $ElevConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="LocalSite/GroundScaleFactor and
                  LocalSite/GroundScaleFactor != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Ground scale factor</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(LocalSite/GroundScaleFactor, $DecPl8, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="LocalSite/NorthingOffset and LocalSite/NorthingOffset != '' and 
                  LocalSite/EastingOffset and LocalSite/EastingOffset != ''">
      <xsl:choose>
        <xsl:when test="$NECoords = 'true'">
          <xsl:call-template name="OutputSingleElementTableLine">
            <xsl:with-param name="Hdr">Northing offset</xsl:with-param>
            <xsl:with-param name="Val" select="format-number(LocalSite/NorthingOffset * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:call-template>

          <xsl:call-template name="OutputSingleElementTableLine">
            <xsl:with-param name="Hdr">Easting offset</xsl:with-param>
            <xsl:with-param name="Val" select="format-number(LocalSite/EastingOffset * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="OutputSingleElementTableLine">
            <xsl:with-param name="Hdr">Easting offset</xsl:with-param>
            <xsl:with-param name="Val" select="format-number(LocalSite/EastingOffset * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:call-template>

          <xsl:call-template name="OutputSingleElementTableLine">
            <xsl:with-param name="Hdr">Northing offset</xsl:with-param>
            <xsl:with-param name="Val" select="format-number(LocalSite/NorthingOffset * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************** Datum Transformation Details Output ************* -->
<!-- **************************************************************** -->
<xsl:template name="DatumTransformationOutput">

  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Datum transformation</p></caption>
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Type</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:if test="Type[.='NoDatum']">None</xsl:if>
        <xsl:if test="Type[.='SevenParameter']">Seven parameter</xsl:if>
        <xsl:if test="Type[.='ThreeParameter']">Three parameter</xsl:if>
        <xsl:if test="Type[.='GridDatum']">Datum grid</xsl:if>
      </xsl:with-param>
    </xsl:call-template>
      
    <xsl:if test="EarthRadius and EarthRadius != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Semi-major axis</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(EarthRadius * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Flattening and Flattening != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Flattening</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(1.0 div Flattening, $DecPl6, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="RotationX and RotationX != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Rotation X</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="RotationX"/>
            <xsl:with-param name="secDecPlaces">4</xsl:with-param>
            <xsl:with-param name="DMSOutput" select="'true'"/>  <!-- Will override current angle units -->
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="RotationY and RotationY != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Rotation Y</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="RotationY"/>
            <xsl:with-param name="secDecPlaces">4</xsl:with-param>
            <xsl:with-param name="DMSOutput" select="'true'"/>  <!-- Will override current angle units -->
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="RotationZ and RotationZ != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Rotation Z</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="RotationZ"/>
            <xsl:with-param name="secDecPlaces">4</xsl:with-param>
            <xsl:with-param name="DMSOutput" select="'true'"/>  <!-- Will override current angle units -->
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="TranslationX and TranslationX != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Translation X</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(TranslationX * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="TranslationY and TranslationY != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Translation Y</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(TranslationY * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="TranslationZ and TranslationZ != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Translation Z</xsl:with-param>
        <xsl:with-param name="Val" select="format-number(TranslationZ * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="Scale and Scale != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Scale</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:value-of select="format-number((Scale - 1.0) * 1000000.0, $DecPl5, 'Standard')"/><xsl:text>ppm</xsl:text>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="GridName and GridName != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Datum Grid File</xsl:with-param>
        <xsl:with-param name="Val" select="GridName"/>
      </xsl:call-template>
    </xsl:if>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************* Horizontal Adjustment Details Output ************* -->
<!-- **************************************************************** -->
<xsl:template name="HorizontalAdjustmentOutput">

  <xsl:if test="Type != 'NoAdjustment'"> <!-- Don't output table at all if there is no adjustment -->
    <xsl:call-template name="StartTable"/>
      <caption align="top"><p align="left">Horizontal adjustment</p></caption>

      <xsl:if test="OriginNorth and OriginNorth != '' and 
                    OriginEast and OriginEast != ''">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'">
            <xsl:call-template name="OutputSingleElementTableLine">
              <xsl:with-param name="Hdr">Origin north</xsl:with-param>
              <xsl:with-param name="Val" select="format-number(OriginNorth * $DistConvFactor, $DecPl3, 'Standard')"/>
            </xsl:call-template>

            <xsl:call-template name="OutputSingleElementTableLine">
              <xsl:with-param name="Hdr">Origin east</xsl:with-param>
              <xsl:with-param name="Val" select="format-number(OriginEast * $DistConvFactor, $DecPl3, 'Standard')"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="OutputSingleElementTableLine">
              <xsl:with-param name="Hdr">Origin east</xsl:with-param>
              <xsl:with-param name="Val" select="format-number(OriginEast * $DistConvFactor, $DecPl3, 'Standard')"/>
            </xsl:call-template>

            <xsl:call-template name="OutputSingleElementTableLine">
              <xsl:with-param name="Hdr">Origin north</xsl:with-param>
              <xsl:with-param name="Val" select="format-number(OriginNorth * $DistConvFactor, $DecPl3, 'Standard')"/>
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:if>

      <xsl:if test="TranslationNorth and TranslationNorth != '' and 
                    TranslationEast and TranslationEast != ''">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'">
            <xsl:call-template name="OutputSingleElementTableLine">
              <xsl:with-param name="Hdr">Translation north</xsl:with-param>
              <xsl:with-param name="Val" select="format-number(TranslationNorth * $DistConvFactor, $DecPl3, 'Standard')"/>
            </xsl:call-template>

            <xsl:call-template name="OutputSingleElementTableLine">
              <xsl:with-param name="Hdr">Translation east</xsl:with-param>
              <xsl:with-param name="Val" select="format-number(TranslationEast * $DistConvFactor, $DecPl3, 'Standard')"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="OutputSingleElementTableLine">
              <xsl:with-param name="Hdr">Translation east</xsl:with-param>
              <xsl:with-param name="Val" select="format-number(TranslationEast * $DistConvFactor, $DecPl3, 'Standard')"/>
            </xsl:call-template>

            <xsl:call-template name="OutputSingleElementTableLine">
              <xsl:with-param name="Hdr">Translation north</xsl:with-param>
              <xsl:with-param name="Val" select="format-number(TranslationNorth * $DistConvFactor, $DecPl3, 'Standard')"/>
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:if>

      <xsl:if test="Rotation and Rotation != ''">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Rotation</xsl:with-param>
          <xsl:with-param name="Val">
            <xsl:call-template name="FormatAngle">
              <xsl:with-param name="theAngle" select="Rotation"/>
              <xsl:with-param name="secDecPlaces">4</xsl:with-param>
            </xsl:call-template>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:if>

      <xsl:if test="ScaleFactor and ScaleFactor != ''">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Scale factor</xsl:with-param>
          <xsl:with-param name="Val" select="format-number(ScaleFactor, $DecPl8, 'Standard')"/>
        </xsl:call-template>
      </xsl:if>

    <xsl:call-template name="EndTable"/>
  </xsl:if>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************** Vertical Adjustment Details Output ************** -->
<!-- **************************************************************** -->
<xsl:template name="VerticalAdjustmentOutput">

  <xsl:if test="Type != 'NoAdjustment'"> <!-- Don't output table at all if there is no adjustment -->
    <xsl:call-template name="StartTable"/>
      <caption align="top"><p align="left">Vertical adjustment</p></caption>

      <xsl:if test="OriginNorth and OriginNorth != '' and 
                    OriginEast and OriginEast != ''">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'">
            <xsl:call-template name="OutputSingleElementTableLine">
              <xsl:with-param name="Hdr">Origin north</xsl:with-param>
              <xsl:with-param name="Val" select="format-number(OriginNorth * $DistConvFactor, $DecPl3, 'Standard')"/>
            </xsl:call-template>

            <xsl:call-template name="OutputSingleElementTableLine">
              <xsl:with-param name="Hdr">Origin east</xsl:with-param>
              <xsl:with-param name="Val" select="format-number(OriginEast * $DistConvFactor, $DecPl3, 'Standard')"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="OutputSingleElementTableLine">
              <xsl:with-param name="Hdr">Origin east</xsl:with-param>
              <xsl:with-param name="Val" select="format-number(OriginEast * $DistConvFactor, $DecPl3, 'Standard')"/>
            </xsl:call-template>

            <xsl:call-template name="OutputSingleElementTableLine">
              <xsl:with-param name="Hdr">Origin north</xsl:with-param>
              <xsl:with-param name="Val" select="format-number(OriginNorth * $DistConvFactor, $DecPl3, 'Standard')"/>
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:if>

      <xsl:if test="SlopeNorthPerUnit and SlopeNorthPerUnit != '' and 
                    SlopeEastPerUnit and SlopeEastPerUnit != ''">
        <xsl:choose>
          <xsl:when test="$NECoords = 'true'">
            <xsl:call-template name="OutputSingleElementTableLine">
              <xsl:with-param name="Hdr">Slope north</xsl:with-param>
              <xsl:with-param name="Val" select="format-number(SlopeNorthPerUnit, $DecPl6, 'Standard')"/>
            </xsl:call-template>

            <xsl:call-template name="OutputSingleElementTableLine">
              <xsl:with-param name="Hdr">Slope east</xsl:with-param>
              <xsl:with-param name="Val" select="format-number(SlopeEastPerUnit, $DecPl6, 'Standard')"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="OutputSingleElementTableLine">
              <xsl:with-param name="Hdr">Slope east</xsl:with-param>
              <xsl:with-param name="Val" select="format-number(SlopeEastPerUnit, $DecPl6, 'Standard')"/>
            </xsl:call-template>

            <xsl:call-template name="OutputSingleElementTableLine">
              <xsl:with-param name="Hdr">Slope north</xsl:with-param>
              <xsl:with-param name="Val" select="format-number(SlopeNorthPerUnit, $DecPl6, 'Standard')"/>
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:if>

      <xsl:if test="ConstantAdjustment and ConstantAdjustment != ''">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Constant adjustment</xsl:with-param>
          <xsl:with-param name="Val" select="format-number(ConstantAdjustment * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:call-template>
      </xsl:if>

      <xsl:if test="GeoidName and GeoidName != ''">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Geoid file</xsl:with-param>
          <xsl:with-param name="Val" select="GeoidName"/>
        </xsl:call-template>
      </xsl:if>
    <xsl:call-template name="EndTable"/>
  </xsl:if>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************* Traverse Definition Output ***************** -->
<!-- **************************************************************** -->
<xsl:template match="TraverseDefinitionRecord">

  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Traverse Definition</p></caption>
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Name</xsl:with-param>
      <xsl:with-param name="Val" select="TraverseName"/>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Backsight azimuth</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:call-template name="FormatAzimuth">
          <xsl:with-param name="theAzimuth" select="BacksightAzimuth"/>
          <xsl:with-param name="northLbl">N</xsl:with-param>
          <xsl:with-param name="eastLbl">E</xsl:with-param>
          <xsl:with-param name="southLbl">S</xsl:with-param>
          <xsl:with-param name="westLbl">W</xsl:with-param>
        </xsl:call-template>
        <xsl:text> to </xsl:text><xsl:value-of select="BacksightPoint"/>
        <xsl:if test="BacksightAzimuthKeyedIn = 'true'">
          <xsl:text> (Keyed in)</xsl:text>
        </xsl:if>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:for-each select="ListOfStations/Item">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr"><xsl:text>Traverse Point </xsl:text><xsl:value-of select="@n"/></xsl:with-param>
        <xsl:with-param name="Val" select="."/>
      </xsl:call-template>
    </xsl:for-each>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Foresight azimuth</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:call-template name="FormatAzimuth">
          <xsl:with-param name="theAzimuth" select="ForesightAzimuth"/>
          <xsl:with-param name="northLbl">N</xsl:with-param>
          <xsl:with-param name="eastLbl">E</xsl:with-param>
          <xsl:with-param name="southLbl">S</xsl:with-param>
          <xsl:with-param name="westLbl">W</xsl:with-param>
        </xsl:call-template>
        <xsl:text> to </xsl:text><xsl:value-of select="ForesightPoint"/>
        <xsl:if test="ForesightAzimuthKeyedIn = 'true'">
          <xsl:text> (Keyed in)</xsl:text>
        </xsl:if>
      </xsl:with-param>
    </xsl:call-template>

  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************* Traverse Adjustment Output ***************** -->
<!-- **************************************************************** -->
<xsl:template match="TraverseAdjustmentRecord">

  <xsl:call-template name="StartTable"/>
    <caption align="top"><p align="left">Traverse Adjustment Method</p></caption>
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Adjustment method</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:if test="AdjustmentMethod[.='Transit']">Transit</xsl:if>
        <xsl:if test="AdjustmentMethod[.='Compass']">Compass</xsl:if>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Error distribution (Angular)</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:if test="AngularErrorDistribution[.='ProportionalToDistance']">Proportional to distance</xsl:if>
        <xsl:if test="AngularErrorDistribution[.='EqualProportions']">Equal proportions</xsl:if>
        <xsl:if test="AngularErrorDistribution[.='None']">None</xsl:if>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr"><xsl:text>Error distribution</xsl:text><xsl:value-of select="concat(' (', $elevPrompt, ')')"/></xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:if test="ElevationErrorDistribution[.='ProportionalToDistance']">Proportional to distance</xsl:if>
        <xsl:if test="ElevationErrorDistribution[.='EqualProportions']">Equal proportions</xsl:if>
        <xsl:if test="ElevationErrorDistribution[.='None']">None</xsl:if>
      </xsl:with-param>
    </xsl:call-template>

  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************** Traverse Closure Output ******************* -->
<!-- **************************************************************** -->
<xsl:template match="TraverseClosureRecord">

  <xsl:variable name="TraverseName">
    <xsl:for-each select="key('travID-search', TraverseDefinitionID)">
      <xsl:value-of select="TraverseName"/>
    </xsl:for-each>
  </xsl:variable>

  <xsl:variable name="AdjMethod">
    <xsl:if test="preceding-sibling::TraverseAdjustmentRecord[1]/AdjustmentMethod = 'Transit'">Transit</xsl:if>
    <xsl:if test="preceding-sibling::TraverseAdjustmentRecord[1]/AdjustmentMethod = 'Compass'">Compass</xsl:if>
  </xsl:variable>
  
  <!-- If the traverse has been adjusted output the traverse details in a 'spreadsheet' table.  -->
  <!-- This record needs to be followed by a TraverseAdjusted PointRecord that is not deleted.  -->
  <!-- This is because a traverse may have been adjusted more than once and if this is the case -->
  <!-- then the original traverse adjusted points are deleted.  Therefore if this is not the    -->
  <!-- final adjustment we don't want to output the traverse 'spreadsheet' details              -->
  <!-- Test the first PointRecord after the current TraverseClosureRecord to ensure it is a     -->
  <!-- non-deleted TraverseAdjusted point.                                                      -->
  <xsl:if test="ClosureStatus[.='Adjusted'] and
                following-sibling::PointRecord[1]/Method[.='TraverseAdjusted'] and
                following-sibling::PointRecord[1]/Deleted[.='false']">
    <xsl:call-template name="StartTable"/>
    <caption align="top">
      <p align="left"><xsl:text>Traverse Summary: </xsl:text><xsl:value-of select="$TraverseName"/></p>
    </caption>
    <xsl:for-each select="TraverseLegs/Leg">
      <xsl:variable name="FromPt" select="FromPoint"/>
      <xsl:variable name="ToPt" select="ToPoint"/>

      <xsl:if test="position() = 1">  <!-- The first traverse leg -->
        <!-- Get the starting point coordinates from the Reductions section -->
        <xsl:variable name="Coords">
          <xsl:for-each select="/JOBFile/Reductions/Point">
            <xsl:if test="Name = $FromPt">
              <xsl:value-of select="concat(Grid/North, '|', Grid/East)"/>
            </xsl:if>
          </xsl:for-each>
        </xsl:variable>
        <xsl:call-template name="OutputTableLine">
          <xsl:with-param name="Hdr1">Point</xsl:with-param>
          <xsl:with-param name="Val1" select="FromPoint"/>

          <xsl:with-param name="Hdr4">
            <xsl:if test="$NECoords = 'true'"><xsl:value-of select="$northPrompt"/></xsl:if>
            <xsl:if test="$NECoords != 'true'"><xsl:value-of select="$eastPrompt"/></xsl:if>
          </xsl:with-param>

          <xsl:with-param name="Val4">
            <xsl:if test="$NECoords = 'true'">
              <xsl:value-of select="format-number(number(substring-before($Coords, '|')) * $DistConvFactor, $DecPl3, 'Standard')"/>
            </xsl:if>
            <xsl:if test="$NECoords != 'true'">
              <xsl:value-of select="format-number(number(substring-after($Coords, '|')) * $DistConvFactor, $DecPl3, 'Standard')"/>
            </xsl:if>
          </xsl:with-param>
          
          <xsl:with-param name="Hdr5">
            <xsl:if test="$NECoords = 'true'"><xsl:value-of select="$eastPrompt"/></xsl:if>
            <xsl:if test="$NECoords != 'true'"><xsl:value-of select="$northPrompt"/></xsl:if>
          </xsl:with-param>

          <xsl:with-param name="Val5">
            <xsl:if test="$NECoords = 'true'">
              <xsl:value-of select="format-number(number(substring-after($Coords, '|')) * $DistConvFactor, $DecPl3, 'Standard')"/>
            </xsl:if>
            <xsl:if test="$NECoords != 'true'">
              <xsl:value-of select="format-number(number(substring-before($Coords, '|')) * $DistConvFactor, $DecPl3, 'Standard')"/>
            </xsl:if>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:if>

      <!-- Locate the traverse adjusted coordinates for the ToPoint following this closure record -->
      <xsl:variable name="Coords">
        <xsl:choose>
          <xsl:when test="position() != last()">
            <xsl:for-each select="following::PointRecord[Name = $ToPt]">
              <xsl:if test="(Method = 'TraverseAdjusted') and
                            (Deleted = 'false')">
                <xsl:value-of select="concat(Grid/North, '|', Grid/East, '|')"/>
              </xsl:if>
            </xsl:for-each>
          </xsl:when>
          <xsl:otherwise>  <!-- This is the closing point so get coords from Reductions section -->
            <xsl:for-each select="/JOBFile/Reductions/Point">
              <xsl:if test="Name = $ToPt">
                <xsl:value-of select="concat(Grid/North, '|', Grid/East, '|')"/>
              </xsl:if>
            </xsl:for-each>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <xsl:call-template name="OutputTableLine">
        <xsl:with-param name="Hdr1">Point</xsl:with-param>
        <xsl:with-param name="Val1" select="ToPoint"/>

        <xsl:with-param name="Hdr2">Azimuth</xsl:with-param>
        <xsl:with-param name="Val2">
          <xsl:call-template name="FormatAzimuth">
            <xsl:with-param name="theAzimuth" select="Polar/Azimuth"/>
            <xsl:with-param name="northLbl">N</xsl:with-param>
            <xsl:with-param name="eastLbl">E</xsl:with-param>
            <xsl:with-param name="southLbl">S</xsl:with-param>
            <xsl:with-param name="westLbl">W</xsl:with-param>
          </xsl:call-template>
        </xsl:with-param>

        <xsl:with-param name="Hdr3">H.Dist</xsl:with-param>
        <xsl:with-param name="Val3" select="format-number(Polar/HorizontalDistance * $DistConvFactor, $DecPl3, 'Standard')"/>

        <xsl:with-param name="Hdr4">
          <xsl:if test="$NECoords = 'true'"><xsl:value-of select="$northPrompt"/></xsl:if>
          <xsl:if test="$NECoords != 'true'"><xsl:value-of select="$eastPrompt"/></xsl:if>
        </xsl:with-param>

        <xsl:with-param name="Val4">
          <xsl:if test="$NECoords = 'true'">
            <xsl:value-of select="format-number(number(substring-before($Coords, '|')) * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:if>
          <xsl:if test="$NECoords != 'true'">
            <xsl:variable name="Temp" select="substring-after($Coords, '|')"/>
            <xsl:value-of select="format-number(number(substring-before($Temp, '|')) * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:if>
        </xsl:with-param>
          
        <xsl:with-param name="Hdr5">
          <xsl:if test="$NECoords = 'true'"><xsl:value-of select="$eastPrompt"/></xsl:if>
          <xsl:if test="$NECoords != 'true'"><xsl:value-of select="$northPrompt"/></xsl:if>
        </xsl:with-param>

        <xsl:with-param name="Val5">
          <xsl:if test="$NECoords = 'true'">
            <xsl:variable name="Temp" select="substring-after($Coords, '|')"/>
            <xsl:value-of select="format-number(number(substring-before($Temp, '|')) * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:if>
          <xsl:if test="$NECoords != 'true'">
            <xsl:value-of select="format-number(number(substring-before($Coords, '|')) * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:if>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:for-each>
    <xsl:call-template name="EndTable"/>
  </xsl:if>

  <!-- Now output the closure details -->
  <xsl:call-template name="StartTable"/>
    <caption align="top">
      <p align="left"><xsl:text>Traverse Closure Details: </xsl:text><xsl:value-of select="$TraverseName"/></p>
    </caption>
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Adjustment status</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:if test="ClosureStatus[.='NotAdjusted']">Not adjusted</xsl:if>
        <xsl:if test="ClosureStatus[.='Closed']">Closed</xsl:if>
        <xsl:if test="ClosureStatus[.='Adjusted']">
          <xsl:text>Adjusted</xsl:text><xsl:value-of select="concat(' (', $AdjMethod, ')')"/>
        </xsl:if>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Angular close</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle">
            <xsl:choose>
              <xsl:when test="AzimuthMiscloseAfterAngularAdjustment != ''">
                <xsl:value-of select="AzimuthMiscloseAfterAngularAdjustment"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="AzimuthMisclose"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>
          <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
        </xsl:call-template>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Distance close</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:choose>
          <xsl:when test="DistanceMiscloseAfterAngularAdjustment != ''">
            <xsl:value-of  select="format-number(DistanceMiscloseAfterAngularAdjustment * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of  select="format-number(DistanceMisclose * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Traverse length</xsl:with-param>
      <xsl:with-param name="Val" select="format-number(TraverseLength * $DistConvFactor, $DecPl3, 'Standard')"/>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Precision</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:choose>
          <xsl:when test="PrecisionAfterAngularAdjustment != ''">
            <xsl:value-of select="concat('1 : ', format-number(PrecisionAfterAngularAdjustment, $DecPl1, 'Standard'))"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="concat('1 : ', format-number(Precision, $DecPl1, 'Standard'))"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr" select="$dNorthPrompt"/>
      <xsl:with-param name="Val">
        <xsl:choose>
          <xsl:when test="DeltaNorthAfterAngularAdjustment != ''">
            <xsl:value-of select="format-number(DeltaNorthAfterAngularAdjustment * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number(DeltaNorth * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr" select="$dEastPrompt"/>
      <xsl:with-param name="Val">
        <xsl:choose>
          <xsl:when test="DeltaEastAfterAngularAdjustment != ''">
            <xsl:value-of select="format-number(DeltaEastAfterAngularAdjustment * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number(DeltaEast * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:if test="DeltaElevation != ''">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr" select="$dElevPrompt"/>
        <xsl:with-param name="Val">
          <xsl:choose>
            <xsl:when test="DeltaElevationAfterAngularAdjustment != ''">
              <xsl:value-of select="format-number(DeltaElevationAfterAngularAdjustment * $ElevConvFactor, $DecPl3, 'Standard')"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="format-number(DeltaElevation * $ElevConvFactor, $DecPl3, 'Standard')"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>

  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>

</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** COGO Transformation Output ****************** -->
<!-- **************************************************************** -->
<xsl:template match="CogoTransformationRecord">

  <xsl:call-template name="StartTable"/>
  <caption align="top">
    <p align="left">
    <xsl:text>Transformations: (Points transformed: </xsl:text>
    <xsl:value-of select="concat(NumberOfPointsTransformed, ')')"/>
    </p>
  </caption>
  <xsl:if test="Rotation">
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Rotation</xsl:with-param>
      <xsl:with-param name="Val1">Origin at</xsl:with-param>

      <xsl:with-param name="Hdr2">Point</xsl:with-param>
      <xsl:with-param name="Val2" select="Rotation/RotationOriginPoint"/>
      
      <xsl:with-param name="Hdr3">
        <xsl:if test="$NECoords = 'true'"><xsl:value-of select="$northPrompt"/></xsl:if>
        <xsl:if test="$NECoords != 'true'"><xsl:value-of select="$eastPrompt"/></xsl:if>
      </xsl:with-param>

      <xsl:with-param name="Val3">
        <xsl:if test="$NECoords = 'true'">
          <xsl:value-of select="format-number(Rotation/RotationOriginGrid/North * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:if>
        <xsl:if test="$NECoords != 'true'">
          <xsl:value-of select="format-number(Rotation/RotationOriginGrid/East * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:if>
      </xsl:with-param>

      <xsl:with-param name="Hdr4">
        <xsl:if test="$NECoords = 'true'"><xsl:value-of select="$eastPrompt"/></xsl:if>
        <xsl:if test="$NECoords != 'true'"><xsl:value-of select="$northPrompt"/></xsl:if>
      </xsl:with-param>

      <xsl:with-param name="Val4">
        <xsl:if test="$NECoords = 'true'">
          <xsl:value-of select="format-number(Rotation/RotationOriginGrid/East * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:if>
        <xsl:if test="$NECoords != 'true'">
          <xsl:value-of select="format-number(Rotation/RotationOriginGrid/North * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:if>
      </xsl:with-param>

      <xsl:with-param name="Hdr5" select="$elevPrompt"/>
      <xsl:with-param name="Val5" select="format-number(Rotation/RotationOriginGrid/Elevation * $ElevConvFactor, $DecPl3, 'Standard')"/>
    </xsl:call-template>

    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Rotation</xsl:with-param>

      <xsl:with-param name="Hdr2">Angle</xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle" select="Rotation/RotationAngle"/>
          <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
        </xsl:call-template>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:if>
  
  <xsl:if test="Scale">
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Scale</xsl:with-param>
      <xsl:with-param name="Val1">Origin at</xsl:with-param>

      <xsl:with-param name="Hdr2">Point</xsl:with-param>
      <xsl:with-param name="Val2" select="Scale/ScaleOriginPoint"/>

      <xsl:with-param name="Hdr3">
        <xsl:if test="$NECoords = 'true'"><xsl:value-of select="$northPrompt"/></xsl:if>
        <xsl:if test="$NECoords != 'true'"><xsl:value-of select="$eastPrompt"/></xsl:if>
      </xsl:with-param>

      <xsl:with-param name="Val3">
        <xsl:if test="$NECoords = 'true'">
          <xsl:value-of select="format-number(Scale/ScaleOriginGrid/North * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:if>
        <xsl:if test="$NECoords != 'true'">
          <xsl:value-of select="format-number(Scale/ScaleOriginGrid/East * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:if>
      </xsl:with-param>

      <xsl:with-param name="Hdr4">
        <xsl:if test="$NECoords = 'true'"><xsl:value-of select="$eastPrompt"/></xsl:if>
        <xsl:if test="$NECoords != 'true'"><xsl:value-of select="$northPrompt"/></xsl:if>
      </xsl:with-param>

      <xsl:with-param name="Val4">
        <xsl:if test="$NECoords = 'true'">
          <xsl:value-of select="format-number(Scale/ScaleOriginGrid/East * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:if>
        <xsl:if test="$NECoords != 'true'">
          <xsl:value-of select="format-number(Scale/ScaleOriginGrid/North * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:if>
      </xsl:with-param>

      <xsl:with-param name="Hdr5" select="$elevPrompt"/>
      <xsl:with-param name="Val5" select="format-number(Scale/ScaleOriginGrid/Elevation * $ElevConvFactor, $DecPl3, 'Standard')"/>
    </xsl:call-template>

    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Scale</xsl:with-param>

      <xsl:with-param name="Hdr2">Scale factor</xsl:with-param>
      <xsl:with-param name="Val2" select="format-number(Scale/ScaleFactor, $DecPl8, 'Standard')"/>
    </xsl:call-template>
  </xsl:if>

  <xsl:if test="Translation">
    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Translation</xsl:with-param>
      <xsl:with-param name="Val1">Deltas</xsl:with-param>

      <xsl:with-param name="Hdr2">Definition</xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:if test="Translation/TranslationDefinitionMethod[.='EnteredDeltas']">Entered data</xsl:if>
        <xsl:if test="Translation/TranslationDefinitionMethod[.='TwoPoints']">Two points</xsl:if>
      </xsl:with-param>

      <xsl:with-param name="Hdr3">
        <xsl:if test="$NECoords = 'true'"><xsl:value-of select="$dNorthPrompt"/></xsl:if>
        <xsl:if test="$NECoords != 'true'"><xsl:value-of select="$dEastPrompt"/></xsl:if>
      </xsl:with-param>

      <xsl:with-param name="Val3">
        <xsl:if test="$NECoords = 'true'">
          <xsl:value-of select="format-number(Translation/TranslationDeltas/DeltaNorth * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:if>
        <xsl:if test="$NECoords != 'true'">
          <xsl:value-of select="format-number(Translation/TranslationDeltas/DeltaEast * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:if>
      </xsl:with-param>

      <xsl:with-param name="Hdr4">
        <xsl:if test="$NECoords = 'true'"><xsl:value-of select="$dEastPrompt"/></xsl:if>
        <xsl:if test="$NECoords != 'true'"><xsl:value-of select="$dNorthPrompt"/></xsl:if>
      </xsl:with-param>

      <xsl:with-param name="Val4">
        <xsl:if test="$NECoords = 'true'">
          <xsl:value-of select="format-number(Translation/TranslationDeltas/DeltaEast * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:if>
        <xsl:if test="$NECoords != 'true'">
          <xsl:value-of select="format-number(Translation/TranslationDeltas/DeltaNorth * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:if>
      </xsl:with-param>

      <xsl:with-param name="Hdr5" select="$dElevPrompt"/>
      <xsl:with-param name="Val5" select="format-number(Translation/TranslationDeltas/DeltaElevation * $ElevConvFactor, $DecPl3, 'Standard')"/>
    </xsl:call-template>

    <xsl:if test="Translation/TranslationDefinitionMethod[.='TwoPoints']">
      <xsl:call-template name="OutputTableLine">
        <xsl:with-param name="Hdr1">Translation</xsl:with-param>

        <xsl:with-param name="Hdr2">Points</xsl:with-param>

        <xsl:with-param name="Hdr3">From point</xsl:with-param>
        <xsl:with-param name="Val3" select="Translation/TranslationFromPoint"/>

        <xsl:with-param name="Hdr4">To point</xsl:with-param>
        <xsl:with-param name="Val4" select="Translation/TranslationToPoint"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:if>

  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>

</xsl:template>


<!-- **************************************************************** -->
<!-- ************* Return an area value string with units *********** -->
<!-- **************************************************************** -->
<xsl:template name="AreaValueString">
  <xsl:param name="area"/>
  
  <xsl:variable name="areaVal" select="$area * $AreaConvFactor"/>
  
  <xsl:choose>
    <xsl:when test="$AreaUnit = 'SquareMetres'">
      <xsl:value-of select="format-number($areaVal, $DecPl2, 'Standard')"/>
      <xsl:text>m²</xsl:text>
    </xsl:when>
    <xsl:when test="$AreaUnit = 'SquareMiles'">
      <xsl:value-of select="format-number($areaVal, $DecPl5, 'Standard')"/>
      <xsl:text>mi²</xsl:text>
    </xsl:when>
    <xsl:when test="$AreaUnit = 'SquareFeet'">
      <xsl:value-of select="format-number($areaVal, $DecPl2, 'Standard')"/>
      <xsl:text>ift²</xsl:text>
    </xsl:when>
    <xsl:when test="$AreaUnit = 'SquareUSSurveyFeet'">
      <xsl:value-of select="format-number($areaVal, $DecPl2, 'Standard')"/>
      <xsl:text>sft²</xsl:text>
    </xsl:when>
    <xsl:when test="$AreaUnit = 'Acres'">
      <xsl:value-of select="format-number($areaVal, $DecPl4, 'Standard')"/>
      <xsl:text>A</xsl:text>
    </xsl:when>
    <xsl:when test="$AreaUnit = 'Hectares'">
      <xsl:value-of select="format-number($areaVal, $DecPl4, 'Standard')"/>
      <xsl:text>Ha</xsl:text>
    </xsl:when>
  </xsl:choose>
  
</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** Compute Area Record Output ****************** -->
<!-- **************************************************************** -->
<xsl:template name="OutputAreaDetails">
  <xsl:call-template name="BlankLine"/>
  <xsl:call-template name="StartTable"/>
  <caption align="top">
    <p align="left">
    <xsl:choose>
      <xsl:when test="name(.) = 'SubdivideAreaRecord'">Subdivide area</xsl:when>
      <xsl:otherwise>Compute area</xsl:otherwise>
    </xsl:choose>
    </p>
  </caption>

    <xsl:call-template name="OutputTableLine">
      <xsl:with-param name="Hdr1">Name</xsl:with-param>
      <xsl:with-param name="Val1">
        <xsl:value-of select="Name"/>
      </xsl:with-param>
      <xsl:with-param name="Hdr2">
        <xsl:choose>
          <xsl:when test="DistanceType = 'Grid'">Area (grid)</xsl:when>
          <xsl:when test="DistanceType = 'Ground'">Area (ground)</xsl:when>
          <xsl:when test="DistanceType = 'Ellipsoid'">Area (ell)</xsl:when>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Val2">
        <xsl:call-template name="AreaValueString">
          <xsl:with-param name="area" select="Area"/>
        </xsl:call-template>
      </xsl:with-param>
      <xsl:with-param name="Hdr3">Perimeter</xsl:with-param>
      <xsl:with-param name="Val3">
        <xsl:value-of select="format-number(Perimeter * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:if test="name(.) = 'SubdivideAreaRecord'">
      <xsl:call-template name="OutputTableLine">
        <xsl:with-param name="Hdr2">
          <xsl:choose>
            <xsl:when test="DistanceType = 'Grid'">New area (grid)</xsl:when>
            <xsl:when test="DistanceType = 'Ground'">New area (ground)</xsl:when>
            <xsl:when test="DistanceType = 'Ellipsoid'">New area (ell)</xsl:when>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Val2">
          <xsl:call-template name="AreaValueString">
            <xsl:with-param name="area" select="SubArea"/>
          </xsl:call-template>
        </xsl:with-param>
        <xsl:with-param name="Hdr3">New perimeter</xsl:with-param>
        <xsl:with-param name="Val3">
          <xsl:value-of select="format-number(SubPerimeter * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:with-param>
      </xsl:call-template>
      <xsl:for-each select="CreatedPoints/Item">
        <xsl:variable name="ptName"    select="."/>
        <xsl:variable name="north"     select="/JOBFile/Reductions/Point[Name = $ptName]/Grid/North"/>
        <xsl:variable name="east"      select="/JOBFile/Reductions/Point[Name = $ptName]/Grid/East"/>
        <xsl:variable name="elevation" select="/JOBFile/Reductions/Point[Name = $ptName]/Grid/Elevation"/>
        <xsl:variable name="code"      select="/JOBFile/Reductions/Point[Name = $ptName]/Code"/>

        <xsl:call-template name="OutputTableLine">
          <xsl:with-param name="Hdr1">New point</xsl:with-param>
          <xsl:with-param name="Val1">
            <xsl:value-of select="."/>
          </xsl:with-param>

          <xsl:with-param name="Hdr2">
            <xsl:choose>
              <xsl:when test="$NECoords = 'true'"><xsl:value-of select="$northPrompt"/></xsl:when>
              <xsl:otherwise><xsl:value-of select="$eastPrompt"/></xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>
          <xsl:with-param name="Val2">
            <xsl:choose>
              <xsl:when test="$NECoords = 'true'">
                <xsl:value-of select="format-number($north * $DistConvFactor, $DecPl3, 'Standard')"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="format-number($east * $DistConvFactor, $DecPl3, 'Standard')"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>

          <xsl:with-param name="Hdr3">
            <xsl:choose>
              <xsl:when test="$NECoords = 'true'"><xsl:value-of select="$eastPrompt"/></xsl:when>
              <xsl:otherwise><xsl:value-of select="$northPrompt"/></xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>
          <xsl:with-param name="Val3">
            <xsl:choose>
              <xsl:when test="$NECoords = 'true'">
                <xsl:value-of select="format-number($east * $DistConvFactor, $DecPl3, 'Standard')"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="format-number($north * $DistConvFactor, $DecPl3, 'Standard')"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>

          <xsl:with-param name="Hdr4" select="$elevPrompt"/>
          <xsl:with-param name="Val4" select="format-number($elevation * $ElevConvFactor, $DecPl3, 'Standard')"/>

          <xsl:with-param name="Hdr5">Code</xsl:with-param>
          <xsl:with-param name="Val5" select="$code"/>
        </xsl:call-template>
      </xsl:for-each>
    </xsl:if>

  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** Compute Area Record Output ****************** -->
<!-- **************************************************************** -->
<xsl:template match="ComputeAreaRecord">
  <xsl:call-template name="OutputAreaDetails"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** Subdivide Area Record Output ***************** -->
<!-- **************************************************************** -->
<xsl:template match="SubdivideAreaRecord">
  <xsl:call-template name="OutputAreaDetails"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- *************** Volume Computation Record Output *************** -->
<!-- **************************************************************** -->
<xsl:template match="VolumeRecord">

  <xsl:call-template name="OutputVolumeHeadings"/>

  <xsl:choose>
    <xsl:when test="AdjustedVolumeResults">
      <xsl:call-template name="OutputAdjustedVolumesTable"/>
    </xsl:when>

    <xsl:otherwise>
      <xsl:call-template name="OutputRawVolumesTable"/>
    </xsl:otherwise>
  </xsl:choose>

  <xsl:call-template name="OutputBulkageShrinkageValues"/>

  <xsl:if test="AdjustedVolumeResults">
    <xsl:call-template name="OutputRawVolumesTable"/>
  </xsl:if>

  <xsl:call-template name="OutputAreaValues"/>
  <xsl:call-template name="SeparatingLine"/>

</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** Output volume headings table ***************** -->
<!-- **************************************************************** -->
<xsl:template name="OutputVolumeHeadings">

  <xsl:call-template name="BlankLine"/>
  <xsl:call-template name="StartTable"/>
  <caption align="top"><p align="left">Volume</p></caption>
    <tr>
      <th align="left" width="25%">Method</th>
      <td align="left" width="75%">
        <xsl:call-template name="VolumeMethod">
          <xsl:with-param name="method" select="VolumeMethod"/>
        </xsl:call-template>
      </td>
    </tr>

    <xsl:choose>
      <xsl:when test="VolumeMethod = 'AboveAnElevation'">
        <tr>
          <th align="left" width="25%">Surface</th>
          <td align="left" width="75%">
            <xsl:value-of select="substring(SurfaceFileName, 1, string-length(SurfaceFileName) - 4)"/>
          </td>
        </tr>
        <tr>
          <th align="left" width="25%">Elevation</th>
          <td align="left" width="75%">
            <xsl:value-of select="format-number(ReferenceElevation * $DistConvFactor, $DecPl3, 'Standard')"/>
          </td>
        </tr>
      </xsl:when>

      <xsl:when test="VolumeMethod = 'VoidVolume'">
        <tr>
          <th align="left" width="25%">Surface</th>
          <td align="left" width="75%">
            <xsl:value-of select="substring(SurfaceFileName, 1, string-length(SurfaceFileName) - 4)"/>
          </td>
        </tr>
        <tr>
          <th align="left" width="25%">Elevation</th>
          <td align="left" width="75%">
            <xsl:value-of select="format-number(ReferenceElevation * $DistConvFactor, $DecPl3, 'Standard')"/>
          </td>
        </tr>
      </xsl:when>

      <xsl:when test="VolumeMethod = 'SurfaceToElevation'">
        <tr>
          <th align="left" width="25%">Surface</th>
          <td align="left" width="75%">
            <xsl:value-of select="substring(SurfaceFileName, 1, string-length(SurfaceFileName) - 4)"/>
          </td>
        </tr>
        <tr>
          <th align="left" width="25%">Elevation</th>
          <td align="left" width="75%">
            <xsl:value-of select="format-number(ReferenceElevation * $DistConvFactor, $DecPl3, 'Standard')"/>
          </td>
        </tr>
      </xsl:when>

      <xsl:when test="VolumeMethod = 'SurfaceToSurface'">
        <tr>
          <th align="left" width="25%">Initial surface</th>
          <td align="left" width="75%">
            <xsl:value-of select="substring(BaseSurfaceFileName, 1, string-length(BaseSurfaceFileName) - 4)"/>
          </td>
        </tr>
        <tr>
          <th align="left" width="25%">Final surface</th>
          <td align="left" width="75%">
            <xsl:value-of select="substring(SurfaceFileName, 1, string-length(SurfaceFileName) - 4)"/>
          </td>
        </tr>
      </xsl:when>

      <xsl:when test="VolumeMethod = 'StockpileDepression'">
        <tr>
          <th align="left" width="25%">Surface</th>
          <td align="left" width="75%">
            <xsl:value-of select="substring(SurfaceFileName, 1, string-length(SurfaceFileName) - 4)"/>
          </td>
        </tr>
      </xsl:when>
    </xsl:choose>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ********* Return the volume method as a nice string ************ -->
<!-- **************************************************************** -->
<xsl:template name="VolumeMethod">
  <xsl:param name="method"/>

  <xsl:choose>
    <xsl:when test="$method = 'AboveAnElevation'">Above an elevation</xsl:when>
    <xsl:when test="$method = 'VoidVolume'">Void volume</xsl:when>
    <xsl:when test="$method = 'SurfaceToElevation'">Surface to elevation</xsl:when>
    <xsl:when test="$method = 'SurfaceToSurface'">Surface to surface</xsl:when>
    <xsl:when test="$method = 'StockpileDepression'">Stockpile/depression</xsl:when>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- **************** Output adjusted volumes table ***************** -->
<!-- **************************************************************** -->
<xsl:template name="OutputAdjustedVolumesTable">

  <xsl:call-template name="StartTable"/>
  <caption align="top"><p align="left">Adjusted volume</p></caption>
    <tr>
      <xsl:choose>
        <!-- Adjusted cut and fill volumes available -->
        <xsl:when test="(string(number(AdjustedVolumeResults/CutVolume)) != 'NaN') and
                        (string(number(AdjustedVolumeResults/FillVolume)) != 'NaN')">
          <th align="left" width="25%">Adjusted cut volume</th>
          <td align="right" width="25%">
            <xsl:call-template name="VolumeValueString">
              <xsl:with-param name="volume" select="AdjustedVolumeResults/CutVolume"/>
            </xsl:call-template>
          </td>
          <th align="left" width="25%">Adjusted fill volume</th>
          <td align="right" width="25%">
            <xsl:call-template name="VolumeValueString">
              <xsl:with-param name="volume" select="AdjustedVolumeResults/FillVolume"/>
            </xsl:call-template>
          </td>
        </xsl:when>
        <!-- Only adjusted cut volume available -->
        <xsl:when test="(string(number(AdjustedVolumeResults/CutVolume)) != 'NaN') and
                        (string(number(AdjustedVolumeResults/FillVolume)) = 'NaN')">
          <th align="left" width="25%">Adjusted cut volume</th>
          <td align="right" width="25%">
            <xsl:call-template name="VolumeValueString">
              <xsl:with-param name="volume" select="AdjustedVolumeResults/CutVolume"/>
            </xsl:call-template>
          </td>
          <td colspan="2">&#160;</td>
        </xsl:when>
        <!-- Only adjusted fill volume available -->
        <xsl:when test="(string(number(AdjustedVolumeResults/CutVolume)) = 'NaN') and
                        (string(number(AdjustedVolumeResults/FillVolume)) != 'NaN')">
          <th align="left" width="25%">Adjusted fill volume</th>
          <td align="right" width="25%">
            <xsl:call-template name="VolumeValueString">
              <xsl:with-param name="volume" select="AdjustedVolumeResults/FillVolume"/>
            </xsl:call-template>
          </td>
          <td colspan="2">&#160;</td>
        </xsl:when>
      </xsl:choose>
    </tr>
    <xsl:if test="string(number(AdjustedVolumeResults/CutFillBalance)) != 'NaN'">
      <tr>
        <th align="left" width="25%">Adjusted cut/fill balance</th>
        <td align="right" width="25%">
          <xsl:call-template name="VolumeValueString">
            <xsl:with-param name="volume" select="AdjustedVolumeResults/CutFillBalance"/>
          </xsl:call-template>
        </td>
        <td colspan="2">&#160;</td>
      </tr>
    </xsl:if>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************** Output raw volumes table ****************** -->
<!-- **************************************************************** -->
<xsl:template name="OutputRawVolumesTable">

  <xsl:call-template name="StartTable"/>
  <caption align="top"><p align="left">In situ volume</p></caption>
    <tr>
      <xsl:choose>
        <!-- Cut and fill volumes available -->
        <xsl:when test="(string(number(VolumeResults/CutVolume)) != 'NaN') and
                        (string(number(VolumeResults/FillVolume)) != 'NaN')">
          <th align="left" width="25%">Cut volume</th>
          <td align="right" width="25%">
            <xsl:call-template name="VolumeValueString">
              <xsl:with-param name="volume" select="VolumeResults/CutVolume"/>
            </xsl:call-template>
          </td>
          <th align="left" width="25%">Fill volume</th>
          <td align="right" width="25%">
            <xsl:call-template name="VolumeValueString">
              <xsl:with-param name="volume" select="VolumeResults/FillVolume"/>
            </xsl:call-template>
          </td>
        </xsl:when>
        <!-- Only cut volume available -->
        <xsl:when test="(string(number(VolumeResults/CutVolume)) != 'NaN') and
                        (string(number(VolumeResults/FillVolume)) = 'NaN')">
          <th align="left" width="25%">Cut volume</th>
          <td align="right" width="25%">
            <xsl:call-template name="VolumeValueString">
              <xsl:with-param name="volume" select="VolumeResults/CutVolume"/>
            </xsl:call-template>
          </td>
          <td colspan="2">&#160;</td>
        </xsl:when>
        <!-- Only fill volume available -->
        <xsl:when test="(string(number(VolumeResults/CutVolume)) = 'NaN') and
                        (string(number(VolumeResults/FillVolume)) != 'NaN')">
          <th align="left" width="25%">Fill volume</th>
          <td align="right" width="25%">
            <xsl:call-template name="VolumeValueString">
              <xsl:with-param name="volume" select="VolumeResults/FillVolume"/>
            </xsl:call-template>
          </td>
          <td colspan="2">&#160;</td>
        </xsl:when>
      </xsl:choose>
    </tr>
    <xsl:if test="string(number(VolumeResults/CutFillBalance)) != 'NaN'">
      <tr>
        <th align="left" width="25%">Cut/fill balance</th>
        <td align="right" width="25%">
          <xsl:call-template name="VolumeValueString">
            <xsl:with-param name="volume" select="VolumeResults/CutFillBalance"/>
          </xsl:call-template>
        </td>
        <td colspan="2">&#160;</td>
      </tr>
    </xsl:if>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************ Output the bulkage and shrinkage values *********** -->
<!-- **************************************************************** -->
<xsl:template name="OutputBulkageShrinkageValues">

  <xsl:call-template name="StartTable"/>
    <tr>
      <xsl:choose>
        <!-- Cut and fill volumes available - report both bulkage and shrinkage values -->
        <xsl:when test="(string(number(VolumeResults/CutVolume)) != 'NaN') and
                        (string(number(VolumeResults/FillVolume)) != 'NaN')">
          <th align="left" width="25%">Haul bulkage (cut)</th>
          <td align="right" width="25%">
            <xsl:value-of select="format-number(HaulBulkagePercentage, $DecPl1, 'Standard')"/>
            <xsl:text>%</xsl:text>
          </td>
          <th align="left" width="25%">Shrinkage (fill)</th>
          <td align="right" width="25%">
            <xsl:value-of select="format-number(ShrinkagePercentage, $DecPl1, 'Standard')"/>
            <xsl:text>%</xsl:text>
          </td>
        </xsl:when>
        <!-- Only cut volume available - report only bulkage value -->
        <xsl:when test="(string(number(VolumeResults/CutVolume)) != 'NaN') and
                        (string(number(VolumeResults/FillVolume)) = 'NaN')">
          <th align="left" width="25%">Haul bulkage (cut)</th>
          <td align="right" width="25%">
            <xsl:value-of select="format-number(HaulBulkagePercentage, $DecPl1, 'Standard')"/>
            <xsl:text>%</xsl:text>
          </td>
          <td colspan="2">&#160;</td>
        </xsl:when>
        <!-- Only fill volume available - report only shrinkage value -->
        <xsl:when test="(string(number(VolumeResults/CutVolume)) = 'NaN') and
                        (string(number(VolumeResults/FillVolume)) != 'NaN')">
          <th align="left" width="25%">Shrinkage (fill)</th>
          <td align="right" width="25%">
            <xsl:value-of select="format-number(ShrinkagePercentage, $DecPl1, 'Standard')"/>
            <xsl:text>%</xsl:text>
          </td>
          <td colspan="2">&#160;</td>
        </xsl:when>
      </xsl:choose>
    </tr>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************** Output the area values ******************** -->
<!-- **************************************************************** -->
<xsl:template name="OutputAreaValues">

  <xsl:call-template name="StartTable"/>
    <tr>
      <xsl:choose>
        <!-- Cut and fill areas both available -->
        <xsl:when test="(string(number(CutArea)) != 'NaN') and
                        (string(number(FillArea)) != 'NaN')">
          <th align="left" width="25%">Cut area</th>
          <td align="right" width="25%">
            <xsl:call-template name="AreaValueString">
              <xsl:with-param name="area" select="CutArea"/>
            </xsl:call-template>
          </td>
          <th align="left" width="25%">Fill area</th>
          <td align="right" width="25%">
            <xsl:call-template name="AreaValueString">
              <xsl:with-param name="area" select="FillArea"/>
            </xsl:call-template>
          </td>
        </xsl:when>
        <!-- Only cut area available -->
        <xsl:when test="(string(number(CutArea)) != 'NaN') and
                        (string(number(FillArea)) = 'NaN')">
          <th align="left" width="25%">Cut area</th>
          <td align="right" width="25%">
            <xsl:call-template name="AreaValueString">
              <xsl:with-param name="area" select="CutArea"/>
            </xsl:call-template>
          </td>
          <td colspan="2">&#160;</td>
        </xsl:when>
        <!-- Only fill area available -->
        <xsl:when test="(string(number(CutArea)) = 'NaN') and
                        (string(number(FillArea)) != 'NaN')">
          <th align="left" width="25%">Fill area</th>
          <td align="right" width="25%">
            <xsl:call-template name="AreaValueString">
              <xsl:with-param name="area" select="FillArea"/>
            </xsl:call-template>
          </td>
          <td colspan="2">&#160;</td>
        </xsl:when>
      </xsl:choose>
    </tr>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************ Return a volume value string with units *********** -->
<!-- **************************************************************** -->
<xsl:template name="VolumeValueString">
  <xsl:param name="volume"/>

  <xsl:variable name="volumeVal" select="$volume * $VolConvFactor"/>

  <xsl:choose>
    <xsl:when test="$VolumeUnit = 'CubicMetres'">
      <xsl:value-of select="format-number($volumeVal, $DecPl2, 'Standard')"/>
      <xsl:text>m&#0179;</xsl:text>
    </xsl:when>
    <xsl:when test="$VolumeUnit = 'CubicFeet'">
      <xsl:value-of select="format-number($volumeVal, $DecPl2, 'Standard')"/>
      <xsl:text>ft&#0179;</xsl:text>
    </xsl:when>
    <xsl:when test="$VolumeUnit = 'CubicUSSurveyFeet'">
      <xsl:value-of select="format-number($volumeVal, $DecPl2, 'Standard')"/>
      <xsl:text>sft&#0179;</xsl:text>
    </xsl:when>
    <xsl:when test="$VolumeUnit = 'CubicYards'">
      <xsl:value-of select="format-number($volumeVal, $DecPl2, 'Standard')"/>
      <xsl:text>yds&#0179;</xsl:text>
    </xsl:when>
    <xsl:when test="$VolumeUnit = 'CubicUSSurveyYards'">
      <xsl:value-of select="format-number($volumeVal, $DecPl4, 'Standard')"/>
      <xsl:text>yds&#0179;</xsl:text>
    </xsl:when>
    <xsl:when test="$VolumeUnit = 'AcreFeet'">
        <xsl:value-of select="format-number($volumeVal, $DecPl5, 'Standard')"/>
        <xsl:text>ac-ft</xsl:text>
    </xsl:when>
    <xsl:when test="$VolumeUnit = 'USAcreFeet'">
        <xsl:value-of select="format-number($volumeVal, $DecPl5, 'Standard')"/>
        <xsl:text>USac-ft</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="format-number($volumeVal, $DecPl2, 'Standard')"/>
      <xsl:text>m&#0179;</xsl:text>
    </xsl:otherwise>
  </xsl:choose>

</xsl:template>


<!-- **************************************************************** -->
<!-- ************ Tilt Sensor Calibration Record Output ************* -->
<!-- **************************************************************** -->
<xsl:template match="TiltSensorCalibrationRecord">

  <xsl:call-template name="StartTable"/>
  <caption align="top"><p align="left">eBubble calibration status</p></caption>
    <tr>
      <th align="left">Event</th>
      <td>
        <xsl:choose>
          <xsl:when test="TiltEvent = 'Calibration'">Calibration</xsl:when>
          <xsl:otherwise>Start survey</xsl:otherwise>
        </xsl:choose>
      </td>

      <th align="left">Calibration expires in</th>
      <td>
        <xsl:choose>
          <xsl:when test="string(number(CalibrationTimeRemaining)) != 'NaN'">
            <xsl:variable name="days" select="floor(CalibrationTimeRemaining div 3600 div 24)"/>
            <xsl:variable name="hours" select="(CalibrationTimeRemaining - $days * 3600 * 24) div 3600"/>
            <xsl:choose>
              <xsl:when test="$days &gt; 0">
                <xsl:value-of select="$days"/>
                <xsl:text>d </xsl:text>
                <xsl:value-of select="format-number($hours, $DecPl0, 'Standard')"/>
                <xsl:text>h</xsl:text>
              </xsl:when>
            </xsl:choose>
          </xsl:when>
          <xsl:otherwise>&#160;&#160;-</xsl:otherwise>
        </xsl:choose>
      </td>

      <th align="left">Calibration age limit</th>
      <td>
        <xsl:choose>
          <xsl:when test="string(number(CalibrationTimeoutPeriod)) != 'NaN'">
            <xsl:variable name="days" select="floor(CalibrationTimeoutPeriod div 3600 div 24)"/>
            <xsl:variable name="hours" select="(CalibrationTimeoutPeriod - $days * 3600 * 24) div 3600"/>
            <xsl:choose>
              <xsl:when test="$days &gt; 0">
                <xsl:value-of select="$days"/>
                <xsl:text>d </xsl:text>
                <xsl:value-of select="format-number($hours, $DecPl0, 'Standard')"/>
                <xsl:text>h</xsl:text>
              </xsl:when>
            </xsl:choose>
          </xsl:when>
          <xsl:otherwise>&#160;&#160;-</xsl:otherwise>
        </xsl:choose>
      </td>

      <th align="left">IMU sensor status</th>
      <td>
        <xsl:choose>
          <xsl:when test="SensorStatus = 'NotInitialised'">Tilt not initialized</xsl:when>
          <xsl:when test="SensorStatus = 'Faulty'">Tilt sensor faulty</xsl:when>
          <xsl:when test="SensorStatus = 'NotCalibrated'">Tilt not calibrated</xsl:when>
          <xsl:when test="SensorStatus = 'CalibratedOk'">Tilt calibrated OK</xsl:when>
          <xsl:when test="SensorStatus = 'Cancelled'">User cancelled</xsl:when>
          <xsl:when test="SensorStatus = 'CalibrationFailed'">Tilt calibration failed</xsl:when>
          <xsl:when test="SensorStatus = 'CalibrationExpired'">Tilt calibration expired</xsl:when>
          <xsl:when test="SensorStatus = 'StateUnrecognized'">Tilt state unrecognised</xsl:when>
        </xsl:choose>
      </td>
    </tr>
  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** SBAS Offset Record Output ******************* -->
<!-- **************************************************************** -->
<xsl:template match="SBASOffsetRecord">

  <xsl:call-template name="StartTable"/>
  <caption align="top"><p align="left">OmniSTAR Offset</p></caption>
    <tr>
      <th align="left">Base</th>
      <td>
        <xsl:value-of select="BasePoint"/>
      </td>

      <th align="left">Rover</th>
      <td>
        <xsl:value-of select="RoverPoint"/>
      </td>

      <th align="left">Date/Time</th>
      <td>
        <xsl:call-template name="FormattedDateString">
          <xsl:with-param name="date" select="substring-before(Date, 'T')"/>
        </xsl:call-template>
        <xsl:value-of select="' '"/>
        <xsl:value-of select="substring-after(Date, 'T')"/>
      </td>

      <th align="left">Note</th>
      <td>
        <xsl:value-of select="ObservationNote"/>
      </td>
    </tr>
    
    <tr>
      <th align="left">σ North</th>
      <td align="right">
        <xsl:value-of select="format-number(SigmaNorth * $DistConvFactor, $DecPl3, 'Standard')"/>
      </td>
      
      <th align="left">σ East</th>
      <td align="right">
        <xsl:value-of select="format-number(SigmaEast * $DistConvFactor, $DecPl3, 'Standard')"/>
      </td>

      <th align="left">σ Elevation</th>
      <td align="right">
        <xsl:value-of select="format-number(SigmaElevation * $DistConvFactor, $DecPl3, 'Standard')"/>
      </td>

      <th>&#160;</th>
      <td>&#160;</td>
    </tr>
    
    <tr>
      <th align="left">Δ Latitude</th>
      <td align="right">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle" select="DeltaLatitude"/>
          <xsl:with-param name="DMSOutput">true</xsl:with-param>
          <xsl:with-param name="secDecPlaces">4</xsl:with-param>
        </xsl:call-template>
      </td>

      <th align="left">Δ Longitude</th>
      <td align="right">
        <xsl:call-template name="FormatAngle">
          <xsl:with-param name="theAngle" select="DeltaLongitude"/>
          <xsl:with-param name="DMSOutput">true</xsl:with-param>
          <xsl:with-param name="secDecPlaces">4</xsl:with-param>
        </xsl:call-template>
      </td>

      <th align="left">Δ Height</th>
      <td align="right">
        <xsl:value-of select="format-number(DeltaHeight * $DistConvFactor, $DecPl3, 'Standard')"/>
      </td>

      <th>&#160;</th>
      <td>&#160;</td>
    </tr>
  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ***** Return the RTX-RTK integrity check as a nice string ****** -->
<!-- **************************************************************** -->
<xsl:template name="RTXRTKIntegrityCheck">
  <xsl:param name="result"/>

  <xsl:choose>
    <xsl:when test="$result = 'NotPerformed'">Not performed</xsl:when>
    <xsl:when test="$result = 'InProgress'">In progress</xsl:when>
    <xsl:when test="$result = 'CompleteOk'">Completed successfully</xsl:when>
    <xsl:when test="$result = 'CompleteFail'">Failed</xsl:when>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******* Return the RTX-RTK offset source as a nice string ****** -->
<!-- **************************************************************** -->
<xsl:template name="RTXRTKOffsetSource">
  <xsl:param name="source"/>

  <xsl:choose>
    <xsl:when test="$source = 'ComputedAtRover'">Computed at Rover</xsl:when>
    <xsl:when test="$source = 'UploadedToRover'">Offset uploaded to Rover</xsl:when>
    <xsl:when test="$source = 'ComputedAtBase'">Computed at Base</xsl:when>
    <xsl:when test="$source = 'UploadedToBase'">Offset uploaded to Base</xsl:when>
    <xsl:when test="$source = 'ComputedInController'">Computed in Controller</xsl:when>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- *************** RTX - RTK Offset Record Output ***************** -->
<!-- **************************************************************** -->
<xsl:template match="RTXRTKOffsetRecord">

  <xsl:call-template name="StartTable"/>
  <caption align="top"><p align="left">RTX - RTK Offset</p></caption>
    <tr>
      <th align="left">Translation X</th>
      <td align="right">
        <xsl:value-of select="format-number(TranslationX * $DistConvFactor, $DecPl3, 'Standard')"/>
      </td>

      <th align="left">Translation Y</th>
      <td align="right">
        <xsl:value-of select="format-number(TranslationY * $DistConvFactor, $DecPl3, 'Standard')"/>
      </td>

      <th align="left">Translation Z</th>
      <td align="right">
        <xsl:value-of select="format-number(TranslationZ * $DistConvFactor, $DecPl3, 'Standard')"/>
      </td>
    </tr>

    <tr>
      <th align="left">σ North</th>
      <td align="right">
        <xsl:value-of select="format-number(SigmaN * $DistConvFactor, $DecPl3, 'Standard')"/>
      </td>

      <th align="left">σ East</th>
      <td align="right">
        <xsl:value-of select="format-number(SigmaE * $DistConvFactor, $DecPl3, 'Standard')"/>
      </td>

      <th align="left">σ Elevation</th>
      <td align="right">
        <xsl:value-of select="format-number(SigmaU * $DistConvFactor, $DecPl3, 'Standard')"/>
      </td>
    </tr>

    <tr>
      <th align="left">Base integrity check</th>
      <td align="left">
        <xsl:call-template name="RTXRTKIntegrityCheck">
          <xsl:with-param name="result" select="BaseCheckResult"/>
        </xsl:call-template>
      </td>

      <th align="left">Rover integrity check</th>
      <td align="left">
        <xsl:call-template name="RTXRTKIntegrityCheck">
          <xsl:with-param name="result" select="RoverCheckResult"/>
        </xsl:call-template>
      </td>

      <th align="left">Rover-Base integrity check</th>
      <td align="left">
        <xsl:call-template name="RTXRTKIntegrityCheck">
          <xsl:with-param name="result" select="BRCheckResult"/>
        </xsl:call-template>
      </td>
    </tr>

    <tr>
      <th align="left">Source of offset</th>
      <td align="left">
        <xsl:call-template name="RTXRTKOffsetSource">
          <xsl:with-param name="source" select="Source"/>
        </xsl:call-template>
      </td>

      <th align="left">ITRF Epoch</th>
      <td align="left">
        <xsl:value-of select="ITRFEpoch"/>
      </td>

      <th>&#160;</th>
      <td>&#160;</td>
    </tr>

    <tr>
      <th align="left">RTK point</th>
      <td align="left">
        <xsl:value-of select="RtkPointId"/>
      </td>

      <th align="left">Base point</th>
      <td align="left">
        <xsl:value-of select="RtkBasePointId"/>
      </td>

      <th align="left">RTX point</th>
      <td align="left">
        <xsl:value-of select="RtxPointId"/>
      </td>
    </tr>
  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- **************** Compute Distance Record Output **************** -->
<!-- **************************************************************** -->
<xsl:template match="ComputeDistanceRecord">

  <xsl:call-template name="StartTable"/>
    <caption align="top">
      <p align="left">
        <xsl:text>Compute distance</xsl:text>
        <xsl:text> (</xsl:text>
          <xsl:choose>
            <xsl:when test="DistanceMethod = 'BetweenTwoPoints'">Between two points</xsl:when>
            <xsl:when test="DistanceMethod = 'BetweenPointAndLine'">Between point and line</xsl:when>
            <xsl:when test="DistanceMethod = 'BetweenPointAndArc'">Between point and arc</xsl:when>
          </xsl:choose>
        <xsl:text>)</xsl:text>
      </p>
    </caption>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">
        <xsl:choose>
          <xsl:when test="DistanceMethod = 'BetweenTwoPoints'">From point</xsl:when>
          <xsl:otherwise>Point</xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Val" select="Point1"/>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">
        <xsl:choose>
          <xsl:when test="DistanceMethod = 'BetweenTwoPoints'">To point</xsl:when>
          <xsl:when test="DistanceMethod = 'BetweenPointAndLine'">Line</xsl:when>
          <xsl:when test="DistanceMethod = 'BetweenPointAndArc'">Arc</xsl:when>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:choose>
          <xsl:when test="DistanceMethod = 'BetweenTwoPoints'"><xsl:value-of select="Point2"/></xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="ReferenceLinearEntity"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">
        <xsl:choose>
          <xsl:when test="DistanceMethod = 'BetweenTwoPoints'">
            <xsl:choose>
              <xsl:when test="DistanceType[.='Grid']">H.Dist (grid)</xsl:when>
              <xsl:when test="DistanceType[.='Ground']">H.Dist (grnd)</xsl:when>
              <xsl:when test="DistanceType[.='Ellipsoid']">H.Dist (ell)</xsl:when>
              <xsl:otherwise>H.Dist (grid)</xsl:otherwise>
            </xsl:choose>
          </xsl:when>
          <xsl:when test="DistanceMethod = 'BetweenPointAndLine'">
            <xsl:choose>
              <xsl:when test="DistanceType[.='Grid']">H.Dist along line (grid)</xsl:when>
              <xsl:when test="DistanceType[.='Ground']">H.Dist along line (grnd)</xsl:when>
              <xsl:when test="DistanceType[.='Ellipsoid']">H.Dist along line (ell)</xsl:when>
              <xsl:otherwise>H.Dist along line (grid)</xsl:otherwise>
            </xsl:choose>
          </xsl:when>
          <xsl:when test="DistanceMethod = 'BetweenPointAndArc'">
            <xsl:choose>
              <xsl:when test="DistanceType[.='Grid']">H.Dist along arc (grid)</xsl:when>
              <xsl:when test="DistanceType[.='Ground']">H.Dist along arc (grnd)</xsl:when>
              <xsl:when test="DistanceType[.='Ellipsoid']">H.Dist along arc (ell)</xsl:when>
              <xsl:otherwise>H.Dist along arc (grid)</xsl:otherwise>
            </xsl:choose>
          </xsl:when>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:choose>
          <xsl:when test="DistanceMethod = 'BetweenTwoPoints'"><xsl:value-of select="format-number(ComputedDistance * $DistConvFactor, $DecPl3, 'Standard')"/></xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number(DistanceAlong * $DistConvFactor, $DecPl3, 'Standard')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:if test="DistanceMethod != 'BetweenTwoPoints'">
      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">
          <xsl:choose>
            <xsl:when test="DistanceMethod = 'BetweenPointAndLine'">
              <xsl:choose>
                <xsl:when test="DistanceType[.='Grid']">H.Dist from line (grid)</xsl:when>
                <xsl:when test="DistanceType[.='Ground']">H.Dist from line (grnd)</xsl:when>
                <xsl:when test="DistanceType[.='Ellipsoid']">H.Dist from line (ell)</xsl:when>
                <xsl:otherwise>H.Dist from line (grid)</xsl:otherwise>
              </xsl:choose>
            </xsl:when>
            <xsl:otherwise>  <!-- Must be BetweenPointAndArc method -->
              <xsl:choose>
                <xsl:when test="DistanceType[.='Grid']">H.Dist from arc (grid)</xsl:when>
                <xsl:when test="DistanceType[.='Ground']">H.Dist from arc (grnd)</xsl:when>
                <xsl:when test="DistanceType[.='Ellipsoid']">H.Dist from arc (ell)</xsl:when>
                <xsl:otherwise>H.Dist from arc (grid)</xsl:otherwise>
              </xsl:choose>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Val" select="format-number(ComputedDistance * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:call-template>
    </xsl:if>
  <xsl:call-template name="EndTable"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************** Panorama Record Output ******************** -->
<!-- **************************************************************** -->
<xsl:template match="PanoramaRecord">

  <!-- Output the panorama details and then the names of the image files associated with it -->
  <xsl:call-template name="StartTable"/>
  <caption align="top"><p align="left">Panorama details</p></caption>
    <tr>
      <th width="10%" align="left">Framing</th>
      <td width="10%" align="left">
        <xsl:choose>
          <xsl:when test="FrameType = 'ScanFullCircle'">Horizontal band</xsl:when>
          <xsl:when test="FrameType = 'ScanRectangle'">Rectangle</xsl:when>
          <xsl:when test="FrameType = 'ScanPolygon'">Polygon</xsl:when>
          <xsl:when test="FrameType = 'ScanFullDome'">Full dome</xsl:when>
        </xsl:choose>
      </td>

      <th width="10%" align="left">At distance</th>
      <td width="10%" align="right">
        <xsl:value-of select="format-number(AtDistance * $DistConvFactor, $DecPl3, 'Standard')"/>
      </td>

      <th width="10%" align="left">Image overlap</th>
      <td width="8%" align="right">
        <xsl:value-of select="concat(Overlap, '%')"/>
      </td>

      <th width="10%" align="left">Resolution</th>
      <td width="11%" align="left">
        <xsl:choose>
          <xsl:when test="Resolution = 0">Overview</xsl:when>
          <xsl:when test="Resolution = 1">Primary</xsl:when>
        </xsl:choose>
      </td>

      <th width="10%" align="left">Fixed exposure</th>
      <td width="11%" align="left">
        <xsl:choose>
          <xsl:when test="FixedExposure = 'true'">Yes</xsl:when>
          <xsl:otherwise>No</xsl:otherwise>
        </xsl:choose>
      </td>
    </tr>
  <xsl:call-template name="EndTable"/>

  <xsl:call-template name="StartTable"/>
    <!-- Now output a list of all the panorama image file names and their horizontal angles -->
    <xsl:variable name="recID" select="@ID"/>
    <xsl:for-each select="following-sibling::ImageRecord[PanoramaID = $recID]">
      <tr>
        <th width="10%" align="left">Image name</th>
        <td width="48%" align="left"><xsl:value-of select="FileName"/></td>
        <th width="10%" align="left">Horizontal angle</th>
        <td width="11%" align="right">
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="HorizontalAngle"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
        </td>
        <th width="10%" align="left">Vertical angle</th>
        <td width="11%" align="right">
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="VerticalAngle"/>
            <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
          </xsl:call-template>
        </td>
      </tr>
    </xsl:for-each>
  <xsl:call-template name="EndTable"/>
  <xsl:call-template name="SeparatingLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************** Scanning Record Output ******************** -->
<!-- **************************************************************** -->
<xsl:template match="ScanningRecord">

  <xsl:if test="not(Deleted) or (Deleted and (Deleted = 'false'))">
    <table border="1" width="100%" cellpadding="2" rules="all">
    <caption align="top"><p align="left">Scan</p></caption>
      <tr>
        <xsl:variable name="stnID" select="StationID"/>
        <xsl:choose>
          <xsl:when test="/JOBFile/FieldBook/StationRecord[@ID = $stnID]/StationType = 'ScanStation'">
            <th width="10%" align="left">Scan station</th>
            <td width="10%" align="left">
              <xsl:value-of select="/JOBFile/FieldBook/StationRecord[@ID = $stnID]/StationName"/>
            </td>
            <td width="10%" align="left">&#160;</td>
            <td width="10%" align="left">&#160;</td>
          </xsl:when>
          <xsl:otherwise>  <!-- Standard station setup -->
            <th width="10%" align="left">Station</th>
            <td width="10%" align="left">
              <xsl:value-of select="/JOBFile/FieldBook/StationRecord[@ID = $stnID]/StationName"/>
            </td>
            <th width="10%" align="left">Backsight point</th>
            <td width="10%" align="left">
              <xsl:variable name="bsID" select="BackBearingID"/>
              <xsl:value-of select="/JOBFile/FieldBook/BackBearingRecord[@ID = $bsID]/BackSight"/>
            </td>
          </xsl:otherwise>
        </xsl:choose>
        <th width="10%" align="left">Framing</th>
        <td width="10%" align="left">
          <xsl:choose>
            <xsl:when test="Type = 'ScanFullCircle'">Horizontal band</xsl:when>
            <xsl:when test="Type = 'ScanRectangle'">Rectangle</xsl:when>
            <xsl:when test="Type = 'ScanPolygon'">Polygon</xsl:when>
            <xsl:when test="Type = 'ScanFullDome'">Full dome</xsl:when>
          </xsl:choose>
        </td>
        <th width="10%" align="left">Name</th>
        <td width="10%" align="left">
          <xsl:choose>
            <xsl:when test="Name != ''"><xsl:value-of select="Name"/></xsl:when>
            <xsl:otherwise>&#160;</xsl:otherwise>
          </xsl:choose>
        </td>
        <th width="10%" align="left">Number of points</th>
        <td width="10%" align="right"><xsl:value-of select="NumberOfPoints"/></td>
      </tr>
      <tr>
        <th align="left">Scan file</th>
        <td colspan="7" align="left">
          <xsl:value-of select="FileName"/>
        </td>
        <xsl:choose>
          <xsl:when test="PPM">
            <th align="left">PPM</th>
            <td align="right"><xsl:value-of select="format-number(PPM, $DecPl1, 'Standard')"/></td>
          </xsl:when>
          <xsl:otherwise>
            <th>&#160;</th>
            <td>&#160;</td>
          </xsl:otherwise>
        </xsl:choose>
      </tr>
      <xsl:if test="TiltChangeWarningGiven and TiltChange">
        <tr>
          <th align="left">Scan density</th>
          <td align="left">
            <xsl:choose>
              <xsl:when test="substring(HorizontalStep, 1, 8) = '0.057295'">Coarse</xsl:when>
              <xsl:when test="substring(HorizontalStep, 1, 8) = '0.028647'">Standard</xsl:when>
              <xsl:when test="substring(HorizontalStep, 1, 8) = '0.014323'">Fine</xsl:when>
              <xsl:when test="substring(HorizontalStep, 1, 8) = '0.007161'">Superfine</xsl:when>
            </xsl:choose>
          </td>
          <th align="left">Tilt error</th>
          <td align="left">
            <xsl:choose>
              <xsl:when test="TiltChangeWarningGiven = 'true'">Yes</xsl:when>
              <xsl:otherwise>No</xsl:otherwise>
            </xsl:choose>
          </td>
          <th colspan="2" align="left">Maximum tilt change</th>
          <td align="left">
            <xsl:call-template name="FormatAngle">
              <xsl:with-param name="theAngle" select="TiltChange"/>
              <xsl:with-param name="gonsDecPlaces" select="$gonsDecPlaces"/>
            </xsl:call-template>
          </td>
          <td>&#160;</td>
          <td>&#160;</td>
          <td>&#160;</td>
        </tr>
      </xsl:if>
    </table>
    <xsl:call-template name="SeparatingLine"/>
  </xsl:if>
</xsl:template>


<!-- **************************************************************** -->
<!-- *** Determines appropriate output width and height for image *** -->
<!-- **************************************************************** -->
<xsl:template name="SetImageWidthAndHeight">
  <xsl:param name="width"/>
  <xsl:param name="height"/>
  
  <!-- Ensure that the image is not output any wider than 600 pixels as this is a good  -->
  <!-- width for presentation purposes when exporting as a pdf file. If the image is    -->
  <!-- wider than 600 pixels then it needs to be scaled down to 600 pixels wide and the -->
  <!-- same scale ratio applied to the image height. If the image is less than 600      -->
  <!-- pixels wide then it doesn't need to be scaled.                                   -->
  <xsl:variable name="outputWidth">
    <xsl:choose>
      <xsl:when test="($width = 0) or ($width = '')">600</xsl:when>
      <xsl:when test="$width &lt; 600">
        <xsl:value-of select="$width"/>
      </xsl:when>
      <xsl:otherwise>600</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="ratio">
    <xsl:choose>
      <xsl:when test="($width = 0) or ($width = '') or ($width &lt; 600)">1.0</xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$outputWidth div $width"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="outputHeight">
    <xsl:choose>
      <xsl:when test="($height = 0) or ($height = '')">480</xsl:when>  <!-- Defaulting to 480 to give scaled default of 800 x 640 image -->
      <xsl:otherwise>
        <xsl:value-of select="round($ratio * $height)"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:element name="width" namespace="">
    <xsl:value-of select="$outputWidth"/>
  </xsl:element>

  <xsl:element name="height" namespace="">
    <xsl:value-of select="$outputHeight"/>
  </xsl:element>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************** MediaFile Record Output ******************* -->
<!-- **************************************************************** -->
<xsl:template match="MediaFileRecord">

  <xsl:variable name="referencedPoint">
    <xsl:if test="RecordID != ''">
      <xsl:variable name="recID" select="RecordID"/>
      <xsl:for-each select="/JOBFile/FieldBook/PointRecord[@ID = $recID]">
        <xsl:value-of select="Name"/>
      </xsl:for-each>
    </xsl:if>
  </xsl:variable>

  <xsl:variable name="outputSize">
    <xsl:call-template name="SetImageWidthAndHeight">
      <xsl:with-param name="width" select="Width"/>
      <xsl:with-param name="height" select="Height"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:if test="FileName != ''">
    <xsl:variable name="displayFileName" select="translate(FileName, '/', '\')"/>  <!-- Ensure path separating chars are backslashes -->
    <h3>
      <xsl:if test="$referencedPoint != ''">
        <xsl:text>Reference point: </xsl:text>
        <xsl:value-of select="$referencedPoint"/>
        <xsl:value-of select="' '"/>
      </xsl:if>
      <xsl:text>File name:</xsl:text><xsl:value-of select="substring-after($displayFileName, '\')"/>
      <xsl:for-each select="Notes/Note">
        <br/><xsl:value-of select="."/>
      </xsl:for-each>
    </h3>
    <xsl:call-template name="NewLine"/>
    <!-- Ensure any backslashes are changed to forward slashes in the file name to be  -->
    <!-- used as the file reference. Backslashes are changes to %5C in the output file -->
    <!-- reference in the html file and the Chrome browser doesn't like this.          -->
    <xsl:variable name="refFilePath" select="translate(FileName, '\', '/')"/>
    <!-- Use a table to provide a border (defining a border in the <img> element doesn't come through to pdf file) -->
    <table class="border">
      <tr>
        <th><img src="{$refFilePath}" width="{msxsl:node-set($outputSize)/width}" height="{msxsl:node-set($outputSize)/height}"/></th>
      </tr>
    </table>
    <xsl:call-template name="NewLine"/>
    <br/>
  </xsl:if>

  <xsl:if test="FileNameModified != ''">
    <xsl:variable name="displayFileName" select="translate(FileNameModified, '/', '\')"/>  <!-- Ensure path separating chars are backslashes -->
    <h3>
      <xsl:if test="$referencedPoint != ''">
        <xsl:text>Reference point: </xsl:text>
        <xsl:value-of select="$referencedPoint"/>
        <xsl:value-of select="' '"/>
      </xsl:if>
      <xsl:text>File name:</xsl:text><xsl:value-of select="substring-after($displayFileName, '\')"/>
      <xsl:for-each select="Notes/Note">
        <br/><xsl:value-of select="."/>
      </xsl:for-each>
    </h3>
    <xsl:call-template name="NewLine"/>
    <!-- Ensure any backslashes are changed to forward slashes in the file name to be  -->
    <!-- used as the file reference. Backslashes are changes to %5C in the output file -->
    <!-- reference in the html file and the Chrome browser doesn't like this.          -->
    <xsl:variable name="refFilePath" select="translate(FileNameModified, '\', '/')"/>
    <!-- Use a table to provide a border (defining a border in the <img> element doesn't come through to pdf file) -->
    <table class="border">
      <tr>
        <th><img src="{$refFilePath}" width="{msxsl:node-set($outputSize)/width}" height="{msxsl:node-set($outputSize)/height}"/></th>
      </tr>
    </table>
    <xsl:call-template name="NewLine"/>
    <br/>
  </xsl:if>

</xsl:template>


<!-- **************************************************************** -->
<!-- ********************** Image Record Output ********************* -->
<!-- **************************************************************** -->
<xsl:template match="ImageRecord">

  <xsl:variable name="referencedPoint">
    <xsl:choose>
      <xsl:when test="PointRecordID != ''">
        <xsl:variable name="recID" select="RecordID"/>
        <xsl:for-each select="/JOBFile/FieldBook/PointRecord[@ID = $recID]">
          <xsl:value-of select="Name"/>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="recID" select="StationID"/>
        <xsl:for-each select="/JOBFile/FieldBook/StationRecord[@ID = $recID]">
          <xsl:value-of select="StationName"/>
        </xsl:for-each>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  
  <xsl:variable name="refPtText">
    <xsl:choose>
      <xsl:when test="PointRecordID != ''"><xsl:text>Reference point: </xsl:text></xsl:when>
      <xsl:otherwise><xsl:text>Station: </xsl:text></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="outputSize">
    <xsl:call-template name="SetImageWidthAndHeight">
      <xsl:with-param name="width" select="Width"/>
      <xsl:with-param name="height" select="Height"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:if test="FileName != ''">
    <xsl:variable name="displayFileName" select="translate(FileName, '/', '\')"/>  <!-- Ensure path separating chars are backslashes -->
    <h3>
      <xsl:value-of select="$refPtText"/>
      <xsl:value-of select="$referencedPoint"/>
      <xsl:value-of select="' '"/>
      <xsl:text>File name:</xsl:text><xsl:value-of select="substring-after($displayFileName, '\')"/>
      <xsl:for-each select="Notes/Note">
        <br/><xsl:value-of select="."/>
      </xsl:for-each>
    </h3>
    <xsl:call-template name="NewLine"/>
    <!-- Ensure any backslashes are changed to forward slashes in the file name to be  -->
    <!-- used as the file reference. Backslashes are changes to %5C in the output file -->
    <!-- reference in the html file and the Chrome browser doesn't like this.          -->
    <xsl:variable name="refFilePath" select="translate(FileName, '\', '/')"/>
    <!-- Use a table to provide a border (defining a border in the <img> element doesn't come through to pdf file) -->
    <table class="border">
      <tr>
        <th><img src="{$refFilePath}" width="{msxsl:node-set($outputSize)/width}" height="{msxsl:node-set($outputSize)/height}"/></th>
      </tr>
    </table>
    <xsl:call-template name="NewLine"/>
    <br/>
  </xsl:if>

  <xsl:if test="FileNameModified != ''">
    <xsl:variable name="displayFileName" select="translate(FileNameModified, '/', '\')"/>  <!-- Ensure path separating chars are backslashes -->
    <h3>
      <xsl:value-of select="$refPtText"/>
      <xsl:value-of select="$referencedPoint"/>
      <xsl:value-of select="' '"/>
      <xsl:text>File name:</xsl:text><xsl:value-of select="substring-after($displayFileName, '\')"/>
      <xsl:for-each select="Notes/Note">
        <br/><xsl:value-of select="."/>
      </xsl:for-each>
    </h3>
    <xsl:call-template name="NewLine"/>
    <!-- Ensure any backslashes are changed to forward slashes in the file name to be  -->
    <!-- used as the file reference. Backslashes are changes to %5C in the output file -->
    <!-- reference in the html file and the Chrome browser doesn't like this.          -->
    <xsl:variable name="refFilePath" select="translate(FileNameModified, '\', '/')"/>
    <!-- Use a table to provide a border (defining a border in the <img> element doesn't come through to pdf file) -->
    <table class="border">
      <tr>
        <th><img src="{$refFilePath}" width="{msxsl:node-set($outputSize)/width}" height="{msxsl:node-set($outputSize)/height}"/></th>
      </tr>
    </table>
    <xsl:call-template name="NewLine"/>
    <br/>
  </xsl:if>

</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** Create Formatted Date String ***************** -->
<!-- **************************************************************** -->
<xsl:template name="FormattedDateString">
  <xsl:param name="date"/>

  <xsl:variable name="month" select="substring($date, 6, 2)"/>

  <xsl:variable name="namedMonth">
    <xsl:choose>
      <xsl:when test="number($month) = 1">Jan</xsl:when>
      <xsl:when test="number($month) = 2">Feb</xsl:when>
      <xsl:when test="number($month) = 3">Mar</xsl:when>
      <xsl:when test="number($month) = 4">Apr</xsl:when>
      <xsl:when test="number($month) = 5">May</xsl:when>
      <xsl:when test="number($month) = 6">Jun</xsl:when>
      <xsl:when test="number($month) = 7">Jul</xsl:when>
      <xsl:when test="number($month) = 8">Aug</xsl:when>
      <xsl:when test="number($month) = 9">Sep</xsl:when>
      <xsl:when test="number($month) = 10">Oct</xsl:when>
      <xsl:when test="number($month) = 11">Nov</xsl:when>
      <xsl:when test="number($month) = 12">Dec</xsl:when>
    </xsl:choose>
  </xsl:variable>

  <xsl:choose>
    <xsl:when test="substring($date, 9, 1) = '0'">
      <xsl:value-of select="substring($date, 10, 1)"/>  <!-- Skip leading 0 -->
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="substring($date, 9, 2)"/>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:value-of select="' '"/>
  <xsl:value-of select="$namedMonth"/>
  <xsl:value-of select="' '"/>
  <xsl:value-of select="substring($date, 1, 4)"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** Reductions Point Output ********************* -->
<!-- **************************************************************** -->
<xsl:template match="Point">
  <xsl:call-template name="GridPoint">
    <xsl:with-param name="reductionsPoint" select="'true'"/>
  </xsl:call-template>
</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** Separating Line Output ********************** -->
<!-- **************************************************************** -->
<xsl:template name="SeparatingLine">
  <xsl:choose>
    <xsl:when test="$IncludeBorders != $YesStr">  <!-- Only include separating lines -->
      <hr></hr>                                   <!-- if there are no table borders -->
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="BlankLine"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************************ Output Table Line ********************* -->
<!-- **************************************************************** -->
<xsl:template name="OutputTableLine">
  <xsl:param name="Hdr1" select="''"/>
  <xsl:param name="Val1" select="''"/>
  <xsl:param name="Hdr2" select="''"/>
  <xsl:param name="Val2" select="''"/>
  <xsl:param name="Hdr3" select="''"/>
  <xsl:param name="Val3" select="''"/>
  <xsl:param name="Hdr4" select="''"/>
  <xsl:param name="Val4" select="''"/>
  <xsl:param name="Hdr5" select="''"/>
  <xsl:param name="Val5" select="''"/>

  <xsl:variable name="col1Span">
    <xsl:choose>
      <xsl:when test="$Val1 = ''">1</xsl:when>
      <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <tr>
    <th width="9%" align="left"><xsl:value-of select="$Hdr1"/></th>
    <td width="11%" align="right"><xsl:value-of select="$Val1"/></td>
    <th width="8%" align="left"><xsl:value-of select="$Hdr2"/></th>
    <td width="13%" align="right"><xsl:value-of select="$Val2"/></td>
    <th width="8%" align="left"><xsl:value-of select="$Hdr3"/></th>
    <td width="13%" align="right"><xsl:value-of select="$Val3"/></td>
    <th width="8%" align="left"><xsl:value-of select="$Hdr4"/></th>
    <td width="11%" align="right"><xsl:value-of select="$Val4"/></td>
    <th width="8%" align="left"><xsl:value-of select="$Hdr5"/></th>
    <td width="11%" align="right"><xsl:value-of select="$Val5"/></td>
  </tr>
  
</xsl:template>


<!-- **************************************************************** -->
<!-- ************** Output Single Element Table Line **************** -->
<!-- **************************************************************** -->
<xsl:template name="OutputSingleElementTableLine">
  <xsl:param name="Hdr" select="''"/>
  <xsl:param name="Val" select="''"/>
  <xsl:param name="spanExtraFields" select="'false'"/>
  
  <xsl:variable name="colsToSpan">
    <xsl:choose>
      <xsl:when test="$spanExtraFields = 'true'">9</xsl:when>
      <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <tr>
    <th width="20%" align="left"><xsl:value-of select="$Hdr"/></th>
    <td width="80%" align="left" colspan="{$colsToSpan}"><xsl:value-of select="$Val"/></td>
  </tr>
  
</xsl:template>


<!-- **************************************************************** -->
<!-- ********************* Blank Line Output ************************ -->
<!-- **************************************************************** -->
<xsl:template name="BlankLine">
  <xsl:value-of select="string(' ')"/>
  <br/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************************* Start Table ************************** -->
<!-- **************************************************************** -->
<xsl:template name="StartTable">
  <xsl:choose>
    <xsl:when test="$IncludeBorders = $YesStr">
      <xsl:value-of disable-output-escaping="yes" select="'&lt;table class=border width=100% cellpadding=2 rules=cols&gt;'"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of disable-output-escaping="yes" select="'&lt;table class=noBborder width=100% cellpadding=2 rules=cols&gt;'"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************************** End Table *************************** -->
<!-- **************************************************************** -->
<xsl:template name="EndTable">
  <xsl:value-of disable-output-escaping="yes" select="'&lt;/table&gt;'"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** Return the ECEF X value ********************* -->
<!-- **************************************************************** -->
<xsl:template name="ecefX">
  <xsl:param name="latitude"/>   <!-- latitude value required in decimal degrees -->
  <xsl:param name="longitude"/>  <!-- longitude value required in decimal degrees -->
  <xsl:param name="height"/>

  <!-- Get the latitude in radians -->
  <xsl:variable name="radLat">
    <xsl:call-template name="AngleInRadians">
      <xsl:with-param name="theAngle" select="$latitude"/>
    </xsl:call-template>
  </xsl:variable>

  <!-- Get the longitude in radians -->
  <xsl:variable name="radLong">
    <xsl:call-template name="AngleInRadians">
      <xsl:with-param name="theAngle" select="$longitude"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="N">
    <xsl:call-template name="RadiusOfCurvature">
      <xsl:with-param name="latitude" select="$radLat"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="cosLat">
    <!-- Cosine of latitude = sin(Pi / 2 - latitude) -->
    <xsl:call-template name="Sine">
      <xsl:with-param name="theAngle" select="$Pi div 2.0 - $radLat"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="cosLong">
    <!-- Cosine of longitude = sin(Pi / 2 - longitude) -->
    <xsl:call-template name="Sine">
      <xsl:with-param name="theAngle" select="$Pi div 2.0 - $radLong"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:value-of select="($N + $height) * $cosLat * $cosLong"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** Return the ECEF Y value ********************* -->
<!-- **************************************************************** -->
<xsl:template name="ecefY">
  <xsl:param name="latitude"/>   <!-- latitude value required in decimal degrees -->
  <xsl:param name="longitude"/>  <!-- longitude value required in decimal degrees -->
  <xsl:param name="height"/>

  <!-- Get the latitude in radians -->
  <xsl:variable name="radLat">
    <xsl:call-template name="AngleInRadians">
      <xsl:with-param name="theAngle" select="$latitude"/>
    </xsl:call-template>
  </xsl:variable>

  <!-- Get the longitude in radians -->
  <xsl:variable name="radLong">
    <xsl:call-template name="AngleInRadians">
      <xsl:with-param name="theAngle" select="$longitude"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="N">
    <xsl:call-template name="RadiusOfCurvature">
      <xsl:with-param name="latitude" select="$radLat"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="cosLat">
    <!-- Cosine of latitude = sin(Pi / 2 - latitude) -->
    <xsl:call-template name="Sine">
      <xsl:with-param name="theAngle" select="$Pi div 2.0 - $radLat"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="sinLong">
    <xsl:call-template name="Sine">
      <xsl:with-param name="theAngle" select="$radLong"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:value-of select="($N + $height) * $cosLat * $sinLong"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** Return the ECEF Z value ********************* -->
<!-- **************************************************************** -->
<xsl:template name="ecefZ">
  <xsl:param name="latitude"/>  <!-- latitude value required in decimal degrees -->  
  <xsl:param name="height"/>

  <!-- Get the latitude in radians -->
  <xsl:variable name="radLat">
    <xsl:call-template name="AngleInRadians">
      <xsl:with-param name="theAngle" select="$latitude"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="N">
    <xsl:call-template name="RadiusOfCurvature">
      <xsl:with-param name="latitude" select="$radLat"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="sinLat">
    <xsl:call-template name="Sine">
      <xsl:with-param name="theAngle" select="$radLat"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:value-of select="($N * (1.0 - $WGS84EccentricitySquared) + $height) * $sinLat"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ********** Compute the Latitude from the ECEF X,Y,Z ************ -->
<!-- **************************************************************** -->
<xsl:template name="LatFromXYZ">  <!-- Returns latitude value in radians -->
  <xsl:param name="X"/>
  <xsl:param name="Y"/>
  <xsl:param name="Z"/>

  <xsl:variable name="p">
    <xsl:call-template name="pVar">
      <xsl:with-param name="X" select="$X"/>
      <xsl:with-param name="Y" select="$Y"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="r">
    <xsl:call-template name="Sqrt">
      <xsl:with-param name="num" select="$p * $p + $Z * $Z"/>
      <xsl:with-param name="maxiter" select="30"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="oneMinusf" select="1 - 1 div $WGS84Flattening"/>

  <xsl:variable name="u">
    <xsl:call-template name="ArcTanSeries">
      <xsl:with-param name="tanVal" select="($Z div $p) *
                                            ($oneMinusf + $WGS84EccentricitySquared * $WGS84SemiMajorAxis div $r)"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="sinu">
    <xsl:call-template name="Sine">
      <xsl:with-param name="theAngle" select="$u"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="sinuCubed" select="$sinu * $sinu * $sinu"/>

  <xsl:variable name="cosu">
    <xsl:call-template name="Cosine">
      <xsl:with-param name="theAngle" select="$u"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="cosuCubed" select="$cosu * $cosu * $cosu"/>

  <xsl:variable name="rawLat">
    <xsl:call-template name="ArcTanSeries">
      <xsl:with-param name="tanVal" select="($Z * $oneMinusf + $WGS84EccentricitySquared * $WGS84SemiMajorAxis * $sinuCubed) div
                                            ($oneMinusf * ($p - $WGS84EccentricitySquared * $WGS84SemiMajorAxis * $cosuCubed))"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:call-template name="RadianAngleBetweenLimits">
    <xsl:with-param name="anAngle" select="$rawLat"/>
    <xsl:with-param name="minVal" select="-1 * $halfPi"/>
    <xsl:with-param name="maxVal" select="$halfPi"/>
    <xsl:with-param name="incVal" select="$halfPi"/>
  </xsl:call-template>
  <!-- Returns latitude value in radians -->
</xsl:template>


<!-- **************************************************************** -->
<!-- ********** Compute the Longitude from the ECEF X,Y,Z *********** -->
<!-- **************************************************************** -->
<xsl:template name="LongFromXYZ">  <!-- Returns longitude value in radians -->
  <xsl:param name="X"/>
  <xsl:param name="Y"/>

  <xsl:variable name="absX" select="concat(substring('-',2 - ($X &lt; 0)), '1') * $X"/>
  <xsl:variable name="absY" select="concat(substring('-',2 - ($Y &lt; 0)), '1') * $Y"/>

  <xsl:variable name="rawLong">
    <xsl:call-template name="ArcTanSeries">
      <xsl:with-param name="tanVal" select="$absY div $absX"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="rawLong1">
    <xsl:choose>
      <xsl:when test="$X &lt; 0.0">
        <xsl:value-of select="$Pi - $rawLong"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$rawLong"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="rawLong2">
    <xsl:choose>
      <xsl:when test="$Y &lt; 0.0">
        <xsl:value-of select="-1.0 * $rawLong1"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$rawLong1"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:call-template name="RadianAngleBetweenLimits">
    <xsl:with-param name="anAngle" select="$rawLong2"/>
    <xsl:with-param name="minVal" select="-1 * $Pi"/>
    <xsl:with-param name="maxVal" select="$Pi"/>
    <xsl:with-param name="incVal" select="2.0 * $Pi"/>
  </xsl:call-template>
  <!-- Returns longitude value in radians -->
</xsl:template>


<!-- **************************************************************** -->
<!-- ************ Compute the Height from the ECEF X,Y,Z ************ -->
<!-- **************************************************************** -->
<xsl:template name="HeightFromXYZ">
  <xsl:param name="X"/>
  <xsl:param name="Y"/>
  <xsl:param name="Z"/>
  <xsl:param name="lat"/>  <!-- lat value required in radians -->

  <xsl:variable name="p">
    <xsl:call-template name="pVar">
      <xsl:with-param name="X" select="$X"/>
      <xsl:with-param name="Y" select="$Y"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="cosLat">
    <xsl:call-template name="Cosine">
      <xsl:with-param name="theAngle" select="$lat"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="sinLat">
    <xsl:call-template name="Sine">
      <xsl:with-param name="theAngle" select="$lat"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="tempVal">
    <xsl:call-template name="Sqrt">
      <xsl:with-param name="num" select="1.0 - $WGS84EccentricitySquared * $sinLat * $sinLat"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:value-of select="$p * $cosLat + $Z * $sinLat - $WGS84SemiMajorAxis * $tempVal"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************ Return the sine of an angle in radians ************ -->
<!-- **************************************************************** -->
<xsl:template name="Sine">
  <xsl:param name="theAngle"/>
  <xsl:variable name="normalisedAngle">
    <xsl:call-template name="RadianAngleBetweenLimits">
      <xsl:with-param name="anAngle" select="$theAngle"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="theSine">
    <xsl:call-template name="sineIter">
      <xsl:with-param name="pX2" select="$normalisedAngle * $normalisedAngle"/>
      <xsl:with-param name="pRslt" select="$normalisedAngle"/>
      <xsl:with-param name="pElem" select="$normalisedAngle"/>
      <xsl:with-param name="pN" select="1"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:value-of select="number($theSine)"/>
</xsl:template>

<xsl:template name="sineIter">
  <xsl:param name="pX2"/>
  <xsl:param name="pRslt"/>
  <xsl:param name="pElem"/>
  <xsl:param name="pN"/>
  <xsl:param name="pEps" select="0.00000001"/>
  <xsl:variable name="vnextN" select="$pN+2"/>
  <xsl:variable name="vnewElem"  select="-$pElem*$pX2 div ($vnextN*($vnextN - 1))"/>
  <xsl:variable name="vnewResult" select="$pRslt + $vnewElem"/>
  <xsl:variable name="vdiffResult" select="$vnewResult - $pRslt"/>
  <xsl:choose>
    <xsl:when test="$vdiffResult > $pEps or $vdiffResult &lt; -$pEps">
      <xsl:call-template name="sineIter">
        <xsl:with-param name="pX2" select="$pX2"/>
        <xsl:with-param name="pRslt" select="$vnewResult"/>
        <xsl:with-param name="pElem" select="$vnewElem"/>
        <xsl:with-param name="pN" select="$vnextN"/>
        <xsl:with-param name="pEps" select="$pEps"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$vnewResult"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- *********** Return the Cosine of an angle in radians *********** -->
<!-- **************************************************************** -->
<xsl:template name="Cosine">
  <xsl:param name="theAngle"/>

  <!-- Use the sine function after subtracting the angle from halfPi -->
  <xsl:call-template name="Sine">
    <xsl:with-param name="theAngle" select="$halfPi - $theAngle"/>
  </xsl:call-template>
</xsl:template>


<!-- **************************************************************** -->
<!-- *************** Return the square root of a value ************** -->
<!-- **************************************************************** -->
<xsl:template name="Sqrt">
  <xsl:param name="num" select="0"/>       <!-- The number you want to find the square root of -->
  <xsl:param name="try" select="1"/>       <!-- The current 'try'.  This is used internally. -->
  <xsl:param name="iter" select="1"/>      <!-- The current iteration, checked against maxiter to limit loop count - used internally -->
  <xsl:param name="maxiter" select="40"/>  <!-- Set this up to insure against infinite loops - used internally -->

  <!-- This template uses Sir Isaac Newton's method of finding roots -->

  <xsl:choose>
    <xsl:when test="$num &lt; 0"></xsl:when>  <!-- Invalid input - no square root of a negative number so return null -->
    <xsl:when test="$try * $try = $num or $iter &gt; $maxiter">
      <xsl:value-of select="$try"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="Sqrt">
        <xsl:with-param name="num" select="$num"/>
        <xsl:with-param name="try" select="$try - (($try * $try - $num) div (2 * $try))"/>
        <xsl:with-param name="iter" select="$iter + 1"/>
        <xsl:with-param name="maxiter" select="$maxiter"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ***** Return Angle between 0 and 360 or -180 to 180 degrees **** -->
<!-- **************************************************************** -->
<xsl:template name="NormalisedAngle">
  <xsl:param name="angle"/>
  <xsl:param name="plusMinus180" select="'false'"/>

  <xsl:variable name="fullCircleAngle">
    <xsl:choose>
      <xsl:when test="$angle &lt; 0">
        <xsl:variable name="newAngle">
          <xsl:value-of select="$angle + 360.0"/>
        </xsl:variable>
        <xsl:call-template name="NormalisedAngle">
          <xsl:with-param name="angle" select="$newAngle"/>
        </xsl:call-template>
      </xsl:when>

      <xsl:when test="$angle &gt;= 360.0">
        <xsl:variable name="newAngle">
          <xsl:value-of select="$angle - 360.0"/>
        </xsl:variable>
        <xsl:call-template name="NormalisedAngle">
          <xsl:with-param name="angle" select="$newAngle"/>
        </xsl:call-template>
      </xsl:when>

      <xsl:otherwise>
        <xsl:value-of select="$angle"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:choose>
    <xsl:when test="$plusMinus180 = 'false'">
      <xsl:value-of select="$fullCircleAngle"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:choose>
        <xsl:when test="$fullCircleAngle &lt;= 180.0">
          <xsl:value-of select="$fullCircleAngle"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$fullCircleAngle - 360.0"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- *************** Return Formatted Station Value ***************** -->
<!-- **************************************************************** -->
<xsl:template name="FormatStationVal">
  <xsl:param name="stationVal"/>
  <xsl:param name="zoneVal" select="''"/>
  <xsl:param name="definedFmt" select="''"/>
  <xsl:param name="stationIndexIncrement" select="''"/>
  <xsl:param name="decPlDefnStr" select="''"/>
  <xsl:param name="includeUnitsAbbrev" select="'false'"/>
  <xsl:param name="distUnitAbbrev" select="''"/>

  <xsl:variable name="decPl">
    <xsl:choose>
      <xsl:when test="$decPlDefnStr != ''">
        <xsl:value-of select="$decPlDefnStr"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$DecPl3"/>  <!-- Default to 3 decimal places -->
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:choose>
    <xsl:when test="string(number($stationVal)) = 'NaN'">
      <xsl:value-of select="format-number($stationVal, $decPl, 'Standard')"/>  <!-- Return appropriate formatted null value -->
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="formatStyle">
        <xsl:choose>
          <xsl:when test="$definedFmt = ''">
            <xsl:value-of select="/JOBFile/Environment/DisplaySettings/StationingFormat"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$definedFmt"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <xsl:variable name="stnIndexIncrement">
        <xsl:choose>
          <xsl:when test="string(number($stationIndexIncrement)) = 'NaN'">
            <xsl:value-of select="/JOBFile/Environment/DisplaySettings/StationIndexIncrement"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$stationIndexIncrement"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <xsl:variable name="stnVal" select="format-number($stationVal * $DistConvFactor, $decPl, 'Standard')"/>
      <xsl:variable name="signChar">
        <xsl:if test="$stnVal &lt; 0.0">-</xsl:if>
      </xsl:variable>

      <xsl:variable name="absStnVal" select="concat(substring('-',2 - ($stnVal &lt; 0)), '1') * $stnVal"/>

      <xsl:variable name="intPart" select="substring-before(format-number($absStnVal, $DecPl3, 'Standard'), '.')"/>
      <xsl:variable name="decPart" select="substring-after($stnVal, '.')"/>

      <xsl:if test="$formatStyle = '1000.0'">
        <xsl:value-of select="$stnVal"/>
      </xsl:if>

      <xsl:if test="$formatStyle = '10+00.0'">
        <xsl:choose>
          <xsl:when test="string-length($intPart) &gt; 2">
            <xsl:value-of select="concat($signChar, substring($intPart, 1, string-length($intPart) - 2),
                                         '+', substring($intPart, string-length($intPart) - 1, 2))"/>
            <xsl:if test="$decPart != ''">
              <xsl:text>.</xsl:text>
              <xsl:value-of select="$decPart"/>
            </xsl:if>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="concat($signChar, '0+', substring('00', 1, 2 - string-length($intPart)), $intPart)"/>
            <xsl:if test="$decPart != ''">
              <xsl:text>.</xsl:text>
              <xsl:value-of select="$decPart"/>
            </xsl:if>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:if>

      <xsl:if test="$formatStyle = '1+000.0'">
        <xsl:choose>
          <xsl:when test="string-length($intPart) &gt; 3">
            <xsl:value-of select="concat($signChar, substring($intPart, 1, string-length($intPart) - 3),
                                         '+', substring($intPart, string-length($intPart) - 2, 3))"/>
            <xsl:if test="$decPart != ''">
              <xsl:text>.</xsl:text>
              <xsl:value-of select="$decPart"/>
            </xsl:if>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="concat($signChar, '0+', substring('000', 1, 3 - string-length($intPart)), $intPart)"/>
            <xsl:if test="$decPart != ''">
              <xsl:text>.</xsl:text>
              <xsl:value-of select="$decPart"/>
            </xsl:if>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:if>

      <xsl:if test="$formatStyle = 'StationIndex'">
        <xsl:variable name="intIncrement" select="format-number($stnIndexIncrement * $DistConvFactor, $DecPl0, 'Standard')"/>

        <xsl:variable name="afterPlusDigits" select="string-length($intIncrement)"/>
        <xsl:variable name="afterPlusZeros" select="substring('000000000000', 1, $afterPlusDigits)"/>
        <xsl:variable name="afterPlusFmt" select="concat($afterPlusZeros, '.', substring-after($decPl, '.'))"/>

        <xsl:variable name="beforePlus" select="floor($absStnVal div ($stnIndexIncrement * $DistConvFactor))"/>
        <xsl:variable name="afterPlus" select="$absStnVal - $beforePlus * ($stnIndexIncrement * $DistConvFactor)"/>
        <xsl:value-of select="concat($signChar, format-number($beforePlus, '#0'), '+', format-number($afterPlus, $afterPlusFmt, 'Standard'))"/>
      </xsl:if>

      <xsl:if test="$includeUnitsAbbrev != 'false'">
        <xsl:value-of select="$distUnitAbbrev"/>
      </xsl:if>

      <xsl:if test="$zoneVal != ''">
        <xsl:value-of select="':'"/>
        <xsl:value-of select="format-number($zoneVal,'0')"/>
      </xsl:if>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************ Output Angle in Appropriate Format **************** -->
<!-- **************************************************************** -->
<xsl:template name="FormatAngle">
  <xsl:param name="theAngle"/>
  <xsl:param name="secDecPlaces" select="0"/>
  <xsl:param name="DMSOutput" select="'false'"/>  <!-- Can be used to force DMS output -->
  <xsl:param name="useSymbols" select="'true'"/>
  <xsl:param name="impliedDecimalPt" select="'false'"/>
  <xsl:param name="gonsDecPlaces" select="5"/>    <!-- Decimal places for gons output -->
  <xsl:param name="decDegDecPlaces" select="5"/>  <!-- Decimal places for decimal degrees output -->
  <xsl:param name="outputAsMilligonsOrSecs" select="'false'"/>
  <xsl:param name="outputAsMilligonsOrSecsSqrd" select="'false'"/>
  <xsl:param name="dmsSymbols">&#0176;'"</xsl:param>

  <xsl:variable name="gonsDecPl">
    <xsl:choose>
      <xsl:when test="$gonsDecPlaces = 1"><xsl:value-of select="$DecPl1"/></xsl:when>
      <xsl:when test="$gonsDecPlaces = 2"><xsl:value-of select="$DecPl2"/></xsl:when>
      <xsl:when test="$gonsDecPlaces = 3"><xsl:value-of select="$DecPl3"/></xsl:when>
      <xsl:when test="$gonsDecPlaces = 4"><xsl:value-of select="$DecPl4"/></xsl:when>
      <xsl:when test="$gonsDecPlaces = 5"><xsl:value-of select="$DecPl5"/></xsl:when>
      <xsl:when test="$gonsDecPlaces = 6"><xsl:value-of select="$DecPl6"/></xsl:when>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="decDegDecPl">
    <xsl:choose>
      <xsl:when test="$decDegDecPlaces = 1"><xsl:value-of select="$DecPl1"/></xsl:when>
      <xsl:when test="$decDegDecPlaces = 2"><xsl:value-of select="$DecPl2"/></xsl:when>
      <xsl:when test="$decDegDecPlaces = 3"><xsl:value-of select="$DecPl3"/></xsl:when>
      <xsl:when test="$decDegDecPlaces = 4"><xsl:value-of select="$DecPl4"/></xsl:when>
      <xsl:when test="$decDegDecPlaces = 5"><xsl:value-of select="$DecPl5"/></xsl:when>
      <xsl:when test="$decDegDecPlaces = 6"><xsl:value-of select="$DecPl6"/></xsl:when>
    </xsl:choose>
  </xsl:variable>

  <xsl:choose>
    <!-- Null angle value -->
    <xsl:when test="string(number($theAngle))='NaN'">
      <xsl:value-of select="format-number($theAngle, $DecPl3, 'Standard')"/> <!-- Use the defined null format output -->
    </xsl:when>
    <!-- There is an angle value -->
    <xsl:otherwise>
      <xsl:choose>
        <xsl:when test="($AngleUnit = 'DMSDegrees') or not($DMSOutput = 'false')">
          <xsl:choose>
            <xsl:when test="$outputAsMilligonsOrSecs != 'false'">
              <xsl:variable name="decPlFmt">
                <xsl:choose>
                  <xsl:when test="$secDecPlaces = 1"><xsl:value-of select="'00.0'"/></xsl:when>
                  <xsl:when test="$secDecPlaces = 2"><xsl:value-of select="'00.00'"/></xsl:when>
                  <xsl:when test="$secDecPlaces = 3"><xsl:value-of select="'00.000'"/></xsl:when>
                  <xsl:when test="$secDecPlaces = 4"><xsl:value-of select="'00.0000'"/></xsl:when>
                  <xsl:when test="$secDecPlaces = 5"><xsl:value-of select="'00.00000'"/></xsl:when>
                  <xsl:when test="$secDecPlaces = 6"><xsl:value-of select="'00.000000'"/></xsl:when>
                  <xsl:otherwise><xsl:value-of select="'00.0'"/></xsl:otherwise>
                </xsl:choose>
              </xsl:variable>
              <xsl:value-of select="format-number($theAngle * $AngleConvFactor * 3600.0, $decPlFmt, 'Standard')"/>
            </xsl:when>            
            <xsl:when test="$outputAsMilligonsOrSecsSqrd != 'false'">
              <xsl:value-of select="format-number($theAngle * $AngleConvFactor * 3600.0 * 3600.0, '00.000', 'Standard')"/>
            </xsl:when>            
            <xsl:otherwise>
              <xsl:call-template name="FormatDMSAngle">
                <xsl:with-param name="decimalAngle" select="$theAngle"/>
                <xsl:with-param name="secDecPlaces" select="$secDecPlaces"/>
                <xsl:with-param name="useSymbols" select="$useSymbols"/>
                <xsl:with-param name="impliedDecimalPt" select="$impliedDecimalPt"/>
                <xsl:with-param name="dmsSymbols" select="$dmsSymbols"/>
              </xsl:call-template>
            </xsl:otherwise>
          </xsl:choose>  
        </xsl:when>

        <xsl:otherwise>
          <xsl:variable name="fmtAngle">
            <xsl:choose>
              <xsl:when test="($AngleUnit = 'Gons') and ($DMSOutput = 'false')">
                <xsl:choose>
                  <xsl:when test="$outputAsMilligonsOrSecs != 'false'">
                    <xsl:value-of select="format-number($theAngle * $AngleConvFactor * 1000.0, $DecPl2, 'Standard')"/>
                  </xsl:when>
                  <xsl:when test="$outputAsMilligonsOrSecsSqrd != 'false'">
                    <xsl:value-of select="format-number($theAngle * $AngleConvFactor * 1000.0 * 1000.0, $DecPl4, 'Standard')"/>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:choose>
                      <xsl:when test="$secDecPlaces &gt; 0">  <!-- More accurate angle output required -->
                        <xsl:value-of select="format-number($theAngle * $AngleConvFactor, $DecPl8, 'Standard')"/>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:value-of select="format-number($theAngle * $AngleConvFactor, $gonsDecPl, 'Standard')"/>
                      </xsl:otherwise>
                    </xsl:choose>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:when>

              <xsl:when test="($AngleUnit = 'Mils') and ($DMSOutput = 'false')">
                <xsl:choose>
                  <xsl:when test="$secDecPlaces &gt; 0">  <!-- More accurate angle output required -->
                    <xsl:value-of select="format-number($theAngle * $AngleConvFactor, $DecPl6, 'Standard')"/>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:value-of select="format-number($theAngle * $AngleConvFactor, $DecPl4, 'Standard')"/>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:when>

              <xsl:when test="($AngleUnit = 'DecimalDegrees') and ($DMSOutput = 'false')">
                <xsl:choose>
                  <xsl:when test="$secDecPlaces &gt; 0">  <!-- More accurate angle output required -->
                    <xsl:value-of select="format-number($theAngle * $AngleConvFactor, $DecPl8, 'Standard')"/>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:value-of select="format-number($theAngle * $AngleConvFactor, $decDegDecPl, 'Standard')"/>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:when>
            </xsl:choose>
          </xsl:variable>
          
          <xsl:choose>
            <xsl:when test="$impliedDecimalPt != 'true'">
              <xsl:value-of select="$fmtAngle"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="translate($fmtAngle, '.', '')"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************ Output Azimuth in Appropriate Format ************** -->
<!-- **************************************************************** -->
<xsl:template name="FormatAzimuth">
  <xsl:param name="theAzimuth"/>
  <xsl:param name="secDecPlaces" select="0"/>
  <xsl:param name="DMSOutput" select="'false'"/>  <!-- Can be used to force DMS output -->
  <xsl:param name="useSymbols" select="'true'"/>
  <xsl:param name="quadrantBearings" select="'false'"/>  <!-- Can be used to force quadrant bearing output -->
  <xsl:param name="impliedDecimalPt" select="'false'"/>
  <xsl:param name="northLbl" select="'N'"/>
  <xsl:param name="eastLbl" select="'E'"/>
  <xsl:param name="southLbl" select="'S'"/>
  <xsl:param name="westLbl" select="'W'"/>
  <xsl:param name="dmsSymbols">&#0176;'"</xsl:param>

  <xsl:choose>
    <xsl:when test="(/JOBFile/Environment/DisplaySettings/AzimuthFormat = 'QuadrantBearings') or ($quadrantBearings != 'false')">
      <xsl:call-template name="FormatQuadrantBearing">
        <xsl:with-param name="decimalAngle" select="$theAzimuth"/>
        <xsl:with-param name="secDecPlaces" select="$secDecPlaces"/>
        <xsl:with-param name="impliedDecimalPt" select="$impliedDecimalPt"/>
        <xsl:with-param name="northLbl" select="$northLbl"/>
        <xsl:with-param name="eastLbl" select="$eastLbl"/>
        <xsl:with-param name="southLbl" select="$southLbl"/>
        <xsl:with-param name="westLbl" select="$westLbl"/>
        <xsl:with-param name="dmsSymbols" select="$dmsSymbols"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="FormatAngle">
        <xsl:with-param name="theAngle" select="$theAzimuth"/>
        <xsl:with-param name="secDecPlaces" select="$secDecPlaces"/>
        <xsl:with-param name="DMSOutput" select="$DMSOutput"/>
        <xsl:with-param name="useSymbols" select="$useSymbols"/>
        <xsl:with-param name="impliedDecimalPt" select="$impliedDecimalPt"/>
        <xsl:with-param name="dmsSymbols" select="$dmsSymbols"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ********* Format latitude or Longitude in DMS Format *********** -->
<!-- **************************************************************** -->
<xsl:template name="FormatLatLong">
  <xsl:param name="theAngle"/>
  <xsl:param name="isLat" select="'true'"/>
  <xsl:param name="secDecPlaces" select="5"/>
  <xsl:param name="impliedDecimalPt" select="'false'"/>
  <xsl:param name="northLbl" select="'N'"/>
  <xsl:param name="eastLbl" select="'E'"/>
  <xsl:param name="southLbl" select="'S'"/>
  <xsl:param name="westLbl" select="'W'"/>
  <xsl:param name="leadingLabel" select="'false'"/>
  <xsl:param name="dmsSymbols">&#0176;'"</xsl:param>
  <xsl:param name="decimalDegrees" select="'false'"/>

  <xsl:choose>
    <!-- Null angle value -->
    <xsl:when test="string(number($theAngle)) = 'NaN'">
      <xsl:value-of select="format-number($theAngle, $DecPl3, 'Standard')"/> <!-- Use the defined null format output -->
    </xsl:when>
    <!-- There is a lat or long value -->
    <xsl:otherwise>
      <xsl:variable name="sign">
        <xsl:choose>
          <xsl:when test="$theAngle &lt; '0.0'">
            <xsl:choose>  <!-- Negative value -->
              <xsl:when test="$isLat = 'true'"><xsl:value-of select="$southLbl"/></xsl:when>
              <xsl:otherwise><xsl:value-of select="$westLbl"/></xsl:otherwise>
            </xsl:choose>
          </xsl:when>
          <xsl:otherwise> <!-- Positive value -->
            <xsl:choose>
              <xsl:when test="$isLat = 'true'"><xsl:value-of select="$northLbl"/></xsl:when>
              <xsl:otherwise><xsl:value-of select="$eastLbl"/></xsl:otherwise>
            </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <!-- Convert to a positive angle before changing to DMS format -->
      <xsl:variable name="posAngle" select="concat(substring('-',2 - ($theAngle &lt; 0)), '1') * $theAngle"/>

      <xsl:variable name="latLongAngle">
        <xsl:choose>
          <xsl:when test="$decimalDegrees = 'false'">
            <xsl:call-template name="FormatDMSAngle">
              <xsl:with-param name="decimalAngle" select="$posAngle"/>
              <xsl:with-param name="secDecPlaces" select="$secDecPlaces"/>
              <xsl:with-param name="impliedDecimalPt" select="$impliedDecimalPt"/>
              <xsl:with-param name="dmsSymbols" select="$dmsSymbols"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>  <!-- Output as decimal degrees to 8 decimal places -->
            <xsl:value-of select="format-number($posAngle, $DecPl8, 'Standard')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <xsl:choose>
        <xsl:when test="$leadingLabel = 'false'">
          <xsl:value-of select="concat($latLongAngle, $sign)"/>
        </xsl:when>
        <xsl:otherwise>  <!-- Trailing label -->
          <xsl:value-of select="concat($sign, $latLongAngle)"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ********************** New Line Output ************************* -->
<!-- **************************************************************** -->
<xsl:template name="NewLine">
  <xsl:text>&#10;</xsl:text>
</xsl:template>


<!-- **************************************************************** -->
<!-- *** An intermediate variable of X,Y,Z to lat,long,height conv ** -->
<!-- **************************************************************** -->
<xsl:template name="pVar">
  <xsl:param name="X"/>
  <xsl:param name="Y"/>

  <xsl:call-template name="Sqrt">
    <xsl:with-param name="num" select="$X * $X + $Y * $Y"/>
    <xsl:with-param name="maxiter" select="30"/>
  </xsl:call-template>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******* Return the Radius of Curvature in Prime Vertical ******* -->
<!-- **************************************************************** -->
<xsl:template name="RadiusOfCurvature">
  <xsl:param name="latitude"/>   <!-- Already converted to radians -->

  <xsl:variable name="sinLat">
    <xsl:call-template name="Sine">
      <xsl:with-param name="theAngle" select="$latitude"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="sinLatSquared">
    <xsl:value-of select="$sinLat * $sinLat"/>
  </xsl:variable>

  <xsl:variable name="tempVal">
    <xsl:call-template name="Sqrt">
      <xsl:with-param name="num" select="1.0 - $WGS84EccentricitySquared * $sinLatSquared"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:value-of select="$WGS84SemiMajorAxis div $tempVal"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ********************** Angle in Radians ************************ -->
<!-- **************************************************************** -->
<xsl:template name="AngleInRadians">
  <xsl:param name="theAngle"/>
  <xsl:param name="normalise" select="'false'"/>
  <xsl:choose>
    <!-- Null angle value -->
    <xsl:when test="string(number($theAngle)) = 'NaN'">
      <xsl:value-of select="''"/>
    </xsl:when>
    <!-- There is an angle value -->
    <xsl:otherwise>
      <xsl:variable name="radiansAngle">
        <xsl:value-of select="$theAngle * $Pi div 180.0"/>
      </xsl:variable>

      <xsl:variable name="outAngle">
        <xsl:choose>
          <xsl:when test="$normalise = 'false'">
            <xsl:value-of select="$radiansAngle"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="RadianAngleBetweenLimits">
              <xsl:with-param name="anAngle" select="$radiansAngle"/>
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:value-of select="$outAngle"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ********* Return radians angle between Specified Limits ******** -->
<!-- **************************************************************** -->
<xsl:template name="RadianAngleBetweenLimits">
  <xsl:param name="anAngle"/>
  <xsl:param name="minVal" select="0.0"/>
  <xsl:param name="maxVal" select="$Pi * 2.0"/>
  <xsl:param name="incVal" select="$Pi * 2.0"/>

  <xsl:variable name="angle1">
    <xsl:call-template name="AngleValueLessThanMax">
      <xsl:with-param name="inAngle" select="$anAngle"/>
      <xsl:with-param name="maxVal" select="$maxVal"/>
      <xsl:with-param name="incVal" select="$incVal"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="angle2">
    <xsl:call-template name="AngleValueGreaterThanMin">
      <xsl:with-param name="inAngle" select="$angle1"/>
      <xsl:with-param name="minVal" select="$minVal"/>
      <xsl:with-param name="incVal" select="$incVal"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:value-of select="$angle2"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******* Return the arcTan value using a series expansion ******* -->
<!-- **************************************************************** -->
<xsl:template name="ArcTanSeries">
  <xsl:param name="tanVal"/>

  <!-- If the absolute value of tanVal is greater than 1 the work with the -->
  <!-- reciprocal value and return the resultant angle subtracted from Pi. -->
  <xsl:variable name="absTanVal" select="concat(substring('-',2 - ($tanVal &lt; 0)), '1') * $tanVal"/>
  <xsl:variable name="tanVal2">
    <xsl:choose>
      <xsl:when test="$absTanVal &gt; 1.0">
        <xsl:value-of select="1.0 div $tanVal"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$tanVal"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="valSq" select="$tanVal2 * $tanVal2"/>

  <xsl:variable name="angVal">
    <xsl:value-of select="$tanVal2 div (1 + ($valSq
                                   div (3 + (4 * $valSq
                                   div (5 + (9 * $valSq
                                   div (7 + (16 * $valSq
                                   div (9 + (25 * $valSq
                                   div (11 + (36 * $valSq
                                   div (13 + (49 * $valSq
                                   div (15 + (64 * $valSq
                                   div (17 + (81 * $valSq
                                   div (19 + (100 * $valSq
                                   div (21 + (121 * $valSq
                                   div (23 + (144 * $valSq
                                   div (25 + (169 * $valSq
                                   div (27 + (196 * $valSq
                                   div (29 + (225 * $valSq))))))))))))))))))))))))))))))"/>
  </xsl:variable>

  <xsl:choose>
    <xsl:when test="$absTanVal &gt; 1.0">
      <xsl:choose>
        <xsl:when test="$tanVal &lt; 0">
          <xsl:value-of select="-$halfPi - $angVal"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$halfPi - $angVal"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$angVal"/>
    </xsl:otherwise>
  </xsl:choose>

</xsl:template>


<!-- **************************************************************** -->
<!-- ********************** Format a DMS Angle ********************** -->
<!-- **************************************************************** -->
<xsl:template name="FormatDMSAngle">
  <xsl:param name="decimalAngle"/>
  <xsl:param name="secDecPlaces" select="0"/>
  <xsl:param name="useSymbols" select="'true'"/>
  <xsl:param name="impliedDecimalPt" select="'false'"/>
  <xsl:param name="dmsSymbols">&#0176;'"</xsl:param>

  <xsl:variable name="degreesSymbol">
    <xsl:choose>
      <xsl:when test="$useSymbols = 'true'"><xsl:value-of select="substring($dmsSymbols, 1, 1)"/></xsl:when>  <!-- Degrees symbol ° -->
      <xsl:otherwise>
        <xsl:if test="$impliedDecimalPt != 'true'">.</xsl:if>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="minutesSymbol">
    <xsl:choose>
      <xsl:when test="$useSymbols = 'true'"><xsl:value-of select="substring($dmsSymbols, 2, 1)"/></xsl:when>
      <xsl:otherwise></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="secondsSymbol">
    <xsl:choose>
      <xsl:when test="$useSymbols = 'true'"><xsl:value-of select="substring($dmsSymbols, 3, 1)"/></xsl:when>
      <xsl:otherwise></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="sign">
    <xsl:if test="$decimalAngle &lt; '0.0'">-1</xsl:if>
    <xsl:if test="$decimalAngle &gt;= '0.0'">1</xsl:if>
  </xsl:variable>

  <xsl:variable name="posDecimalDegrees" select="number($decimalAngle * $sign)"/>

  <xsl:variable name="positiveDecimalDegrees">  <!-- Ensure an angle very close to 360° is treated as 0° -->
    <xsl:choose>
      <xsl:when test="(360.0 - $posDecimalDegrees) &lt; 0.00001">
        <xsl:value-of select="0"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$posDecimalDegrees"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="decPlFmt">
    <xsl:choose>
      <xsl:when test="$secDecPlaces = 0"><xsl:value-of select="''"/></xsl:when>
      <xsl:when test="$secDecPlaces = 1"><xsl:value-of select="'.0'"/></xsl:when>
      <xsl:when test="$secDecPlaces = 2"><xsl:value-of select="'.00'"/></xsl:when>
      <xsl:when test="$secDecPlaces = 3"><xsl:value-of select="'.000'"/></xsl:when>
      <xsl:when test="$secDecPlaces = 4"><xsl:value-of select="'.0000'"/></xsl:when>
      <xsl:when test="$secDecPlaces = 5"><xsl:value-of select="'.00000'"/></xsl:when>
      <xsl:when test="$secDecPlaces = 6"><xsl:value-of select="'.000000'"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="''"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="degrees" select="floor($positiveDecimalDegrees)"/>
  <xsl:variable name="decimalMinutes" select="number(number($positiveDecimalDegrees - $degrees) * 60 )"/>
  <xsl:variable name="minutes" select="floor($decimalMinutes)"/>
  <xsl:variable name="seconds" select="number(number($decimalMinutes - $minutes)*60)"/>

  <xsl:variable name="partiallyNormalisedMinutes">
    <xsl:if test="number(format-number($seconds, concat('00', $decPlFmt))) = 60"><xsl:value-of select="number($minutes + 1)"/></xsl:if>
    <xsl:if test="not(number(format-number($seconds, concat('00', $decPlFmt))) = 60)"><xsl:value-of select="$minutes"/></xsl:if>
  </xsl:variable>

  <xsl:variable name="normalisedSeconds">
    <xsl:if test="number(format-number($seconds, concat('00', $decPlFmt))) = 60"><xsl:value-of select="0"/></xsl:if>
    <xsl:if test="not(number(format-number($seconds, concat('00', $decPlFmt))) = 60)"><xsl:value-of select="$seconds"/></xsl:if>
  </xsl:variable>

  <xsl:variable name="partiallyNormalisedDegrees">
    <xsl:if test="format-number($partiallyNormalisedMinutes, '0') = '60'"><xsl:value-of select="number($degrees + 1)"/></xsl:if>
    <xsl:if test="not(format-number($partiallyNormalisedMinutes, '0') = '60')"><xsl:value-of select="$degrees"/></xsl:if>
  </xsl:variable>

  <xsl:variable name="normalisedDegrees">
    <xsl:if test="format-number($partiallyNormalisedDegrees, '0') = '360'"><xsl:value-of select="0"/></xsl:if>
    <xsl:if test="not(format-number($partiallyNormalisedDegrees, '0') = '360')"><xsl:value-of select="$partiallyNormalisedDegrees"/></xsl:if>
  </xsl:variable>

  <xsl:variable name="normalisedMinutes">
    <xsl:if test="format-number($partiallyNormalisedMinutes, '00') = '60'"><xsl:value-of select="0"/></xsl:if>
    <xsl:if test="not(format-number($partiallyNormalisedMinutes, '00') = '60')"><xsl:value-of select="$partiallyNormalisedMinutes"/></xsl:if>
  </xsl:variable>

  <xsl:if test="$sign = -1">-</xsl:if>
  <xsl:value-of select="format-number($normalisedDegrees, '0')"/>
  <xsl:value-of select="$degreesSymbol"/>
  <xsl:value-of select="format-number($normalisedMinutes, '00')"/>
  <xsl:value-of select="$minutesSymbol"/>
  <xsl:choose>
    <xsl:when test="$useSymbols = 'true'">
      <xsl:value-of select="format-number($normalisedSeconds, concat('00', $decPlFmt))"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="translate(format-number($normalisedSeconds, concat('00', $decPlFmt)), '.', '')"/>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:value-of select="$secondsSymbol"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******************* Format a Quadrant Bearing ****************** -->
<!-- **************************************************************** -->
<xsl:template name="FormatQuadrantBearing">
  <xsl:param name="decimalAngle"/>
  <xsl:param name="secDecPlaces" select="0"/>
  <xsl:param name="impliedDecimalPt" select="'false'"/>
  <xsl:param name="northLbl" select="'N'"/>
  <xsl:param name="eastLbl" select="'E'"/>
  <xsl:param name="southLbl" select="'S'"/>
  <xsl:param name="westLbl" select="'W'"/>
  <xsl:param name="dmsSymbols">&#0176;'"</xsl:param>

  <xsl:choose>
    <!-- Null azimuth value -->
    <xsl:when test="string(number($decimalAngle)) = 'NaN'">
      <xsl:value-of select="format-number($decimalAngle, $DecPl3, 'Standard')"/>  <!-- Use the defined null format output -->
    </xsl:when>
    <!-- There is an azimuth value -->
    <xsl:otherwise>
      <xsl:variable name="quadrantAngle">
        <xsl:if test="($decimalAngle &lt;= 90.0)">
          <xsl:value-of select="number ( $decimalAngle )"/>
        </xsl:if>
        <xsl:if test="($decimalAngle &gt; 90.0) and ($decimalAngle &lt;= 180.0)">
          <xsl:value-of select="number( 180.0 - $decimalAngle )"/>
        </xsl:if>
        <xsl:if test="($decimalAngle &gt; 180.0) and ($decimalAngle &lt; 270.0)">
          <xsl:value-of select="number( $decimalAngle - 180.0 )"/>
        </xsl:if>
        <xsl:if test="($decimalAngle &gt;= 270.0) and ($decimalAngle &lt;= 360.0)">
          <xsl:value-of select="number( 360.0 - $decimalAngle )"/>
        </xsl:if>
      </xsl:variable>

      <xsl:variable name="quadrantPrefix">
        <xsl:if test="($decimalAngle &lt;= 90.0) or ($decimalAngle &gt;= 270.0)"><xsl:value-of select="$northLbl"/></xsl:if>
        <xsl:if test="($decimalAngle &gt; 90.0) and ($decimalAngle &lt; 270.0)"><xsl:value-of select="$southLbl"/></xsl:if>
      </xsl:variable>

      <xsl:variable name="quadrantSuffix">
        <xsl:if test="($decimalAngle &lt;= 180.0)"><xsl:value-of select="$eastLbl"/></xsl:if>
        <xsl:if test="($decimalAngle &gt; 180.0)"><xsl:value-of select="$westLbl"/></xsl:if>
      </xsl:variable>

      <xsl:value-of select="$quadrantPrefix"/>
      <xsl:choose>
        <xsl:when test="$AngleUnit = 'DMSDegrees'">
          <xsl:call-template name="FormatDMSAngle">
            <xsl:with-param name="decimalAngle" select="$quadrantAngle"/>
            <xsl:with-param name="secDecPlaces" select="$secDecPlaces"/>
            <xsl:with-param name="impliedDecimalPt" select="$impliedDecimalPt"/>
            <xsl:with-param name="dmsSymbols" select="$dmsSymbols"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="FormatAngle">
            <xsl:with-param name="theAngle" select="$quadrantAngle"/>
            <xsl:with-param name="secDecPlaces" select="$secDecPlaces"/>
            <xsl:with-param name="impliedDecimalPt" select="$impliedDecimalPt"/>
            <xsl:with-param name="dmsSymbols" select="$dmsSymbols"/>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:value-of select="$quadrantSuffix"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ******* Return radians angle less than Specificed Maximum ****** -->
<!-- **************************************************************** -->
<xsl:template name="AngleValueLessThanMax">
  <xsl:param name="inAngle"/>
  <xsl:param name="maxVal"/>
  <xsl:param name="incVal"/>

  <xsl:choose>
    <xsl:when test="$inAngle &gt; $maxVal">
      <xsl:variable name="newAngle">
        <xsl:value-of select="$inAngle - $incVal"/>
      </xsl:variable>
      <xsl:call-template name="AngleValueLessThanMax">
        <xsl:with-param name="inAngle" select="$newAngle"/>
      </xsl:call-template>
    </xsl:when>

    <xsl:otherwise>
      <xsl:value-of select="$inAngle"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************* Return radians angle greater than Zero *********** -->
<!-- **************************************************************** -->
<xsl:template name="AngleValueGreaterThanMin">
  <xsl:param name="inAngle"/>
  <xsl:param name="minVal"/>
  <xsl:param name="incVal"/>

  <xsl:choose>
    <xsl:when test="$inAngle &lt; $minVal">
      <xsl:variable name="newAngle">
        <xsl:value-of select="$inAngle + $incVal"/>
      </xsl:variable>
      <xsl:call-template name="AngleValueGreaterThanMin">
        <xsl:with-param name="inAngle" select="$newAngle"/>
      </xsl:call-template>
    </xsl:when>

    <xsl:otherwise>
      <xsl:value-of select="$inAngle"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


</xsl:stylesheet>
