<?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">
<xsl:output method="text"/>

<!-- (c) 2016, 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:decimal-format name="station" decimal-separator='.' grouping-separator='+' NaN='?'/>
<xsl:decimal-format name="null" NaN='?'/>

<xsl:variable name="Null" select="'?'"/>
<xsl:variable name="Eol" select="'&#13;&#10;'"/>
<xsl:variable name="Space" select="' '"/>
<xsl:variable name="Percent" select="'%'"/>
<xsl:variable name="FieldSeparator" select="'&#9;'"/>
<xsl:variable name="DegreesSymbol" select="'&#xb0;'"/>
<xsl:variable name="MinutesSymbol"><xsl:text>'</xsl:text></xsl:variable>
<xsl:variable name="SecondsSymbol"><xsl:text>"</xsl:text></xsl:variable>

<xsl:variable name="DMSAngleDPs">
  <xsl:choose>
    <xsl:when test="/Stakeout/DisplaySettings/DMSAngleDisplayDPs = 0">00</xsl:when>
    <xsl:when test="/Stakeout/DisplaySettings/DMSAngleDisplayDPs = 1">00.0</xsl:when>
    <xsl:otherwise>00</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="DecAngleDPs">
  <xsl:choose>
    <xsl:when test="/Stakeout/DisplaySettings/DecimalAngleDisplayDPs = 3">0.000</xsl:when>
    <xsl:when test="/Stakeout/DisplaySettings/DecimalAngleDisplayDPs = 4">0.0000</xsl:when>
    <xsl:when test="/Stakeout/DisplaySettings/DecimalAngleDisplayDPs = 5">0.00000</xsl:when>
    <xsl:otherwise>0.0000</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="DistDPs">
  <xsl:choose>
    <xsl:when test="/Stakeout/DisplaySettings/DistanceDisplayDPs = 1">0.0</xsl:when>
    <xsl:when test="/Stakeout/DisplaySettings/DistanceDisplayDPs = 2">0.00</xsl:when>
    <xsl:when test="/Stakeout/DisplaySettings/DistanceDisplayDPs = 3">0.000</xsl:when>
    <xsl:when test="/Stakeout/DisplaySettings/DistanceDisplayDPs = 4">0.0000</xsl:when>
    <xsl:otherwise>0.000</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="GradeUnit" select="/Stakeout/DisplaySettings/GradeUnits"/>
<xsl:variable name="AngleUnit" select="/Stakeout/DisplaySettings/AngleUnits"/>
<xsl:variable name="HeightUnit" select="/Stakeout/DisplaySettings/HeightUnits"/>
<xsl:variable name="DistanceUnit" select="/Stakeout/DisplaySettings/DistanceUnits"/>
<xsl:variable name="CoordinateOrder" select="/Stakeout/DisplaySettings/CoordinateOrder"/>

<xsl:variable name="StationingFormat">
  <xsl:choose>
    <xsl:when test="/Stakeout/DisplaySettings/StationingFormat = '1000.0'"><xsl:value-of select="$DistDPs"/></xsl:when>
    <xsl:when test="/Stakeout/DisplaySettings/StationingFormat = '10+00.0'"><xsl:value-of select="concat('0+0', $DistDPs)"/></xsl:when>
    <xsl:when test="/Stakeout/DisplaySettings/StationingFormat = '1+000.0'"><xsl:value-of select="concat('0+00', $DistDPs)"/></xsl:when>
    <xsl:when test="/Stakeout/DisplaySettings/StationingFormat = 'StationIndex'"><xsl:value-of select="$DistDPs"/></xsl:when>
    <xsl:otherwise><xsl:value-of select="concat('0+00', $DistDPs)"/></xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="StationFormatNumber">
  <xsl:choose>
    <xsl:when test="/Stakeout/DisplaySettings/StationingFormat = '1000.0'">0</xsl:when>
    <xsl:when test="/Stakeout/DisplaySettings/StationingFormat = '10+00.0'"><xsl:value-of select="4 + /Stakeout/DisplaySettings/DistanceDisplayDPs"/></xsl:when>
    <xsl:when test="/Stakeout/DisplaySettings/StationingFormat = '1+000.0'"><xsl:value-of select="5 + /Stakeout/DisplaySettings/DistanceDisplayDPs"/></xsl:when>
    <xsl:otherwise><xsl:value-of select="5 + /Stakeout/DisplaySettings/DistanceDisplayDPs"/></xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="NorthFirst">
  <xsl:choose>
    <xsl:when test="contains ( $CoordinateOrder, 'CAD' )">false</xsl:when>
    <xsl:when test="contains ( $CoordinateOrder, 'North-East' )">true</xsl:when>
    <xsl:when test="contains ( $CoordinateOrder, 'X-Y' )">true</xsl:when>
    <xsl:otherwise>false</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="EastFirst">
  <xsl:choose>
    <xsl:when test="contains ( $CoordinateOrder, 'CAD' )">true</xsl:when>
    <xsl:when test="contains ( $CoordinateOrder, 'East-North' )">true</xsl:when>
    <xsl:when test="contains ( $CoordinateOrder, 'Y-X' )">true</xsl:when>
    <xsl:otherwise>false</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="AngleFactor">
  <xsl:choose>
    <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>

