<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"   
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"            
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<!-- (c) 2006, Trimble Navigation Limited. 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 NAVIGATION LIMITED PROVIDES THIS STYLE SHEET "AS IS" AND WITH ALL FAULTS.         -->
<!-- TRIMBLE NAVIGATION LIMITED SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY -->
<!-- OR FITNESS FOR A PARTICULAR USE. TRIMBLE NAVIGATION LIMITED DOES NOT WARRANT THAT THE     -->
<!-- OPERATION OF THIS STYLE SHEET WILL BE UNINTERRUPTED OR ERROR FREE.                        -->

<xsl:output method="text" omit-xml-declaration="no" encoding="ISO-8859-1"/>

<xsl:variable name="includeHeadingLines" select="'Yes'"/>
<xsl:variable name="outputFormattedStationVals" select="'Yes'"/>

<!-- Set the numeric display details i.e. decimal point, thousands separator etc -->
<xsl:variable name="DecPt" select="'.'"/>    <!-- Change as appropriate for US/European -->
<xsl:variable name="GroupSep" select="','"/> <!-- Change as appropriate for US/European -->
<!-- Also change decimal-separator & grouping-separator in decimal-format below 
     as appropriate for US/European output -->
<xsl:decimal-format name="Standard" 
                    decimal-separator="."
                    grouping-separator=","
                    infinity="Infinity"
                    minus-sign="-"
                    NaN="                "
                    percent="%"
                    per-mille="&#2030;"
                    zero-digit="0" 
                    digit="#" 
                    pattern-separator=";" />

<xsl:variable name="DecPl0" select="'#0'"/>
<xsl:variable name="DecPl1" select="concat('#0', $DecPt, '0')"/>
<xsl:variable name="DecPl2" select="concat('#0', $DecPt, '00')"/>
<xsl:variable name="DecPl3" select="concat('#0', $DecPt, '000')"/>
<xsl:variable name="DecPl4" select="concat('#0', $DecPt, '0000')"/>
<xsl:variable name="DecPl5" select="concat('#0', $DecPt, '00000')"/>
<xsl:variable name="DecPl8" select="concat('#0', $DecPt, '00000000')"/>

<xsl:variable name="fileExt" select="'dc'"/>

<!-- User variable definitions - Appropriate fields are displayed on the       -->
<!-- Survey Controller screen to allow the user to enter specific values       -->
<!-- which can then be used within the style sheet definition to control the   -->
<!-- output data.                                                              -->
<!--                                                                           -->
<!-- All user variables must be identified by a variable element definition    -->
<!-- named starting with 'userField' (case sensitive) followed by one or more  -->
<!-- characters uniquely identifying the user variable definition.             -->
<!--                                                                           -->
<!-- The text within the 'select' field for the user variable description      -->
<!-- references the actual user variable and uses the '|' character to         -->
<!-- separate the definition details into separate fields as follows:          -->
<!-- For all user variables the first field must be the name of the user       -->
<!-- variable itself (this is case sensitive) and the second field is the      -->
<!-- prompt that will appear on the Survey Controller screen.                  -->
<!-- The third field defines the variable type - there are four possible       -->
<!-- variable types: Double, Integer, String and StringMenu.  These variable   -->
<!-- type references are not case sensitive.                                   -->
<!-- The fields that follow the variable type change according to the type of  -->
<!-- variable as follow:                                                       -->
<!-- Double and Integer: Fourth field = optional minimum value                 -->
<!--                     Fifth field = optional maximum value                  -->
<!--   These minimum and maximum values are used by the Survey Controller for  -->
<!--   entry validation.                                                       -->
<!-- String: No further fields are needed or used.                             -->
<!-- StringMenu: Fourth field = number of menu items                           -->
<!--             Remaining fields are the actual menu items - the number of    -->
<!--             items provided must equal the specified number of menu items. -->
<!--                                                                           -->
<!-- The style sheet must also define the variable itself, named according to  -->
<!-- the definition.  The value within the 'select' field will be displayed in -->
<!-- the Survey Controller as the default value for the item.                  -->
<xsl:variable name="userField1" select="'outputUnits|Distance units for DC file|StringMenu|3|Metres|International Feet|US Survey Feet'"/>
<xsl:variable name="outputUnits" select="'Metres'"/>

