<?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="Offset" select="format-number(/Stakeout/RoadDesign/Offset,'0.000')"/>

<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="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="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="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="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:variable name="DisplayCode">
  <xsl:if test="/Stakeout/RoadDesign/StakeoutMethod = 'StationAndOffset'">true</xsl:if>
  <xsl:if test="/Stakeout/RoadDesign/StakeoutMethod = 'SideSlopeFromAlignment'">true</xsl:if>
  <xsl:if test="/Stakeout/RoadDesign/StakeoutMethod = 'NearestOffset'">true</xsl:if>
  <xsl:if test="/Stakeout/RoadDesign/StakeoutMethod = 'StationAlongString'">true</xsl:if>
  <xsl:if test="/Stakeout/RoadDesign/StakeoutMethod = 'PositionFromFile'">true</xsl:if>
</xsl:variable>

<xsl:variable name="DisplayDeltaStation">
  <xsl:if test="/Stakeout/RoadDesign/StakeoutMethod = 'StationAndOffset'">true</xsl:if>
  <xsl:if test="/Stakeout/RoadDesign/StakeoutMethod = 'StationOnString'">true</xsl:if>
  <xsl:if test="/Stakeout/RoadDesign/StakeoutMethod = 'SideSlopeFromAlignment'">true</xsl:if>
  <xsl:if test="/Stakeout/RoadDesign/StakeoutMethod = 'PositionFromFile'">true</xsl:if>
</xsl:variable>

<xsl:variable name="DisplayDeltaOffset">
  <xsl:if test="not(/Stakeout/RoadDesign/StakeoutMethod = 'PositionOnRoad')">true</xsl:if>
</xsl:variable>

<xsl:variable name="StakingSideSlope">
  <xsl:if test="format-number(/Stakeout/RoadDesign/SideSlopeGrade,'0.000')='NaN'">false</xsl:if>
  <xsl:if test="not(format-number(/Stakeout/RoadDesign/SideSlopeGrade,'0.000')='NaN')">true</xsl:if>
</xsl:variable>

<xsl:variable name="RoadType">
  <xsl:if test="/Stakeout/RoadType = 'DBENTITY_TrimbleRoad'">trimble</xsl:if>
  <xsl:if test="/Stakeout/RoadType = 'DBENTITY_GenioRoad'">genio</xsl:if>
  <xsl:if test="/Stakeout/RoadType = 'DBENTITY_LandXmlRoad'">landxml</xsl:if>
</xsl:variable>

<xsl:variable name="DisplayStationConstructionOffset">
  <xsl:if test="$RoadType = 'genio'">
    <xsl:if test="/Stakeout/RoadDesign/StakeoutMethod = 'StationOnString'">
      <xsl:if test="$StakingSideSlope = 'false'">true</xsl:if>
    </xsl:if>
  </xsl:if>
    <xsl:if test="$RoadType = 'trimble'">
    <xsl:if test="/Stakeout/XMLStringRoad = 'true'">
      <xsl:if test="/Stakeout/RoadDesign/StakeoutMethod = 'StationAndOffset'">
        <xsl:if test="$StakingSideSlope = 'false'">true</xsl:if>
      </xsl:if>
    </xsl:if>
  </xsl:if>
</xsl:variable>

<xsl:template name="FormatGradeAsPercentage">
  <xsl:param name="GradeValueAsPercentage"/>
  <xsl:param name="decPlaces" select="4"/>
  <xsl:variable name="decPlStr">
    <xsl:choose>
      <xsl:when test="$decPlaces = 0"><xsl:value-of select="'0'"/></xsl:when>
      <xsl:when test="$decPlaces = 1"><xsl:value-of select="'0.0'"/></xsl:when>
      <xsl:when test="$decPlaces = 2"><xsl:value-of select="'0.00'"/></xsl:when>
      <xsl:when test="$decPlaces = 3"><xsl:value-of select="'0.000'"/></xsl:when>
      <xsl:when test="$decPlaces = 4"><xsl:value-of select="'0.0000'"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="'0.0000'"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:value-of select="format-number($GradeValueAsPercentage,$decPlStr,'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="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="FormatGradeAsDecimalAngle">
  <xsl:param name="GradeValueAsDecimalAngle"/>
  <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($GradeValueAsDecimalAngle * $AngleFactor),$DecAngleDPs,'null')"/>
  <xsl:value-of select="$AngleSuffix"/>
</xsl:template>