<xsl:variable name="AngleDisplayType">
  <xsl:choose>
    <xsl:when test="$AngleUnit='Mils'">DecimalAngle</xsl:when>
    <xsl:when test="$AngleUnit='Gons'">DecimalAngle</xsl:when>
    <xsl:when test="$AngleUnit='DecimalDegrees'">DecimalAngle</xsl:when>
    <xsl:otherwise>DMS</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="GradeDisplayType">
  <xsl:choose>
    <xsl:when test="$GradeUnit='Percentage'">Percentage</xsl:when>
    <xsl:when test="$GradeUnit='RatioRiseRun'">Ratio</xsl:when>
    <xsl:when test="$GradeUnit='RatioRunRise'">Ratio</xsl:when>
    <xsl:when test="$GradeUnit='Angle'">
      <xsl:choose>
        <xsl:when test="$AngleUnit='Mils'">DecimalAngle</xsl:when>
        <xsl:when test="$AngleUnit='Gons'">DecimalAngle</xsl:when>
        <xsl:when test="$AngleUnit='DecimalDegrees'">DecimalAngle</xsl:when>
        <xsl:otherwise>DMS</xsl:otherwise>
      </xsl:choose>
    </xsl:when>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="HorzDistAbbrev">
  <xsl:choose>
    <xsl:when test="$DistanceUnit ='Metres'"><xsl:text>m</xsl:text></xsl:when>
    <xsl:when test="$DistanceUnit ='InternationalFeet'"><xsl:text>ift</xsl:text></xsl:when>
    <xsl:when test="$DistanceUnit ='USSurveyFeet'"><xsl:text>sft</xsl:text></xsl:when>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="HorzDistFactor">
  <xsl:choose>
    <xsl:when test="$DistanceUnit = 'Metres'">1.0</xsl:when>
    <xsl:when test="$DistanceUnit = 'InternationalFeet'">3.280839895</xsl:when>
    <xsl:when test="$DistanceUnit = 'USSurveyFeet'">3.2808333333357</xsl:when>
    <xsl:otherwise>1.0</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="VertDistAbbrev">
  <xsl:choose>
    <xsl:when test="$HeightUnit ='Metres'"><xsl:text>m</xsl:text></xsl:when>
    <xsl:when test="$HeightUnit ='InternationalFeet'"><xsl:text>ift</xsl:text></xsl:when>
    <xsl:when test="$HeightUnit ='USSurveyFeet'"><xsl:text>sft</xsl:text></xsl:when>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="VertDistFactor">
  <xsl:choose>
    <xsl:when test="$HeightUnit = 'Metres'">1.0</xsl:when>
    <xsl:when test="$HeightUnit = 'InternationalFeet'">3.280839895</xsl:when>
    <xsl:when test="$HeightUnit = 'USSurveyFeet'">3.2808333333357</xsl:when>
    <xsl:otherwise>1.0</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="DisplayFtAndInches">
  <xsl:choose>
    <xsl:when test="(/Stakeout/DisplaySettings/FeetAndInchesDisplayPrecision = 0) or
                    ((/Stakeout/DisplaySettings/FeetAndInchesDisplayPrecision != 0) and ($DistanceUnit ='Metres'))">false</xsl:when>
    <xsl:otherwise>true</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:template name="SetZeroValuesAlwaysPositive">
  <xsl:param name="inVal"/>
  
  <xsl:choose>
    <xsl:when test="number($inVal) = 0.0">
      <xsl:value-of select="format-number(0.0, $DistDPs, 'null')"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$inVal"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="FormatHorizontalDistance">
  <xsl:param name="Distance"/>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:choose>
    <xsl:when test="$DisplayFtAndInches = 'false'">
      <xsl:variable name="fmtVal">
        <xsl:value-of select="format-number(number($Distance * $HorzDistFactor),$DistDPs,'null')"/>
      </xsl:variable>
      <xsl:call-template name="SetZeroValuesAlwaysPositive">
        <xsl:with-param name="inVal" select="$fmtVal"/>
      </xsl:call-template>
      <xsl:if test="not(format-number($Distance,'0.000')='NaN')">
        <xsl:value-of select="$HorzDistAbbrev"/>
      </xsl:if>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="FormattedFeetAndInchesDistance">
        <xsl:with-param name="distVal" select="number($Distance * $HorzDistFactor)"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template name="FormatPrefixedVerticalDistance">
  <xsl:param name="Distance"/>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:if test="not(format-number($Distance,'0.000')='NaN')">
    <xsl:variable name="Prefix">
      <xsl:if test="$Distance &lt;= '0.0'"><xsl:text>Cut</xsl:text></xsl:if>
      <xsl:if test="$Distance &gt; '0.0'"><xsl:text>Fill</xsl:text></xsl:if>
    </xsl:variable>
    <xsl:value-of select="$Prefix"/>
    <xsl:value-of select="$Space"/>
  </xsl:if>
  <xsl:variable name="Factor">
    <xsl:if test="$Distance &lt; '0.0'"><xsl:value-of select="number(-1 * $VertDistFactor)"/></xsl:if>
    <xsl:if test="$Distance &gt;= '0.0'"><xsl:value-of select="$VertDistFactor"/></xsl:if>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="$DisplayFtAndInches = 'false'">
      <xsl:value-of select="format-number(number($Distance * $Factor),$DistDPs,'null')"/>
      <xsl:if test="not(format-number($Distance,'0.000')='NaN')">
        <xsl:value-of select="$VertDistAbbrev"/>
      </xsl:if>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="FormattedFeetAndInchesDistance">
        <xsl:with-param name="distVal" select="number($Distance * $Factor)"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>

  <!-- Set up the value color based on the sign of the vertical distance -->
  <xsl:variable name="absVertDist" select="concat(substring('-',2 - ($Distance &lt; 0)), '1') * $Distance"/>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:text>ValueColor=</xsl:text>
  <xsl:choose>
    <xsl:when test="$absVertDist &lt; 0.001">Green</xsl:when>
    <xsl:when test="$Distance &lt; 0.0">Red</xsl:when>
    <xsl:when test="$Distance &gt; 0.0">Blue</xsl:when>
    <xsl:otherwise>Black</xsl:otherwise>
  </xsl:choose>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template name="FormatVerticalDistance">
  <xsl:param name="Distance"/>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:variable name="Factor">
    <xsl:if test="$Distance &lt; '0.0'"><xsl:value-of select="number(-1 * $VertDistFactor)"/></xsl:if>
    <xsl:if test="$Distance &gt;= '0.0'"><xsl:value-of select="$VertDistFactor"/></xsl:if>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="$DisplayFtAndInches = 'false'">
      <xsl:value-of select="format-number(number($Distance * $Factor),$DistDPs,'null')"/>
      <xsl:if test="not(format-number($Distance,'0.000')='NaN')">
        <xsl:value-of select="$VertDistAbbrev"/>
      </xsl:if>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="FormattedFeetAndInchesDistance">
        <xsl:with-param name="distVal" select="number($Distance * $Factor)"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:if test="not(format-number($Distance,'0.000')='NaN')">
    <xsl:variable name="DirectionSuffix">
      <xsl:if test="$Distance &lt; '0.0'"><xsl:text>Down</xsl:text></xsl:if>
      <xsl:if test="$Distance &gt; '0.0'"><xsl:text>Up</xsl:text></xsl:if>
    </xsl:variable>
    <xsl:value-of select="$Space"/>
    <xsl:value-of select="$DirectionSuffix"/>
  </xsl:if>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:text>ValueColor=Purple</xsl:text>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<!-- **************************************************************** -->