<xsl:variable name="DistUnit">
  <xsl:choose>
    <xsl:when test="$outputUnits = 'International Feet'">InternationalFeet</xsl:when>
    <xsl:when test="$outputUnits = 'US Survey Feet'">USSurveyFeet</xsl:when>
    <xsl:otherwise>Metres</xsl:otherwise>
  </xsl:choose>
</xsl:variable>  
<xsl:variable name="AngleUnit"  select="'DMSDegrees'" />
<xsl:variable name="CoordOrder" select="'North-East-Elevation'" />
<xsl:variable name="TempUnit"   select="'Celsius'" />
<xsl:variable name="PressUnit"  select="'MilliBar'" />

<!-- Setup conversion factor for coordinate and distance values -->
<!-- Dist/coord values in JobXML file are always in metres -->
<xsl:variable name="DistConvFactor">
  <xsl:choose>
    <xsl:when test="$DistUnit='Metres'">1.0</xsl:when>
    <xsl:when test="$DistUnit='InternationalFeet'">3.280839895    </xsl:when>
    <xsl:when test="$DistUnit='USSurveyFeet'">3.2808333333357</xsl:when>
    <xsl:otherwise>1.0</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<!-- Setup conversion factor for angular values -->
<!-- Angular values in JobXML file are always in decimal degrees -->
<xsl:variable name="AngleConvFactor">
  <xsl:choose>
    <xsl:when test="$AngleUnit='DMSDegrees'">1.0</xsl:when>
    <xsl:when test="$AngleUnit='Gons'">1.111111111111</xsl:when>
    <xsl:when test="$AngleUnit='Mils'">17.77777777777</xsl:when>
    <xsl:otherwise>1.0</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<!-- Setup boolean variable for coordinate order -->
<xsl:variable name="NECoords">
  <xsl:choose>
    <xsl:when test="$CoordOrder='North-East-Elevation'">
      <xsl:value-of select="'True'"/>
    </xsl:when>
    <xsl:when test="$CoordOrder='X-Y-Z'">
      <xsl:value-of select="'True'"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="'False'"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<!-- Setup conversion factor for pressure values -->
<!-- Pressure values in JobXML file are always in millibars (hPa) -->
<xsl:variable name="PressConvFactor">
  <xsl:choose>
    <xsl:when test="$PressUnit='MilliBar'">1.0</xsl:when>
    <xsl:when test="$PressUnit='InchHg'">0.029529921</xsl:when>
    <xsl:when test="$PressUnit='mmHg'">0.75006</xsl:when>
    <xsl:otherwise>1.0</xsl:otherwise>
  </xsl:choose>
</xsl:variable>


<!-- **************************************************************** -->
<!-- ************************** Main Loop *************************** -->
<!-- **************************************************************** -->
<xsl:template match="/" >
  <!-- Select the TrimbleRoad node to process -->
  <xsl:apply-templates select="TrimbleRoad" />
</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** TrimbleRoad Node Processing ****************** -->
<!-- **************************************************************** -->
<xsl:template match="TrimbleRoad">

  <xsl:call-template name="headerRecordOutput"/>
  <xsl:call-template name="jobRecordOutput"/>
  <xsl:call-template name="roadNameRecordOutput"/>
  
  <!-- Output the horizontal alignment records -->
  <xsl:apply-templates select="HorizontalAlignment"/>

  <!-- Output the Vertical alignment records -->
  <xsl:apply-templates select="VerticalAlignment"/>

  <!-- Output the Template positioning records -->
  <xsl:apply-templates select="TemplatePositioning"/>

  <!-- Output the superelevation and widening records -->
  <xsl:apply-templates select="SuperelevationAndWidening"/>

  <!-- Output the Template records -->
  <xsl:apply-templates select="TemplateRecord"/>

</xsl:template>