<xsl:template name="FormatGradeAsDMS">
  <xsl:param name="GradeValueAsDecimalAngle"/>
  <xsl:variable name="Sign">
    <xsl:if test="$GradeValueAsDecimalAngle &lt; '0.0'">-1</xsl:if>
    <xsl:if test="$GradeValueAsDecimalAngle &gt;= '0.0'">1</xsl:if>
  </xsl:variable>
  <xsl:variable name="PositiveDecimalDegrees" select="number($GradeValueAsDecimalAngle * $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:value-of select="format-number(number($NormalisedDegrees * $Sign), '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="FormatGradeAsAngle">
  <xsl:param name="Grade"/>
  <xsl:param name="DisplayAbsoluteValue" select="'true'"/>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:if test="$Grade = ''">
    <xsl:value-of select="format-number($Grade,'#','null')"/>
  </xsl:if>
  <xsl:if test="not($Grade = '')">
    <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="FormatGradeAsDecimalAngle">
        <xsl:with-param name="GradeValueAsDecimalAngle" select="$AbsValue"/>
      </xsl:call-template>
    </xsl:if>
    <xsl:if test="$GradeDisplayType = 'DMS'">
      <xsl:call-template name="FormatGradeAsDMS">
        <xsl:with-param name="GradeValueAsDecimalAngle" select="$AbsValue"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:if>
  <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="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="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:param name="Color" select="'Black'"/>
  <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=</xsl:text>
  <xsl:value-of select="$Color"/>
  <xsl:value-of select="$Eol"/>
</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="FormatDepth">
  <xsl:param name="Depth"/>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:value-of select="format-number(number($Depth * $VertDistFactor),$DistDPs,'null')"/>
  <xsl:if test="not(format-number($Depth,'0.000')='NaN')">
    <xsl:value-of select="$VertDistAbbrev"/>
  </xsl:if>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template name="FormatStation">
  <xsl:param name="Station"/>
  <xsl:param name="Zone" select="''"/>
  <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 test="Zone != ''">
      <xsl:value-of select="':'"/>
      <xsl:value-of select="format-number($Zone,'0')"/>
    </xsl:if>
  </xsl:if>
</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/CatchPointTemplateReport">
  <xsl:text>DisplayReportSoftkey</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/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/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/DeltaElevation">
  <xsl:choose>
    <xsl:when test="/Stakeout/@UseRailroadTerminology = 'true'">Lift to railroad</xsl:when>
    <xsl:otherwise>V.Dist. to road</xsl:otherwise>
  </xsl:choose>
  <xsl:call-template name="FormatPrefixedVerticalDistance">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/RoadDesign/Name">
  <xsl:choose>
    <xsl:when test="/Stakeout/@UseRailroadTerminology = 'true'"><xsl:text>Railroad name</xsl:text></xsl:when>
    <xsl:otherwise><xsl:text>Road name</xsl:text></xsl:otherwise>
  </xsl:choose>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:copy-of select="."/>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template match="/Stakeout/RoadDesign/Code">
  <xsl:if test="$DisplayCode = 'true'">
    <xsl:text>Code</xsl:text>
    <xsl:value-of select="$FieldSeparator"/>
    <xsl:copy-of select="."/>
    <xsl:value-of select="$Eol"/>
  </xsl:if>
</xsl:template>

<xsl:template match="/Stakeout/RoadDesign/StakeoutMethod">
  <xsl:variable name="Method">
    <xsl:choose>
      <xsl:when test=". = 'StationAndOffset'">
        <xsl:choose>
          <xsl:when test="/Stakeout/@UseChainageTerminology = 'true'">Chainage and offset</xsl:when>
          <xsl:otherwise>Station and offset</xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:when test=". = 'PositionOnRoad'">
        <xsl:choose>
          <xsl:when test="/Stakeout/@UseRailroadTerminology = 'true'"><xsl:text>Position on railroad</xsl:text></xsl:when>
          <xsl:otherwise><xsl:text>Position on road</xsl:text></xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:when test=". = 'SideSlopeFromAlignment'"><xsl:text>Side slope from alignment</xsl:text></xsl:when>
      <xsl:when test=". = 'StationOnString'">
        <xsl:choose>
          <xsl:when test="/Stakeout/@UseChainageTerminology = 'true'">Chainage on string</xsl:when>
          <xsl:otherwise>Station on string</xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:when test=". = 'StationAlongString'"><xsl:text>Along string</xsl:text></xsl:when>
      <xsl:when test=". = 'NearestOffset'"><xsl:text>Nearest offset</xsl:text></xsl:when>
      <xsl:when test=". = 'PositionFromFile'"><xsl:text>Position from file</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/RoadDesign/EquatedStation">
  <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="Station"/>
    <xsl:with-param name="Zone" select="Zone"/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/RoadDesign/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/RoadDesign/StationClass">
  <xsl:if test="string-length(.) &gt; '0'">
    <xsl:if test="contains(., 'StraightToSpiral')"><xsl:value-of select="$Space"/><xsl:text>(TS)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'SpiralToArc')"><xsl:value-of select="$Space"/><xsl:text>(SC)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'ArcToSpiral')"><xsl:value-of select="$Space"/><xsl:text>(CS)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'SpiralToStraight')"><xsl:value-of select="$Space"/><xsl:text>(ST)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'SpiralToSpiral')"><xsl:value-of select="$Space"/><xsl:text>(SS)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'StraightToArc')"><xsl:value-of select="$Space"/><xsl:text>(PC)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'ArcToStraight')"><xsl:value-of select="$Space"/><xsl:text>(PT)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'IntersectionPoint')"><xsl:value-of select="$Space"/><xsl:text>(PI)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'RoadStart')"><xsl:value-of select="$Space"/><xsl:text>(S)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'RoadEnd')"><xsl:value-of select="$Space"/><xsl:text>(E)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'NoneToArc')"><xsl:value-of select="$Space"/><xsl:text>(PBC)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'NoneToStraight')"><xsl:value-of select="$Space"/><xsl:text>(PBT)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'ArcToArcCompound')"><xsl:value-of select="$Space"/><xsl:text>(PCC)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'ArcToArcReverse')"><xsl:value-of select="$Space"/><xsl:text>(PRC)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'ArcToNone')"><xsl:value-of select="$Space"/><xsl:text>(PAC)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'StraightToNone')"><xsl:value-of select="$Space"/><xsl:text>(PAT)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'VerticalCurveStart')"><xsl:value-of select="$Space"/><xsl:text>(VCS)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'VerticalCurveEnd')"><xsl:value-of select="$Space"/><xsl:text>(VCE)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'VerticalPoint')"><xsl:value-of select="$Space"/><xsl:text>(VPI)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'CrossSection')"><xsl:value-of select="$Space"/><xsl:text>(XS)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'VerticalSagPoint')"><xsl:value-of select="$Space"/><xsl:text>(LO)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'VerticalSummitPoint')"><xsl:value-of select="$Space"/><xsl:text>(HI)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'TemplateAssignment')"><xsl:value-of select="$Space"/><xsl:text>(T)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'SuperelevationStart')"><xsl:value-of select="$Space"/><xsl:text>(SES)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'WideningStart')"><xsl:value-of select="$Space"/><xsl:text>(WS)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'SuperelevationEnd')"><xsl:value-of select="$Space"/><xsl:text>(SEE)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'WideningMaximum')"><xsl:value-of select="$Space"/><xsl:text>(WM)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'WideningEnd')"><xsl:value-of select="$Space"/><xsl:text>(WE)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'SuperelevationMaximum')"><xsl:value-of select="$Space"/><xsl:text>(SEM)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'SuperelevationEqual')"><xsl:value-of select="$Space"/><xsl:text>(SEQ)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'SuperelevationInterest')"><xsl:value-of select="$Space"/><xsl:text>(SE)</xsl:text></xsl:if>
    <xsl:if test="contains(., 'StationEquation')"><xsl:value-of select="$Space"/>
      <xsl:choose>
        <xsl:when test="/Stakeout/@UseChainageTerminology = 'true'"><xsl:text>(CHEQ)</xsl:text></xsl:when>
        <xsl:otherwise><xsl:text>(STEQ)</xsl:text></xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:if>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template match="/Stakeout/RoadDesign/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/RoadDesign/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/RoadDesign/OriginalDesignElevation">
  <xsl:text>Original design elev</xsl:text>
  <xsl:call-template name="FormatElevation">
    <xsl:with-param name="Elevation" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/RoadDesign/CrossSlopeDelta">
  <xsl:text>V.Dist. cross slope</xsl:text>
  <xsl:call-template name="FormatPrefixedVerticalDistance">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/RoadDesign/CrossSlopeOffset">
  <xsl:text>Cross slope offset</xsl:text>
  <xsl:call-template name="FormatVerticalDistance">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>