<!-- ********** Formatted feet and inches distance value ************ -->
<!-- **************************************************************** -->
<xsl:template name="FormattedFeetAndInchesDistance">
  <xsl:param name="distVal"/>

  <xsl:variable name="sign">
    <xsl:choose>
      <xsl:when test="number($distVal) &lt; 0.0">-1</xsl:when>
      <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="feetVal" select="$distVal * $sign"/>
  <xsl:variable name="feet" select="floor($feetVal)"/>
  <xsl:variable name="footFrac" select="$feetVal - $feet"/>
  <xsl:variable name="inch" select="floor($footFrac * 12.0)"/>
  <xsl:variable name="inchFrac" select="$footFrac * 12.0 - $inch"/>
  <xsl:variable name="numerator" select="round($inchFrac * /Stakeout/DisplaySettings/FeetAndInchesDisplayPrecision)"/>

  <xsl:variable name="simplifiedFractionVals">
    <xsl:call-template name="SimplifyFractionValues">
      <xsl:with-param name="numerator" select="$numerator"/>
      <xsl:with-param name="denominator" select="/Stakeout/DisplaySettings/FeetAndInchesDisplayPrecision"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:choose>
    <xsl:when test="string(number($distVal)) = 'NaN'">  <!-- Null value so handle via format command -->
      <xsl:value-of select="format-number($distVal,$DistDPs,'null')"/>
    </xsl:when>
    <xsl:when test="msxsl:node-set($simplifiedFractionVals)/numerator = msxsl:node-set($simplifiedFractionVals)/denominator">
      <!-- We have a whole inch in the fraction part so add 1 to the inch variable and deal with it reaching 12 inches -->
      <xsl:variable name="adjVals">
        <xsl:choose>
          <xsl:when test="$inch + 1 = 12">
            <xsl:element name="inch">0</xsl:element>
            <xsl:element name="feet">
              <xsl:value-of select="$feet + 1"/>
            </xsl:element>
          </xsl:when>
          <xsl:otherwise>
            <xsl:element name="inch">
              <xsl:value-of select="$inch + 1"/>
            </xsl:element>
            <xsl:element name="feet">
              <xsl:value-of select="$feet"/>
            </xsl:element>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:if test="$sign = -1">-</xsl:if>
      <xsl:if test="msxsl:node-set($adjVals)/feet &gt; 0">
        <xsl:value-of select="msxsl:node-set($adjVals)/feet"/>
        <xsl:text>' </xsl:text>
      </xsl:if>
      <xsl:value-of select="msxsl:node-set($adjVals)/inch"/>
      <xsl:text>"</xsl:text>
    </xsl:when>

    <xsl:otherwise>  <!-- Value with fractions of an inch -->
      <xsl:if test="$sign = -1">-</xsl:if>
      <xsl:if test="$feet &gt; 0">
        <xsl:value-of select="$feet"/>
        <xsl:text>' </xsl:text>
      </xsl:if>
      <xsl:choose>
        <xsl:when test="$inch &gt; 0">
          <xsl:value-of select="$inch"/>
          <xsl:value-of select="' '"/>
        </xsl:when>
        <xsl:otherwise>   <!-- We have 0 inches - if there are no decimals of an inch we should output 0 to give 0" -->
          <xsl:if test="msxsl:node-set($simplifiedFractionVals)/numerator = 0">0</xsl:if>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:if test="msxsl:node-set($simplifiedFractionVals)/numerator &gt; 0">
        <xsl:value-of select="msxsl:node-set($simplifiedFractionVals)/numerator"/>
        <xsl:text>/</xsl:text>
        <xsl:value-of select="msxsl:node-set($simplifiedFractionVals)/denominator"/>
      </xsl:if>
      <xsl:text>"</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- **************************************************************** -->
<!-- ********** Simplifies the values defining a fraction *********** -->
<!-- **************************************************************** -->
<xsl:template name="SimplifyFractionValues">
  <xsl:param name="numerator"/>
  <xsl:param name="denominator"/>

  <xsl:choose>
    <xsl:when test="($numerator &gt; 0) and (($numerator div 2) - floor($numerator div 2) = 0.0)">
      <xsl:call-template name="SimplifyFractionValues">
        <xsl:with-param name="numerator" select="floor($numerator div 2)"/>
        <xsl:with-param name="denominator" select="floor($denominator div 2)"/>
      </xsl:call-template>
    </xsl:when>

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

      <xsl:element name="denominator">
        <xsl:value-of select="$denominator"/>
      </xsl:element>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="FormatElevation">
  <xsl:param name="Elevation"/>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:value-of select="format-number(number($Elevation * $VertDistFactor),$DistDPs,'null')"/>
  <xsl:if test="not(format-number($Elevation,'0.000')='NaN')">
    <xsl:value-of select="$VertDistAbbrev"/>
  </xsl:if>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template name="FormatDecimalAngle">
  <xsl:param name="DecimalAngle"/>
  <xsl:variable name="AngleSuffix">
    <xsl:choose>
      <xsl:when test="$AngleUnit='Mils'"><xsl:text>Mil</xsl:text></xsl:when>
      <xsl:when test="$AngleUnit='Gons'"><xsl:text>gon</xsl:text></xsl:when>
      <xsl:when test="$AngleUnit='DecimalDegrees'"><xsl:value-of select="$DegreesSymbol"/></xsl:when>
    </xsl:choose>
  </xsl:variable>
  <xsl:value-of select="format-number(number($DecimalAngle * $AngleFactor),$DecAngleDPs,'null')"/>
  <xsl:value-of select="$AngleSuffix"/>
</xsl:template>