<!-- **************************************************************** -->
<!-- ******************** DC Header Record Output ******************* -->
<!-- **************************************************************** -->
<xsl:template name="headerRecordOutput">
  <xsl:value-of select="'00NMSC V10-70       0000'"/>

  <xsl:variable name="dateForFile" select="'2006-10-17T10:12:00'"/>
  
  <xsl:call-template name="formattedDateString">
    <xsl:with-param name="date" select="$dateForFile"/>
  </xsl:call-template>
  <xsl:value-of select="concat(' ', substring($dateForFile, 12, 5), ' ')"/>
  <!-- Output the angle units flag: 1 = Degrees, 2 = Gons, 3 = Mils, 4 = Quadrant bearings (stored as degrees in DC file) -->
  <xsl:choose>
    <xsl:when test="$AngleUnit='DMSDegrees'">1</xsl:when>
    <xsl:when test="$AngleUnit='Gons'">2</xsl:when>
    <xsl:when test="$AngleUnit='Mils'">3</xsl:when>
    <xsl:otherwise>4</xsl:otherwise>
  </xsl:choose>

  <!-- Output the distance units flag: 1 = Metres, 2 = International Feet, 3 = US Survey Feet -->
  <xsl:choose>
    <xsl:when test="$DistUnit='Metres'">1</xsl:when>
    <xsl:when test="$DistUnit='InternationalFeet'">2</xsl:when>
    <xsl:when test="$DistUnit='USSurveyFeet'">3</xsl:when>
    <xsl:otherwise>1</xsl:otherwise>
  </xsl:choose>

  <!-- Output the pressure units flag: 1 = mmHg, 2 = inch Hg, 3 = Millibars (hPa) -->
  <xsl:choose>
    <xsl:when test="$PressUnit='MilliBar'">3</xsl:when>
    <xsl:when test="$PressUnit='InchHg'">2</xsl:when>
    <xsl:when test="$PressUnit='mmHg'">1</xsl:when>
    <xsl:otherwise>3</xsl:otherwise>
  </xsl:choose>

  <!-- Output the temperature units flag: 1 = Celsius, 2 = Fahrenheit -->
  <xsl:choose>
    <xsl:when test="$TempUnit='Celsius'">1</xsl:when>
    <xsl:when test="$TempUnit='Fahrenheit'">2</xsl:when>
    <xsl:otherwise>1</xsl:otherwise>
  </xsl:choose>

  <!-- Output the coordinate order flag: 1 = Nth-East-Elev, 2 = East-Nth-Elev, 3 = Y-X-Z (German notation Y = East, X = North) -->
  <xsl:choose>
    <xsl:when test="$CoordOrder='North-East-Elevation'">1</xsl:when>
    <xsl:when test="$CoordOrder='East-North-Elevation'">2</xsl:when>
    <xsl:when test="$CoordOrder='Y-X-Z'">3</xsl:when>
    <xsl:when test="$CoordOrder='X-Y-Z'">1</xsl:when>  <!-- Same as Nth-East-Elev -->
    <xsl:otherwise>1</xsl:otherwise>
  </xsl:choose>

  <xsl:value-of select="'1'"/>  <!-- Always Angles Right -->
  
  <xsl:call-template name="NewLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ********************* DC Job Record Output ********************* -->
<!-- **************************************************************** -->
<xsl:template name="jobRecordOutput">
  <xsl:value-of select="'10NM'"/>
  <xsl:call-template name="padRight">
    <xsl:with-param name="stringWidth" select="16"/>
    <xsl:with-param name="theString" select="/TrimbleRoad/Name"/>
  </xsl:call-template>

  <xsl:value-of select="'121221'"/>  <!-- Flags for: Point name size, Include elevation, Atmos correction, C & R Correction, Refraction constant, Sea level reductions -->
  <xsl:call-template name="NewLine"/>
</xsl:template>

  
<!-- **************************************************************** -->
<!-- ****************** DC Road Name Record Output ****************** -->
<!-- **************************************************************** -->
<xsl:template name="roadNameRecordOutput">
  <xsl:value-of select="'28NM'"/>   <!-- Road Name record identifier -->
  <xsl:call-template name="padRight">
    <xsl:with-param name="stringWidth" select="16"/>
    <xsl:with-param name="theString" select="/TrimbleRoad/Name"/>
  </xsl:call-template>

  <xsl:value-of select="'1'"/>  <!-- Flag for: Point name size (ID type) -->
  <xsl:call-template name="NewLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- **************************************************************** -->
<!-- **************************************************************** -->
<!-- ************** Horizontal Alignment Records Output ************* -->
<!-- **************************************************************** -->
<!-- **************************************************************** -->
<!-- **************************************************************** -->
<xsl:template match="HorizontalAlignment">

  <xsl:value-of select="'84NM'"/>   <!-- Horizontal alignment record identifier -->
  <!-- Output the start station -->
  <xsl:call-template name="numericFieldString">
    <xsl:with-param name="value" select="StartStation"/>
  </xsl:call-template>
  
  <!-- Output the start azimuth - get it from first child record with a StartAzimuth element -->
  <xsl:call-template name="numericFieldString">
    <xsl:with-param name="value" select="child::*[StartAzimuth][1]/StartAzimuth"/>
    <xsl:with-param name="conv