</xsl:template>

<xsl:template match="/Stakeout/RoadDesign/HorizontalConstructionOffsetApplied">
  <xsl:variable name="OffsetMethod">
    <xsl:choose>
      <xsl:when test=". = 'Horizontal'"><xsl:text>Horizontal</xsl:text></xsl:when>
      <xsl:when test=". = 'Slope'"><xsl:text>Slope</xsl:text></xsl:when>
    </xsl:choose>
  </xsl:variable>
  <xsl:text>Horizontal offset method</xsl:text>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:copy-of select="$OffsetMethod"/>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template match="/Stakeout/RoadDesign/HorizontalOffsetMethod">
  <xsl:variable name="Method">
    <xsl:choose>
      <xsl:when test=". = 'Tangential to master'"><xsl:text>Alignment</xsl:text></xsl:when>
      <xsl:when test=". = 'Tangential to substring'"><xsl:text>String</xsl:text></xsl:when>
    </xsl:choose>
  </xsl:variable>
  <xsl:text>Horizontal offset perpendicular to</xsl:text>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:value-of select="$Method"/>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template match="/Stakeout/RoadDesign/SubgradeDepth">
  <xsl:text>Depth to subgrade</xsl:text>
  <xsl:call-template name="FormatDepth">
    <xsl:with-param name="Depth" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/RoadDesign/HorizontalConstructionOffset">
  <xsl:text>Horizontal offset</xsl:text>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:variable name="Factor">
    <xsl:if test=". &lt; '0.0'"><xsl:value-of select="number(-1 * $HorzDistFactor)"/></xsl:if>
    <xsl:if test=". &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(. * $Factor),$DistDPs,'null')"/>
      <xsl:if test="not(format-number(.,'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(. * $Factor)"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:if test="not(format-number(.,'0.000')='NaN')">
    <xsl:variable name="OffsetSuffix">
      <xsl:if test="$Offset = '0.000'">
        <xsl:if test=". &lt; '0.0'"><xsl:text>Left</xsl:text></xsl:if>
        <xsl:if test=". &gt; '0.0'"><xsl:text>Right</xsl:text></xsl:if>
      </xsl:if>
      <xsl:if test="$Offset != '0.000'">
        <xsl:if test=". &lt; '0.0'"><xsl:text>In</xsl:text></xsl:if>
        <xsl:if test=". &gt; '0.0'"><xsl:text>Out</xsl:text></xsl:if>
      </xsl:if>
    </xsl:variable>
    <xsl:value-of select="$Space"/>
    <xsl:value-of select="$OffsetSuffix"/>
  </xsl:if>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:text>ValueColor=Purple</xsl:text>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template match="/Stakeout/RoadDesign/VerticalConstructionOffset">
  <xsl:if test="/Stakeout/RoadDesign/StakeoutMethod = 'PositionOnRoad'">
    <xsl:text>V.Offset</xsl:text>
  </xsl:if>
  <xsl:if test="not(/Stakeout/RoadDesign/StakeoutMethod = 'PositionOnRoad')">
    <xsl:text>Vertical offset</xsl:text>
  </xsl:if >
  <xsl:call-template name="FormatVerticalDistance">
    <xsl:with-param name="Distance" select="."/>
    <xsl:with-param name="Color" select="'Purple'"/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/RoadDesign/StationConstructionOffset">
  <xsl:if test=". != 0">
    <xsl:choose>
      <xsl:when test="/Stakeout/@UseChainageTerminology = 'true'">Chainage offset</xsl:when>
      <xsl:otherwise>Station offset</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/RoadDesign/SideSlopeGrade">
  <xsl:if test=". &lt; '0.0'"><xsl:text>Fill slope (design)</xsl:text></xsl:if>
  <xsl:if test=". &gt;= '0.0'"><xsl:text>Cut slope (design)</xsl:text></xsl:if>
  <xsl:call-template name="FormatGrade">
    <xsl:with-param name="Grade" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/RoadDesign/SideSlopeGradeInDecimalDegrees">
  <xsl:if test=". &lt; '0.0'"><xsl:text>Fill slope (design)</xsl:text></xsl:if>
  <xsl:if test=". &gt;= '0.0'"><xsl:text>Cut slope (design)</xsl:text></xsl:if>
  <xsl:call-template name="FormatGradeAsAngle">
    <xsl:with-param name="Grade" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/RoadDesign/DitchOffset">
  <xsl:text>Cut ditch width</xsl:text>
  <xsl:call-template name="FormatHorizontalDistance">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/RoadDesign/NewOffsetForSideSlope">
  <xsl:text>New hinge offset</xsl:text>
  <xsl:call-template name="FormatOffset">
    <xsl:with-param name="Offset" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/RoadDesign/SideSlopeFromAlignmentHingeDefinition/Method">
  <xsl:variable name="Method">
    <xsl:choose>
      <xsl:when test=". = 'OffsetAndElevation'"><xsl:text>Offset and elevation</xsl:text></xsl:when>
      <xsl:when test=". = 'OffsetAndGrade'"><xsl:text>Offset and slope</xsl:text></xsl:when>
      <xsl:when test=". = 'OffsetAndVerticalDistance'"><xsl:text>Offset and vertical difference</xsl:text></xsl:when>
    </xsl:choose>
  </xsl:variable>
  <xsl:text>Hinge derivation method</xsl:text>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:value-of select="$Method"/>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template match="/Stakeout/RoadDesign/SideSlopeFromAlignmentHingeDefinition/Offset">
  <xsl:text>Hinge offset</xsl:text>
  <xsl:call-template name="FormatOffset">
    <xsl:with-param name="Offset" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/RoadDesign/SideSlopeFromAlignmentHingeDefinition/Grade">
  <xsl:text>Hinge slope</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/RoadDesign/SideSlopeFromAlignmentHingeDefinition/GradeInDecimalDegrees">
  <xsl:text>Hinge slope</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/RoadDesign/SideSlopeFromAlignmentHingeDefinition/VerticalDistance">
  <xsl:text>Hinge vertical diff</xsl:text>
  <xsl:call-template name="FormatVerticalDistance">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/RoadDesign/SideSlopeFromAlignmentHingeDefinition/Elevation">
  <xsl:text>Hinge elevation</xsl:text>
  <xsl:call-template name="FormatElevation">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/RoadDesign/SideSlopeFromAlignmentHingeDefinition">
  <xsl:apply-templates select="/Stakeout/RoadDesign/SideSlopeFromAlignmentHingeDefinition/Method"/>
  <xsl:apply-templates select="/Stakeout/RoadDesign/SideSlopeFromAlignmentHingeDefinition/Offset"/>
  <xsl:if test="not($GradeUnit='Angle')">
     <xsl:apply-templates select="/Stakeout/RoadDesign/SideSlopeFromAlignmentHingeDefinition/Grade"/>
  </xsl:if>
  <xsl:if test="$GradeUnit='Angle'">
     <xsl:apply-templates select="/Stakeout/RoadDesign/SideSlopeFromAlignmentHingeDefinition/GradeInDecimalDegrees"/>
  </xsl:if>
  <xsl:apply-templates select="/Stakeout/RoadDesign/SideSlopeFromAlignmentHingeDefinition/VerticalDistance"/>
  <xsl:apply-templates select="/Stakeout/RoadDesign/SideSlopeFromAlignmentHingeDefinition/Elevation"/>