<xsl:template name="FormatDMSAngle">
  <xsl:param name="DecimalAngle"/>
  <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="PositiveDecimalDegrees" select="number($DecimalAngle * $Sign)"/>
  <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="PartialyNormalisedMinutes">
    <xsl:if test="format-number($Seconds, $DMSAngleDPs) = '60'"><xsl:value-of select="number($Minutes + 1)"/></xsl:if>
    <xsl:if test="not(format-number($Seconds, $DMSAngleDPs) = '60')"><xsl:value-of select="$Minutes"/></xsl:if>
  </xsl:variable>

  <xsl:variable name="NormalisedSeconds">
    <xsl:if test="format-number($Seconds, $DMSAngleDPs) = '60'"><xsl:value-of select="0"/></xsl:if>
    <xsl:if test="not(format-number($Seconds, $DMSAngleDPs) = '60')"><xsl:value-of select="$Seconds"/></xsl:if>
  </xsl:variable>

  <xsl:variable name="NormalisedDegrees">
    <xsl:if test="format-number($PartialyNormalisedMinutes, '00') = '60'"><xsl:value-of select="number($Degrees + 1)"/></xsl:if>
    <xsl:if test="not(format-number($PartialyNormalisedMinutes, '00') = '60')"><xsl:value-of select="$Degrees"/></xsl:if>
  </xsl:variable>

  <xsl:variable name="NormalisedMinutes">
    <xsl:if test="format-number($PartialyNormalisedMinutes, '00') = '60'"><xsl:value-of select="0"/></xsl:if>
    <xsl:if test="not(format-number($PartialyNormalisedMinutes, '00') = '60')"><xsl:value-of select="$PartialyNormalisedMinutes"/></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:value-of select="format-number($NormalisedSeconds, $DMSAngleDPs)"/>
  <xsl:value-of select="$SecondsSymbol"/>
</xsl:template>

<xsl:template name="FormatQuadrantBearing">
  <xsl:param name="DecimalAngle"/>
  <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:text>N</xsl:text>
    </xsl:if>
    <xsl:if test="($DecimalAngle &gt; '90.0') and ($DecimalAngle &lt; '270.0')">
      <xsl:text>S</xsl:text>
    </xsl:if>
  </xsl:variable>
  <xsl:variable name="QuadrantSuffix">
    <xsl:if test="($DecimalAngle &lt;= '180.0')">
      <xsl:text>E</xsl:text>
    </xsl:if>
    <xsl:if test="($DecimalAngle &gt; '180.0')">
      <xsl:text>W</xsl:text>
    </xsl:if>
  </xsl:variable>
  <xsl:value-of select="$QuadrantPrefix"/>
  <xsl:choose>
    <xsl:when test="$AngleDisplayType = 'DecimalAngle'">
      <xsl:call-template name="FormatDecimalAngle">
        <xsl:with-param name="DecimalAngle" select="$QuadrantAngle"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="FormatDMSAngle">
        <xsl:with-param name="DecimalAngle" select="$QuadrantAngle"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:value-of select="$QuadrantSuffix"/>
</xsl:template>

<xsl:template name="FormatAzimuth">
  <xsl:param name="Azimuth"/>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:if test="$Azimuth = ''">
    <xsl:value-of select="format-number($Azimuth,'#','null')"/>
  </xsl:if>
  <xsl:if test="not($Azimuth = '')">
    <xsl:choose>
      <xsl:when test="/Stakeout/DisplaySettings/AzimuthFormat = 'QuadrantBearings'">
        <xsl:call-template name="FormatQuadrantBearing">
          <xsl:with-param name="DecimalAngle" select="$Azimuth"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="$AngleDisplayType = 'DecimalAngle'">
            <xsl:call-template name="FormatDecimalAngle">
              <xsl:with-param name="DecimalAngle" select="$Azimuth"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="FormatDMSAngle">
              <xsl:with-param name="DecimalAngle" select="$Azimuth"/>
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:if>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template name="FormatOffset">
  <xsl:param name="Offset"/>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:variable name="Factor">
    <xsl:if test="$Offset &lt; '0.0'"><xsl:value-of select="number(-1 * $HorzDistFactor)"/></xsl:if>
    <xsl:if test="$Offset &gt;= '0.0'"><xsl:value-of select="$HorzDistFactor"/></xsl:if>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="$DisplayFtAndInches = 'false'">
      <xsl:value-of select="format-number(number($Offset * $Factor),$DistDPs,'null')"/>
      <xsl:if test="not(string(number($Offset))='NaN')">
        <xsl:value-of select="$HorzDistAbbrev"/>
      </xsl:if>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="FormattedFeetAndInchesDistance">
        <xsl:with-param name="distVal" select="number($Offset * $Factor)"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:if test="not(string(number($Offset))='NaN')">
    <xsl:variable name="OffsetSuffix">
      <xsl:if test="$Offset &lt; '0.0'"><xsl:text>Left</xsl:text></xsl:if>
      <xsl:if test="$Offset &gt; '0.0'"><xsl:text>Right</xsl:text></xsl:if>
    </xsl:variable>
      <xsl:value-of select="$Space"/>
      <xsl:value-of select="$OffsetSuffix"/>
  </xsl:if>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:text>ValueColor=</xsl:text>
    <xsl:if test="$Offset &lt; 0.0"><xsl:text>Red</xsl:text></xsl:if>
    <xsl:if test="$Offset &gt; 0.0"><xsl:text>Green</xsl:text></xsl:if>
    <xsl:if test="$Offset = 0.0">Black</xsl:if>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template name="FormatGradeAsPercentage">
  <xsl:param name="GradeValueAsPercentage"/>
  <xsl:value-of select="format-number($GradeValueAsPercentage,'0.0000','null')"/>
  <xsl:value-of select="$Percent"/>
</xsl:template>