</xsl:template>



<xsl:template match="/Stakeout/CatchPoint/Offset">
  <xsl:text>Distance to alignment</xsl:text>
  <xsl:variable name="AbsValue" select="concat(substring('-',2 - (. &lt; 0)), '1') * ."/>
  <xsl:call-template name="FormatHorizontalDistance">
    <xsl:with-param name="Distance" select="$AbsValue"/>
  </xsl:call-template>  
</xsl:template>

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

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

<xsl:template match="/Stakeout/CatchPoint/VerticalDistanceToHingePoint">
  <xsl:choose>
    <xsl:when test="/Stakeout/@UseRailroadTerminology = 'true'">Lift to hinge point</xsl:when>
    <xsl:otherwise>V.Dist to hinge point</xsl:otherwise>
  </xsl:choose>
  <xsl:call-template name="FormatPrefixedVerticalDistance">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/CatchPoint/SlopeDistanceToHingePoint">
  <xsl:text>S.dist to hinge</xsl:text>
  <xsl:call-template name="FormatHorizontalDistance">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/CatchPoint/SlopeDistanceToHingePointWithConstructionOffset">
  <xsl:text>S.dist to hinge + Constr off</xsl:text>
  <xsl:call-template name="FormatHorizontalDistance">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/CatchPoint/AsStakedSideSlopeGrade">
  <xsl:if test="not(format-number(.,'0.000')='NaN')">
    <xsl:if test=". &lt; '0.0'"><xsl:text>Fill slope (as staked)</xsl:text></xsl:if>
    <xsl:if test=". &gt;= '0.0'"><xsl:text>Cut slope (as staked)</xsl:text></xsl:if>
    <xsl:call-template name="FormatGrade">
      <xsl:with-param name="Grade" select="."/>
    </xsl:call-template>  
  </xsl:if>
</xsl:template>

<xsl:template match="/Stakeout/CatchPoint/AsStakedSideSlopeGradeInDecimalDegrees">
  <xsl:if test="not(format-number(.,'0.000')='NaN')">
    <xsl:if test=". &lt; '0.0'"><xsl:text>Fill slope (as staked)</xsl:text></xsl:if>
    <xsl:if test=". &gt;= '0.0'"><xsl:text>Cut slope (as staked)</xsl:text></xsl:if>
    <xsl:call-template name="FormatGradeAsAngle">
      <xsl:with-param name="Grade" select="."/>
    </xsl:call-template>  
  </xsl:if>
</xsl:template>

<xsl:template match="/Stakeout/DeltasRelativeToDTM/DeltaElevation">
  <xsl:choose>
    <xsl:when test="/Stakeout/@UseRailroadTerminology = 'true'">Lift surface</xsl:when>
    <xsl:otherwise>V.Dist surface</xsl:otherwise>
  </xsl:choose>
  <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/SecondaryRoadDeltas/RoadName">
  <xsl:choose>
    <xsl:when test="/Stakeout/@UseRailroadTerminology = 'true'"><xsl:text>Railroad name</xsl:text></xsl:when>
    <xsl:otherwise><xsl:text>Road name</xsl:text></xsl:otherwise>
  </xsl:choose>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:copy-of select="."/>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template match="/Stakeout/SecondaryRoadDeltas/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:value-of select="$Eol"/>