<xsl:template name="FormatGradeAsRatio">
  <xsl:param name="GradeValueAsPercentage"/>
  <xsl:variable name="AbsValue" select="concat(substring('-',2 - ($GradeValueAsPercentage &lt; 0)), '1') * $GradeValueAsPercentage"/>
  <xsl:if test="$GradeUnit = 'RatioRiseRun'">
     <xsl:if test="$GradeValueAsPercentage &lt; '0.0'">-</xsl:if>
     <xsl:value-of select="'1:'"/>
     <xsl:value-of select="format-number(number(100 div $AbsValue),'0.000','null')"/>
  </xsl:if>
  <xsl:if test="$GradeUnit = 'RatioRunRise'">
    <xsl:value-of select="format-number(number(100 div $AbsValue),'0.000','null')"/>
    <xsl:if test="$GradeValueAsPercentage &lt; '0.0'">:-1</xsl:if>
    <xsl:if test="$GradeValueAsPercentage &gt;= '0.0'">:1</xsl:if>
  </xsl:if>
</xsl:template>

<xsl:template name="FormatGradeAsAngle">
  <xsl:param name="Grade"/>
  <xsl:param name="DisplayAbsoluteValue" select="'true'"/>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:variable name="AbsValue">
    <xsl:if test="$DisplayAbsoluteValue = 'true'">
      <xsl:value-of select="concat(substring('-',2 - ($Grade &lt; 0)), '1') * $Grade"/>
    </xsl:if>
    <xsl:if test="not($DisplayAbsoluteValue = 'true')"><xsl:value-of select="$Grade"/></xsl:if>
  </xsl:variable>
  <xsl:if test="$GradeDisplayType = 'DecimalAngle'">
    <xsl:call-template name="FormatDecimalAngle">
      <xsl:with-param name="DecimalAngle" select="$AbsValue"/>
    </xsl:call-template>  
  </xsl:if> 
  <xsl:if test="$GradeDisplayType = 'DMS'">
    <xsl:call-template name="FormatDMSAngle">
      <xsl:with-param name="DecimalAngle" select="$AbsValue"/>
    </xsl:call-template>  
  </xsl:if> 
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template name="FormatGrade">
  <xsl:param name="Grade"/>
  <xsl:param name="DisplayAbsoluteValue" select="'true'"/>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:variable name="AbsValue">
    <xsl:if test="$DisplayAbsoluteValue = 'true'">
      <xsl:value-of select="concat(substring('-',2 - ($Grade &lt; 0)), '1') * $Grade"/>
    </xsl:if>
    <xsl:if test="not($DisplayAbsoluteValue = 'true')"><xsl:value-of select="$Grade"/></xsl:if>
  </xsl:variable>
  <xsl:if test="$GradeDisplayType = 'Percentage'">
    <xsl:call-template name="FormatGradeAsPercentage">
      <xsl:with-param name="GradeValueAsPercentage" select="$AbsValue"/>
    </xsl:call-template>  
  </xsl:if> 
  <xsl:if test="$GradeDisplayType = 'Ratio'">
    <xsl:call-template name="FormatGradeAsRatio">
      <xsl:with-param name="GradeValueAsPercentage" select="$AbsValue"/>
    </xsl:call-template>  
  </xsl:if> 
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template name="FormatStation">
  <xsl:param name="Station"/>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:choose>
    <xsl:when test="contains($StationingFormat,'+') and ($Station != '')">
      <xsl:variable name="FormattedStation" select="format-number(number($Station * $HorzDistFactor ),$StationingFormat,'station')"/>
      <xsl:variable name="StartPos" select="string-length($FormattedStation) - $StationFormatNumber + 1"/>
      <xsl:variable name="CorrectlyFormattedPart" select="substring($FormattedStation, $StartPos, $StationFormatNumber )"/>
      <xsl:variable name="IncorrectlyFormattedPart" select="substring($FormattedStation, 1, $StartPos - 1 )"/>
      <xsl:variable name="ReformattedPart" select="translate($IncorrectlyFormattedPart, '+', '')"/>
      <xsl:value-of select="concat($ReformattedPart,$CorrectlyFormattedPart)"/>
    </xsl:when>

    <xsl:when test="(/Stakeout/DisplaySettings/StationingFormat = 'StationIndex') and ($Station != '')">
      <xsl:variable name="intIncrement" select="format-number(/Stakeout/DisplaySettings/StationIndexIncrement * $HorzDistFactor, '#0', 'null')"/>

      <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($StationingFormat, '.'))"/>

      <xsl:variable name="signChar">
        <xsl:if test="$Station &lt; 0.0">-</xsl:if>
      </xsl:variable>
      <xsl:variable name="absStnVal" select="concat(substring('-',2 - ($Station &lt; 0)), '1') * $Station"/>

      <xsl:variable name="beforePlus" select="floor($absStnVal div (/Stakeout/DisplaySettings/StationIndexIncrement * $HorzDistFactor))"/>
      <xsl:variable name="afterPlus" select="$absStnVal - $beforePlus * (/Stakeout/DisplaySettings/StationIndexIncrement * $HorzDistFactor)"/>
      <xsl:value-of select="concat($signChar, format-number($beforePlus, '#0'), '+', format-number($afterPlus, $afterPlusFmt, 'null'))"/>
    </xsl:when>

    <xsl:otherwise>
      <xsl:value-of select="format-number(number($Station * $HorzDistFactor ),$StationingFormat,'station')"/>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:if test="not(format-number($Station,'0.000')='NaN')">
    <xsl:value-of select="$HorzDistAbbrev"/>
  </xsl:if>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template name="FormatDeltaStation">
  <xsl:param name="Distance"/>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:variable name="Factor">
    <xsl:if test="$Distance &lt; '0.0'"><xsl:value-of select="number(-1 * $HorzDistFactor)"/></xsl:if>
    <xsl:if test="$Distance &gt;= '0.0'"><xsl:value-of select="$HorzDistFactor"/></xsl:if>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="$DisplayFtAndInches = 'false'">
      <xsl:value-of select="format-number(number($Distance * $Factor),$DistDPs,'null')"/>
      <xsl:if test="not(string(number($Distance))='NaN')">
        <xsl:value-of select="$HorzDistAbbrev"/>
      </xsl:if>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="FormattedFeetAndInchesDistance">
        <xsl:with-param name="distVal" select="number($Distance * $Factor)"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:if test="not(string(number($Distance))='NaN')">
    <xsl:variable name="DeltaStationSuffix">
      <xsl:if test="$Distance &lt; '0.0'"><xsl:text>Bkwd</xsl:text></xsl:if>
      <xsl:if test="$Distance &gt; '0.0'"><xsl:text>Fwd</xsl:text></xsl:if>
    </xsl:variable>
    <xsl:value-of select="$Space"/>
    <xsl:value-of select="$DeltaStationSuffix"/>
  </xsl:if>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template match="/Stakeout/Name">
  <xsl:text>LivePointName</xsl:text>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template match="/Stakeout/Code">
  <xsl:text>LivePointCode</xsl:text>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template match="/Stakeout/Description1">
  <xsl:text>LiveDescription1</xsl:text>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template match="/Stakeout/Description2">
  <xsl:text>LiveDescription2</xsl:text>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template match="/Stakeout/ComputedGrid">
  <xsl:text>As-staked elevation</xsl:text>
  <xsl:call-template name="FormatElevation">
    <xsl:with-param name="Elevation" select="Elevation"/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/GridDeltas/DeltaNorth">
  <xsl:if test=". != ''">  <!-- There is a DeltaNorth value -->
    <xsl:choose>
      <xsl:when test="contains($CoordinateOrder, 'CAD')"><xsl:text>ΔY</xsl:text></xsl:when>
      <xsl:when test="contains($CoordinateOrder, 'North')"><xsl:text>Δ North</xsl:text></xsl:when>
      <xsl:when test="contains($CoordinateOrder, 'X')"><xsl:text>ΔX</xsl:text></xsl:when>
      <xsl:otherwise><xsl:text>Δ North</xsl:text></xsl:otherwise>
    </xsl:choose>
    <xsl:call-template name="FormatHorizontalDistance">
      <xsl:with-param name="Distance" select="."/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