</xsl:template>

<xsl:template match="/Stakeout/SecondaryRoadDeltas/CalculatedHorizontalConstructionOffset">
  <xsl:variable name="OffsetSuffix">
    <xsl:if test=". &lt; '0.0'"><xsl:text>In</xsl:text></xsl:if>
    <xsl:if test=". &gt; '0.0'"><xsl:text>Out</xsl:text></xsl:if>
  </xsl:variable>
  <xsl:variable name="Factor">
    <xsl:if test=". &lt; '0.0'"><xsl:value-of select="number(-1 * $HorzDistFactor)"/></xsl:if>
    <xsl:if test=". &gt;= '0.0'"><xsl:value-of select="$HorzDistFactor"/></xsl:if>
  </xsl:variable>
  <xsl:text>Horizontal construction offset (calc'd)</xsl:text>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:choose>
    <xsl:when test="$DisplayFtAndInches = 'false'">
      <xsl:value-of select="format-number(number(. * $Factor),$DistDPs,'null')"/>
      <xsl:if test="not(format-number(.,'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(. * $Factor)"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:if test="not(format-number(.,'0.000')='NaN')">
    <xsl:value-of select="$Space"/>
    <xsl:value-of select="$OffsetSuffix"/>
  </xsl:if>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template match="/Stakeout/SecondaryRoadDeltas/OffsetToSecondaryMasterAlignment">
  <xsl:text>Distance to alignment</xsl:text>
  <xsl:call-template name="FormatHorizontalDistance">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>  
</xsl:template>

<xsl:template match="/Stakeout/SecondaryRoadDeltas/DeltaElevation">
  <xsl:choose>
    <xsl:when test="/Stakeout/@UseRailroadTerminology = 'true'">Lift to railroad</xsl:when>
    <xsl:otherwise>V.Dist. to road</xsl:otherwise>
  </xsl:choose>
  <xsl:call-template name="FormatPrefixedVerticalDistance">
    <xsl:with-param name="Distance" select="."/>
  </xsl:call-template>  
</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:if test="$DisplayDeltaStation = 'true'">
    <xsl:apply-templates select="/Stakeout/LinearDeltas/DeltaStation"/>
  </xsl:if>
  <xsl:if test="$DisplayDeltaOffset = 'true'">
    <xsl:apply-templates select="/Stakeout/LinearDeltas/DeltaOffset"/>
  </xsl:if>
  <xsl:apply-templates select="/Stakeout/LinearDeltas/DeltaElevation"/>
  <xsl:apply-templates select="/Stakeout/RoadDesign/CrossSlopeDelta"/>
  <xsl:apply-templates select="/Stakeout/CatchPoint/Offset"/>
  <xsl:apply-templates select="/Stakeout/CatchPoint/Elevation"/>
  <xsl:apply-templates select="/Stakeout/CatchPoint/HorizontalDistanceToHingePoint"/>
  <xsl:apply-templates select="/Stakeout/CatchPoint/VerticalDistanceToHingePoint"/>
  <xsl:apply-templates select="/Stakeout/CatchPoint/SlopeDistanceToHingePoint"/>
  <xsl:apply-templates select="/Stakeout/CatchPoint/SlopeDistanceToHingePointWithConstructionOffset"/>
  <xsl:apply-templates select="/Stakeout/DeltasRelativeToDTM/DeltaElevation"/>
  <xsl:apply-templates select="/Stakeout/DeltasRelativeToDTM/DTMName"/>
  <xsl:apply-templates select="/Stakeout/DeltasRelativeToDTM/VerticalOffset"/>
  <xsl:text>GroupboxEnd</xsl:text>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template name="DesignRoad">
  <xsl:text>GroupboxStart</xsl:text>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:choose>
    <xsl:when test="/Stakeout/@UseRailroadTerminology = 'true'"><xsl:text>Design Railroad</xsl:text></xsl:when>
    <xsl:otherwise><xsl:text>Design Road</xsl:text></xsl:otherwise>
  </xsl:choose>
  <xsl:value-of select="$Eol"/>
  <xsl:choose>
    <xsl:when test="/Stakeout/RoadDesign/EquatedStation">
      <xsl:apply-templates select="/Stakeout/RoadDesign/EquatedStation"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="/Stakeout/RoadDesign/Station"/>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:apply-templates select="/Stakeout/RoadDesign/StationClass"/>
  <xsl:choose>
    <xsl:when test="(/Stakeout/RoadDesign/StakeoutMethod = 'NearestOffset') and
                    (Code != '')">
      <xsl:apply-templates select="/Stakeout/RoadDesign/Code"/>
    </xsl:when>
    <xsl:otherwise>  <!-- Output the offset value if present -->
      <xsl:if test="/Stakeout/RoadDesign/Offset != ''">
        <xsl:apply-templates select="/Stakeout/RoadDesign/Offset"/>
      </xsl:if>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:if test="$StakingSideSlope = 'true'">
    <xsl:if test="not($GradeUnit='Angle')">
      <xsl:apply-templates select="/Stakeout/RoadDesign/SideSlopeGrade"/>
    </xsl:if>
    <xsl:if test="$GradeUnit='Angle'">
      <xsl:apply-templates select="/Stakeout/RoadDesign/SideSlopeGradeInDecimalDegrees"/>
    </xsl:if>
  </xsl:if>
  <xsl:text>GroupboxEnd</xsl:text>
  <xsl:value-of select="$Eol"/>
</xsl:template>

<xsl:template name="ConstructionOffsets">
  <xsl:if test="((/Stakeout/RoadDesign/HorizontalConstructionOffset != 0) and
                 (/Stakeout/RoadDesign/HorizontalConstructionOffset != '')) or
                ((/Stakeout/RoadDesign/VerticalConstructionOffset != 0)and
                 (/Stakeout/RoadDesign/VerticalConstructionOffset != ''))">
    <xsl:text>GroupboxStart</xsl:text>
    <xsl:value-of select="$FieldSeparator"/>
    <xsl:text>Construction offsets</xsl:text>
    <xsl:value-of select="$Eol"/>
    <xsl:apply-templates select="/Stakeout/RoadDesign/HorizontalConstructionOffset"/>
    <xsl:apply-templates select="/Stakeout/RoadDesign/VerticalConstructionOffset"/>
    <xsl:if test="$DisplayStationConstructionOffset = 'true'">
      <xsl:apply-templates select="/Stakeout/RoadDesign/StationConstructionOffset"/>
    </xsl:if>
    <xsl:text>GroupboxEnd</xsl:text>
    <xsl:value-of select="$Eol"/>
  </xsl:if>