<xsl:template match="/Stakeout/GridDeltas/DeltaEast">
  <xsl:if test=". != ''">  <!-- There is a DeltaEast value -->
    <xsl:choose>
      <xsl:when test="contains($CoordinateOrder, 'CAD')"><xsl:text>ΔX</xsl:text></xsl:when>
      <xsl:when test="contains($CoordinateOrder, 'East')"><xsl:text>Δ East</xsl:text></xsl:when>
      <xsl:when test="contains($CoordinateOrder, 'Y')"><xsl:text>ΔY</xsl:text></xsl:when>
      <xsl:otherwise><xsl:text>Δ East</xsl:text></xsl:otherwise>
    </xsl:choose>
    <xsl:call-template name="FormatHorizontalDistance">
      <xsl:with-param name="Distance" select="."/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

<xsl:template match="/Stakeout/GridDeltas/DeltaElevation">
  <xsl:text>V.Dist</xsl:text>
  <xsl:call-template name="FormatPrefixedVerticalDistance">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/LinearDeltas/DeltaOffset">
  <xsl:text>Δ H.Offset</xsl:text>
  <xsl:call-template name="FormatOffset">
    <xsl:with-param name="Offset" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/LinearDeltas/DeltaStation">
  <xsl:choose>
    <xsl:when test="/Stakeout/@UseChainageTerminology = 'true'">Δ Chainage</xsl:when>
    <xsl:otherwise>Δ Station</xsl:otherwise>
  </xsl:choose>
  <xsl:call-template name="FormatDeltaStation">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/LinearDeltas/DeltaElevation">
  <xsl:text>V.Dist</xsl:text>
  <xsl:call-template name="FormatPrefixedVerticalDistance">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/LinearDeltas/VDistSlope">
  <xsl:text>V.Dist to slope</xsl:text>
  <xsl:call-template name="FormatPrefixedVerticalDistance">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/PolarDeltas/HorizontalDistance">
  <xsl:text>H.Dist to point</xsl:text>
  <xsl:call-template name="FormatHorizontalDistance">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/PolarDeltas/VerticalDistance">
  <xsl:text>V.Dist to point</xsl:text>
  <xsl:call-template name="FormatPrefixedVerticalDistance">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/PolarDeltas/Azimuth">
  <xsl:if test="/Stakeout/DisplaySettings/AzimuthFormat = 'QuadrantBearings'"><xsl:text>Bearing</xsl:text></xsl:if>
  <xsl:if test="not(/Stakeout/DisplaySettings/AzimuthFormat = 'QuadrantBearings')"><xsl:text>Azimuth</xsl:text></xsl:if>
  <xsl:call-template name="FormatAzimuth">
    <xsl:with-param name="Azimuth" select="."/>
  </xsl:call-template>
</xsl:template>