</xsl:template>


<xsl:template match="/Stakeout/SecondaryRoadDeltas">
  <xsl:text>GroupboxStart</xsl:text>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:text>Secondary road deltas</xsl:text>
  <xsl:value-of select="$Eol"/>
    <xsl:apply-templates select="/Stakeout/SecondaryRoadDeltas/DeltaElevation"/>
  <xsl:text>GroupboxEnd</xsl:text>
  <xsl:value-of select="$Eol"/>

  <xsl:text>GroupboxStart</xsl:text>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:text>Secondary Road Design</xsl:text>
  <xsl:value-of select="$Eol"/>
    <xsl:apply-templates select="/Stakeout/SecondaryRoadDeltas/Station"/>

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

  <xsl:text>GroupboxEnd</xsl:text>
  <xsl:value-of select="$Eol"/>

  <xsl:text>GroupboxStart</xsl:text>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:text>Secondary Construction Offsets</xsl:text>
  <xsl:value-of select="$Eol"/>
    <xsl:apply-templates select="/Stakeout/SecondaryRoadDeltas/CalculatedHorizontalConstructionOffset"/>
  <xsl:text>GroupboxEnd</xsl:text>
  <xsl:value-of select="$Eol"/>
</xsl:template>


<!-- A function to output the individual deltas of each template component -->
<xsl:template name="crossSectionElements">
  <xsl:text>GroupboxStart</xsl:text>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:text>Cross-section elements</xsl:text>
  <xsl:value-of select="$Eol"/>

  <xsl:for-each select="/Stakeout/CatchPointTemplateReport">
    <xsl:variable name="from">
      <xsl:if test="position() = 1">
        <xsl:text>Catch</xsl:text>
      </xsl:if>
      <xsl:if test="position() > 1">
        <xsl:for-each select="preceding-sibling::CatchPointTemplateReport[1]">
          <xsl:value-of select="Code"/>
        </xsl:for-each>
      </xsl:if>
    </xsl:variable>

    <xsl:variable name="dOffset">
      <xsl:if test="position() = 1">
        <xsl:value-of select="HorizontalDistance"/>
      </xsl:if>
      <xsl:if test="position() > 1">
        <xsl:variable name="startDist" select="HorizontalDistance"/>
        <xsl:for-each select="preceding-sibling::CatchPointTemplateReport[1]">
          <xsl:value-of select="$startDist - HorizontalDistance"/>
        </xsl:for-each>
      </xsl:if>
    </xsl:variable>

    <xsl:variable name="dElev">
      <xsl:if test="position() = 1">
        <xsl:value-of select="DeltaElevation"/>
      </xsl:if>
      <xsl:if test="position() > 1">
        <xsl:variable name="startElev" select="DeltaElevation"/>
        <xsl:for-each select="preceding-sibling::CatchPointTemplateReport[1]">
          <xsl:value-of select="$startElev - DeltaElevation"/>
        </xsl:for-each>
      </xsl:if>
    </xsl:variable>

    <xsl:variable name="grade">  <!-- Compute the grade using the rounded $dElev and $dOffset values so the grade matches the delta values -->
      <xsl:choose>
        <xsl:when test="$GradeDisplayType = 'Ratio'">
          <xsl:call-template name="FormatGradeAsRatio">
            <xsl:with-param name="GradeValueAsPercentage" select="format-number($dElev,$DistDPs,'null') div format-number($dOffset,$DistDPs,'null') * 100"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>  <!-- Output as a percentage grade -->
          <xsl:call-template name="FormatGradeAsPercentage">
            <xsl:with-param name="GradeValueAsPercentage" select="format-number($dElev,$DistDPs,'null') div format-number($dOffset,$DistDPs,'null') * 100"/>
            <xsl:with-param name="decPlaces" select="2"/>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:value-of select="concat($from, ' - ', Code, '      ')"/>
    <xsl:text>Slope</xsl:text>
    <xsl:value-of select="concat(': ', $grade)"/>
    <xsl:value-of select="$FieldSeparator"/>
    <xsl:text>ΔHD</xsl:text>
    <xsl:value-of select="': '"/>
    <xsl:value-of select="format-number(number($dOffset * $HorzDistFactor),$DistDPs,'null')"/>
    <xsl:value-of select="$HorzDistAbbrev"/>
    <xsl:value-of select="'    '"/>
    <xsl:text>ΔElev</xsl:text>
    <xsl:value-of select="': '"/>
    <xsl:value-of select="format-number(number($dElev * $VertDistFactor),$DistDPs,'null')"/>
    <xsl:value-of select="$VertDistAbbrev"/>
    <xsl:value-of select="$Eol"/>

  </xsl:for-each>

  <xsl:text>GroupboxEnd</xsl:text>
  <xsl:value-of select="$Eol"/>