<xsl:template match="/Stakeout/ArcDesign/Name">
  <xsl:text>Arc name</xsl:text>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:copy-of select="."/>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template match="/Stakeout/ArcDesign/Code">
  <xsl:text>Code</xsl:text>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:copy-of select="."/>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template match="/Stakeout/ArcDesign/StakeoutMethod">
  <xsl:variable name="Method">
    <xsl:choose>
      <xsl:when test=". = 'ToTheArc'"><xsl:text>To the arc</xsl:text></xsl:when>
      <xsl:when test=". = 'PointOnArc'">
        <xsl:choose>
          <xsl:when test="/Stakeout/@UseChainageTerminology = 'true'">Chainage on the arc</xsl:when>
          <xsl:otherwise>Station on arc</xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:when test=". = 'SlopeFromArc'"><xsl:text>Slope from arc</xsl:text></xsl:when>
      <xsl:when test=". = 'CentrePointOfArc'"><xsl:text>Center point of arc</xsl:text></xsl:when>
      <xsl:when test=". = 'PointOffsetFromArc'">
        <xsl:choose>
          <xsl:when test="/Stakeout/@UseChainageTerminology = 'true'">Chainage/offset from arc</xsl:when>
          <xsl:otherwise>Station/offset from arc</xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:when test=". = 'IntersectionPointOfArc'"><xsl:text>Intersect point of arc</xsl:text></xsl:when>
    </xsl:choose>
  </xsl:variable>
  <xsl:text>Method</xsl:text>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:value-of select="$Method"/>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template match="/Stakeout/ArcDesign/Station">
  <xsl:choose>
    <xsl:when test="/Stakeout/@UseChainageTerminology = 'true'">Chainage</xsl:when>
    <xsl:otherwise>Station</xsl:otherwise>
  </xsl:choose>
  <xsl:call-template name="FormatStation">
    <xsl:with-param name="Station" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/ArcDesign/Offset">
  <xsl:text>H.Offset</xsl:text>
  <xsl:call-template name="FormatOffset">
    <xsl:with-param name="Offset" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/ArcDesign/Elevation">
  <xsl:text>Design elevation</xsl:text>
  <xsl:call-template name="FormatElevation">
    <xsl:with-param name="Elevation" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/DeltasRelativeToDTM/DeltaElevation">
  <xsl:text>V.Dist surface</xsl:text>
  <xsl:call-template name="FormatPrefixedVerticalDistance">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/DeltasRelativeToDTM/DTMName">
  <xsl:text>Surface</xsl:text>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:copy-of select="."/>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template match="/Stakeout/DeltasRelativeToDTM/VerticalOffset">
  <xsl:text>V.Offset</xsl:text>
  <xsl:if test="/Stakeout/DeltasRelativeToDTM/OffsetPerpendicularToDTM = 'true'">
    <xsl:text> (</xsl:text>
    <xsl:text>Perpendicular</xsl:text>
    <xsl:text>)</xsl:text>
  </xsl:if>
  <xsl:call-template name="FormatVerticalDistance">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/SlopeDesign//Type">
  <xsl:variable name="MethodPrompt">
    <xsl:choose>
      <xsl:when test=". = 'HorizontalAndVerticalDistance'"><xsl:text>Horz. and vert. dist</xsl:text></xsl:when>
      <xsl:when test=". = 'GradeAndSlopeDistance'"><xsl:text>Grade and slope dist</xsl:text></xsl:when>
      <xsl:when test=". = 'GradeAndHorizontalDistance'"><xsl:text>Grade and horz. dist</xsl:text></xsl:when>
    </xsl:choose>
  </xsl:variable>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:value-of select="$MethodPrompt"/>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template match="/Stakeout/SlopeDesign//HorizontalDistance">
  <xsl:text>H.Dist</xsl:text>
  <xsl:call-template name="FormatHorizontalDistance">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/SlopeDesign//VerticalDistance">
  <xsl:text>V.Dist</xsl:text>
  <xsl:call-template name="FormatVerticalDistance">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/SlopeDesign//SlopeDistance">
  <xsl:text>Slope dist</xsl:text>
  <xsl:call-template name="FormatHorizontalDistance">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/SlopeDesign//Grade">
  <xsl:text>Grade</xsl:text>
  <xsl:call-template name="FormatGrade">
    <xsl:with-param name="Grade" select="."/>
    <xsl:with-param name="DisplayAbsoluteValue" select="'false'"/>
  </xsl:call-template>  
</xsl:template>  

<xsl:template match="/Stakeout/SlopeDesign//GradeAsAngle">
  <xsl:text>Grade</xsl:text>
  <xsl:call-template name="FormatGradeAsAngle">
    <xsl:with-param name="Grade" select="."/>
    <xsl:with-param name="DisplayAbsoluteValue" select="'false'"/>
  </xsl:call-template>  
</xsl:template>  

<xsl:template match="/Stakeout/SlopeDesign/SlopeLeft">
   <xsl:text>Slope left</xsl:text>
   <xsl:apply-templates select="/Stakeout/SlopeDesign/SlopeLeft/Type"/>
   <xsl:if test="/Stakeout/SlopeDesign/SlopeLeft/Type = 'HorizontalAndVerticalDistance'">
     <xsl:apply-templates select="/Stakeout/SlopeDesign/SlopeLeft/HorizontalDistance"/>
     <xsl:apply-templates select="/Stakeout/SlopeDesign/SlopeLeft/VerticalDistance"/>
   </xsl:if>
   <xsl:if test="/Stakeout/SlopeDesign/SlopeLeft/Type = 'GradeAndSlopeDistance'">
     <xsl:if test="not($GradeUnit='Angle')">
       <xsl:apply-templates select="/Stakeout/SlopeDesign/SlopeLeft/Grade"/>
     </xsl:if>    
     <xsl:if test="$GradeUnit='Angle'">
       <xsl:apply-templates select="/Stakeout/SlopeDesign/SlopeLeft/GradeAsAngle"/>
     </xsl:if>    
     <xsl:apply-templates select="/Stakeout/SlopeDesign/SlopeLeft/SlopeDistance"/>
   </xsl:if>
   <xsl:if test="/Stakeout/SlopeDesign/SlopeLeft/Type = 'GradeAndHorizontalDistance'">
     <xsl:if test="not($GradeUnit='Angle')">
       <xsl:apply-templates select="/Stakeout/SlopeDesign/SlopeLeft/Grade"/>
     </xsl:if>    
     <xsl:if test="$GradeUnit='Angle'">
       <xsl:apply-templates select="/Stakeout/SlopeDesign/SlopeLeft/GradeAsAngle"/>
     </xsl:if>    
     <xsl:apply-templates select="/Stakeout/SlopeDesign/SlopeLeft/HorizontalDistance"/>
   </xsl:if>    
</xsl:template>  

<xsl:template match="/Stakeout/SlopeDesign/SlopeRight">
   <xsl:text>Slope right</xsl:text>
   <xsl:apply-templates select="/Stakeout/SlopeDesign/SlopeRight/Type"/>
   <xsl:if test="/Stakeout/SlopeDesign/SlopeRight/Type = 'HorizontalAndVerticalDistance'">
     <xsl:apply-templates select="/Stakeout/SlopeDesign/SlopeRight/HorizontalDistance"/>
     <xsl:apply-templates select="/Stakeout/SlopeDesign/SlopeRight/VerticalDistance"/>
   </xsl:if>
   <xsl:if test="/Stakeout/SlopeDesign/SlopeRight/Type = 'GradeAndSlopeDistance'">
     <xsl:if test="not($GradeUnit='Angle')">
       <xsl:apply-templates select="/Stakeout/SlopeDesign/SlopeRight/Grade"/>
     </xsl:if>    
     <xsl:if test="$GradeUnit='Angle'">
       <xsl:apply-templates select="/Stakeout/SlopeDesign/SlopeRight/GradeAsAngle"/>
     </xsl:if>    
     <xsl:apply-templates select="/Stakeout/SlopeDesign/SlopeRight/SlopeDistance"/>
   </xsl:if>
   <xsl:if test="/Stakeout/SlopeDesign/SlopeRight/Type = 'GradeAndHorizontalDistance'">
     <xsl:if test="not($GradeUnit='Angle')">
       <xsl:apply-templates select="/Stakeout/SlopeDesign/SlopeRight/Grade"/>
     </xsl:if>    
     <xsl:if test="$GradeUnit='Angle'">
       <xsl:apply-templates select="/Stakeout/SlopeDesign/SlopeRight/GradeAsAngle"/>
     </xsl:if>    
     <xsl:apply-templates select="/Stakeout/SlopeDesign/SlopeRight/HorizontalDistance"/>
   </xsl:if>    
</xsl:template>  

<xsl:template name="DesignArc">
  <xsl:text>GroupboxStart</xsl:text>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:text>Design arc</xsl:text>
  <xsl:value-of select="$Eol"/>
  <xsl:apply-templates select="/Stakeout/ArcDesign/Station"/>
  <xsl:if test="/Stakeout/ArcDesign/StakeoutMethod = 'PointOffsetFromArc'">
    <xsl:apply-templates select="/Stakeout/ArcDesign/Offset"/>
    <xsl:apply-templates select="/Stakeout/ArcDesign/Elevation"/>
  </xsl:if>  
  <xsl:text>GroupboxEnd</xsl:text>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template name="StakeDeltas">
  <xsl:text>GroupboxStart</xsl:text>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:text>Stake deltas</xsl:text>
  <xsl:value-of select="$Eol"/>
  <xsl:choose>
    <xsl:when test="/Stakeout/ArcDesign/StakeoutMethod = 'ToTheArc'">
      <xsl:apply-templates select="/Stakeout/ArcDesign/Station"/>
      <xsl:apply-templates select="/Stakeout/LinearDeltas/DeltaOffset"/>
      <xsl:apply-templates select="/Stakeout/LinearDeltas/DeltaElevation"/>
    </xsl:when>

    <xsl:when test="/Stakeout/ArcDesign/StakeoutMethod = 'PointOnArc'">
      <xsl:apply-templates select="/Stakeout/GridDeltas/DeltaElevation"/>
      <xsl:apply-templates select="/Stakeout/LinearDeltas/DeltaOffset"/>
    </xsl:when>

    <xsl:when test="/Stakeout/ArcDesign/StakeoutMethod = 'PointOffsetFromArc'">
      <xsl:apply-templates select="/Stakeout/GridDeltas/DeltaElevation"/>
    </xsl:when>

    <xsl:when test="/Stakeout/ArcDesign/StakeoutMethod = 'SlopeFromArc'">
      <xsl:apply-templates select="/Stakeout/ArcDesign/Station"/>
      <xsl:apply-templates select="/Stakeout/ArcDesign/Offset"/>
      <xsl:apply-templates select="/Stakeout/LinearDeltas/VDistSlope"/>
    </xsl:when>

    <xsl:otherwise>  <!-- Centre point of arc or Intersection point of arc -->
      <xsl:if test="$NorthFirst = 'true'">
        <xsl:apply-templates select="/Stakeout/GridDeltas/DeltaNorth"/>
        <xsl:apply-templates select="/Stakeout/GridDeltas/DeltaEast"/>
      </xsl:if>
      <xsl:if test="$EastFirst = 'true'">
        <xsl:apply-templates select="/Stakeout/GridDeltas/DeltaEast"/>
        <xsl:apply-templates select="/Stakeout/GridDeltas/DeltaNorth"/>
      </xsl:if>
      <xsl:apply-templates select="/Stakeout/GridDeltas/DeltaElevation"/>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:text>GroupboxEnd</xsl:text>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<!-- Key words that can be used to control the presentation

     Screen display:
     Including the key word WideScreen at the start of the output will instruct
     the Survey Controller to use the full screen width for the output details
     
     Font sizes:
     Font sizes can be defined for the Prompts (PromptSize) and Values (ValueSize)
     There are 3 font sizes available for Prompts: Small, Medium and Large
     There are 4 font sizes available for Values:  Small, Medium, Large and ExtraLarge
     Default Prompt and Value sizes can be set at the start of the output using
     the following commands:
     DefaultPromptSize=Size where Size = Small, Medium or Large
     DefaultValueSize=Size  where Size = Small, Medium, Large or ExtraLarge
     Individual Prompt and Value sizes can be assigned to specific Prompt/Value pairs
     by following the Prompt/Value pair with a second tab separator then space separated
     PromptSize=Size and ValueSize=Size commands where the Size options are as defined 
     above:
     Prompt \t Value \t PromptSize=Size ValueSize=Size\r\n
     
     Font Colors:
     The default color for Prompts and Values is Black but colors can be assigned to
     Prompts and Values on an individual basis by following the Prompt/Value pair with
     a second tab separator then space separated PromptColor=Color and ValueColor=Color
     commands:
     Prompt \t Value \t PromptColor=Color ValueColor=Color\r\n
     The available colors are listed below:
     Red, Mud, Blue, Cyan, Gray, Lime, Navy, Rose, Teal, Black, Brick, Cream, Green, 
     Lemon, Olive, Peach, White, Orange, Purple, Silver, Yellow, Magenta, DarkGray, 
     MediumGray, BlueBlack, LightBlue, RoyalBlue, Turquoise, TrimbleBlue and 
     TrimblePaleBlue
     
     Note: These PromptColor and ValueColor commands can be combined with the PromptSize 
     and ValueSize commands.
-->
  
<xsl:template match="/Stakeout">
  <xsl:text>WideScreen</xsl:text>
  <xsl:value-of select="$Eol"/>  
  <xsl:text>DefaultPromptSize=Medium</xsl:text>
  <xsl:value-of select="$Eol"/>  
  <xsl:text>DefaultValueSize=Large</xsl:text>
  <xsl:value-of select="$Eol"/>  

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

  <xsl:if test="(/Stakeout/ArcDesign/StakeoutMethod = 'PointOnArc') or
                (/Stakeout/ArcDesign/StakeoutMethod = 'PointOffsetFromArc')">
    <xsl:call-template name="DesignArc"/>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>