</xsl:template>


<xsl:template name="verticalStakeDeltas">
  <!-- Output Stake deltas group box data -->
  <xsl:text>GroupboxStart</xsl:text>
  <xsl:value-of select="$FieldSeparator"/>
  <xsl:text>Stake deltas</xsl:text>
  <xsl:value-of select="$Eol"/>

  <xsl:apply-templates select="/Stakeout/LinearDeltas/DeltaElevation"/>

  <xsl:if test="/Stakeout/RoadDesign/CrossSlopeDelta">
    <xsl:apply-templates select="/Stakeout/RoadDesign/CrossSlopeDelta"/>
  </xsl:if>

  <xsl:if test="(/Stakeout/RoadDesign/StakeoutMethod = 'PositionOnRoad') and
                (/Stakeout/RoadDesign/VerticalConstructionOffset != '') and
                (/Stakeout/RoadDesign/VerticalConstructionOffset != 0)">
    <xsl:text>Construction V.Offset</xsl:text>
    <xsl:call-template name="FormatVerticalDistance">
      <xsl:with-param name="Distance" select="/Stakeout/RoadDesign/VerticalConstructionOffset"/>
    </xsl:call-template>
  </xsl:if>

  <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"/>  

  <!-- Write out the vertical stake deltas -->
  <xsl:call-template name="verticalStakeDeltas"/>

  <!-- Write out the design details -->
  <xsl:call-template name="DesignRoad"/>

  <!-- Output any construction offsets if required -->
  <xsl:if test="not(/Stakeout/RoadDesign/StakeoutMethod = 'PositionOnRoad')">
    <xsl:call-template name="ConstructionOffsets"/>
  </xsl:if>

  <xsl:apply-templates select="/Stakeout/SecondaryRoadDeltas"/>

  <!-- Add report button if appropriate -->
  <xsl:apply-templates select="/Stakeout/CatchPointTemplateReport[1]"/>

  <!-- Add an extra section that provides delta ht, delta offset to each template point -->
  <xsl:if test="$StakingSideSlope = 'true'">
    <xsl:call-template name="crossSectionElements"/>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>