<?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">


<!-- **************************************************************** -->
<!-- Karlsruhe                                                                                                                 -->
<!--                                                                                                                                  -->
<!-- Version     : 1.00                                                                                                      -->
<!-- Date         : November  2006                                                                                  -->
<!-- Author      : C. Breuer                                                                                              -->
<!--                                                                                                                                   -->
<!-- **************************************************************** -->


<xsl:output method="text" omit-xml-declaration="yes" encoding="utf-8"/>

<xsl:decimal-format name="Standard" 
                    decimal-separator="."
                    grouping-separator=","
                    infinity="Infinity"
                    minus-sign="-"
                    NaN="NaN"
                    percent="%"
                    per-mille="&#2030;"
                    zero-digit="0" 
                    digit="#" 
                    pattern-separator=";" />

<xsl:variable name="FormatDistanz" select="'0.000'"/>
<xsl:variable name="DefaultText" select="''"/>

<xsl:variable name="fileExt" select="'txt'"/>

<!-- **************************************************************** -->
<!-- ************ Define search keys to speed up searchs ************ -->
<!-- **************************************************************** -->
<xsl:key name="StationID-search" match="//JOBFile/FieldBook/StationRecord" use="@ID"/>
<xsl:key name="TargetID-search" match="//JOBFile/FieldBook/TargetRecord" use="@ID"/>
<xsl:key name="AtmosphereID-search" match="//JOBFile/FieldBook/AtmosphereRecord" use="@ID"/>

<!-- **************************************************************** -->
<!-- Units in JOB-File                                                -->
<!--                                                                  -->
<!-- Coordinates  : Meter                                             -->
<!-- Distances    : Meter                                             -->
<!-- Angles       : Gons        >>>   Conversion Factor               -->
<!-- Temperature  : Celsius                                           -->
<!-- Pressure     : hPas                                              -->
<!-- **************************************************************** -->
<xsl:variable name="AngleConvFactor" select="1.111111111111"/>


<!-- **************************************************************** -->
<!-- ************************** Main Loop *************************** -->
<!-- **************************************************************** -->
<xsl:template match="/">
<xsl:call-template name="PadLeft">
	  <xsl:with-param name="Text" select="'Alet/Ref Y'"/>
	  <xsl:with-param name="Width" select="31"/>
	</xsl:call-template>
	
	<xsl:call-template name="PadLeft">
	  <xsl:with-param name="Text" select="'YA'"/>
	  <xsl:with-param name="Width" select="10"/>
	</xsl:call-template>
	
	<xsl:call-template name="PadLeft">
	  <xsl:with-param name="Text" select="'DA'"/>
	  <xsl:with-param name="Width" select="10"/>
	</xsl:call-template>

    <xsl:call-template name="PadLeft">
	  <xsl:with-param name="Text" select="'EM'"/>
	  <xsl:with-param name="Width" select="10"/>
	</xsl:call-template>
    	
	<xsl:call-template name="NewLine"/>	
<xsl:apply-templates select="JOBFile/FieldBook"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** FieldBook Node Processing ******************** -->
<!-- **************************************************************** -->
<xsl:template match="FieldBook">

  <xsl:variable name="outputData">  <!-- Add all the output data to the variable $outputData so that it can be further processed later -->
    <xsl:for-each select="*">

      <xsl:if test="name(current()) = 'PointRecord'">
        <xsl:apply-templates select="current()"/>
      </xsl:if>     

    </xsl:for-each>
  </xsl:variable>

  <!-- Now process the $outputData variable to create the output file -->
  <xsl:for-each select="msxsl:node-set($outputData)/*">
    <xsl:choose>
        <xsl:when test="name(current()) = 'Point'">

        <!-- Observation Output -->
        <!-- ****************** -->
		
	<xsl:variable name="STIDTemp" select="STID"/>
	<xsl:if test="(position() = 1) or ($STIDTemp != preceding-sibling::*[1]/STID)">
	
	  <xsl:value-of select="'--------------------------------------------------------------'"/>
	  <xsl:call-template name="NewLine"/>
        
      <xsl:call-template name="PadRight">
	    <xsl:with-param name="Text" select="'Durulan'"/>
	    <xsl:with-param name="Width" select="15"/>
	  </xsl:call-template>	  
	
	  <xsl:call-template name="PadRight">
	    <xsl:with-param name="Text" select="Station"/>
	    <xsl:with-param name="Width" select="10"/>
	  </xsl:call-template>
	  
	  <xsl:call-template name="PadLeft">
	    <xsl:with-param name="Text" select="format-number(InstrumentH,'#0.000')"/>
	    <xsl:with-param name="Width" select="6"/>
	  </xsl:call-template>
	  
	  <xsl:value-of select="ResectionPointID"/>

	  <xsl:call-template name="NewLine"/>
	  
	</xsl:if>
	
	<xsl:choose>
	  <xsl:when test="SightType = 'BackSight'">
	    <xsl:call-template name="PadRight">
	      <xsl:with-param name="Text" select="'Geri'"/>
	      <xsl:with-param name="Width" select="15"/>
	    </xsl:call-template>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:call-template name="PadRight">
	      <xsl:with-param name="Text" select="'İleri'"/>
	      <xsl:with-param name="Width" select="15"/>
	    </xsl:call-template>
	  </xsl:otherwise>
	</xsl:choose>
	
	
	<xsl:call-template name="PadRight">
	  <xsl:with-param name="Text" select="ObsPoint"/>
	  <xsl:with-param name="Width" select="10"/>
	</xsl:call-template>
	
	<xsl:call-template name="PadLeft">
	  <xsl:with-param name="Text" select="format-number(TargetH,'#0.000')"/>
	  <xsl:with-param name="Width" select="6"/>
	</xsl:call-template>
	
	<xsl:call-template name="PadLeft">
	  <xsl:with-param name="Text" select="format-number(Hz * $AngleConvFactor,'#0.0000')"/>
	  <xsl:with-param name="Width" select="10"/>
	</xsl:call-template>
	
	<xsl:call-template name="PadLeft">
	  <xsl:with-param name="Text" select="format-number(V * $AngleConvFactor,'#0.0000')"/>
	  <xsl:with-param name="Width" select="10"/>
	</xsl:call-template>
	
	<xsl:choose>
	  <xsl:when test="PPM != ''">	
	    <xsl:call-template name="PadLeft">
	      <xsl:with-param name="Text" select="format-number(Dist + PrismC + (PPM * Dist * 0.000001),'#0.000')"/>
	      <xsl:with-param name="Width" select="10"/>
	    </xsl:call-template>
      </xsl:when>
	  <xsl:otherwise>
	    <xsl:call-template name="PadLeft">
	      <xsl:with-param name="Text" select="format-number(Dist + PrismC,'#0.000')"/>
	      <xsl:with-param name="Width" select="10"/>
	    </xsl:call-template>
	  </xsl:otherwise>
	</xsl:choose>

	
	<xsl:if test="SightType != 'BackSight'">
	  <xsl:call-template name="PadLeft">
	    <xsl:with-param name="Text" select="format-number(Rechtswert,'#0.000')"/>
	    <xsl:with-param name="Width" select="13"/>
	  </xsl:call-template>
	  <xsl:call-template name="PadLeft">
	    <xsl:with-param name="Text" select="format-number(Hochwert,'#0.000')"/>
	    <xsl:with-param name="Width" select="13"/>
	  </xsl:call-template>
	  <xsl:call-template name="PadLeft">
	    <xsl:with-param name="Text" select="format-number(Hoehe,'#0.000')"/>
	    <xsl:with-param name="Width" select="10"/>
	  </xsl:call-template>
	</xsl:if>
	
	<xsl:call-template name="NewLine"/>
        
      </xsl:when>
    </xsl:choose>
  </xsl:for-each>
  
</xsl:template>

<!-- **************************************************************** -->
<!-- ******************** PointRecord Output ************************ -->
<!-- **************************************************************** -->
<xsl:template match="PointRecord">
  <xsl:if test="Deleted = 'false'">
    <xsl:call-template name="Observation"/>
  </xsl:if>
</xsl:template>

<!-- **************************************************************** -->
<!-- **************** Observation Details Output ******************** -->
<!-- **************************************************************** -->
<xsl:template name="Observation">

  <xsl:choose>
  
    <xsl:when test="Circle">
    
      <xsl:element name="Point">
      
        <xsl:variable name="FindStation">
	  <xsl:for-each select="key('StationID-search', StationID)[1]">
	    <xsl:value-of select="StationName"/>
	  </xsl:for-each>
        </xsl:variable>
		
		<xsl:element name="Station">
		  <xsl:value-of select="$FindStation"/>
		</xsl:element>
		
		<xsl:element name="STID">
		  <xsl:value-of select="StationID"/>
		</xsl:element>
        
        <xsl:element name="InstrumentH">
	  <xsl:for-each select="key('StationID-search', StationID)[1]">
	    <xsl:value-of select="TheodoliteHeight"/>
	  </xsl:for-each>
        </xsl:element>
        
        <xsl:variable name="AtmoID">
          <xsl:for-each select="key('StationID-search', StationID)[1]">
	    <xsl:value-of select="AtmosphereID"/>
	  </xsl:for-each>
	</xsl:variable>
	
	    <xsl:element name="SightType">
		  <xsl:value-of select="Classification"/>
		</xsl:element>
        
        <xsl:element name="PPM">
	  <xsl:for-each select="key('AtmosphereID-search', $AtmoID)[1]">
	    <xsl:value-of select="PPM"/>
	  </xsl:for-each>
        </xsl:element>
    
        <xsl:element name="ObsPoint">
          <xsl:value-of select="Name"/>
        </xsl:element>
        
        <xsl:element name="Hz">
          <xsl:value-of select="Circle/HorizontalCircle"/>
        </xsl:element>
       
        <xsl:element name="V">
          <xsl:value-of select="Circle/VerticalCircle"/>
        </xsl:element>
       
        <xsl:element name="Dist">
          <xsl:value-of select="Circle/EDMDistance"/>
        </xsl:element>
		
		<xsl:element name="Rechtswert">
		  <xsl:value-of select="ComputedGrid/East"/>
		</xsl:element>
		
		<xsl:element name="Hochwert">
		  <xsl:value-of select="ComputedGrid/North"/>
		</xsl:element>
		
		<xsl:element name="Hoehe">
		  <xsl:value-of select="ComputedGrid/Elevation"/>
		</xsl:element>
        
        <xsl:element name="FeatureCode">
          <xsl:value-of select="Code"/>
        </xsl:element>
        
        <xsl:element name="TargetH">
          <xsl:for-each select="key('TargetID-search', TargetID)[1]">
	    <xsl:value-of select="TargetHeight"/>
	  </xsl:for-each>
	</xsl:element>
	        
        <xsl:element name="PrismC">
	  <xsl:for-each select="key('TargetID-search', TargetID)[1]">
	    <xsl:value-of select="PrismConstant"/>
	  </xsl:for-each>
	</xsl:element>
	
	<xsl:element name="ResectionPointID">
	  <xsl:for-each select="JOBFile/FieldBook/PointRecord[(Name = $FindStation) and (Method = 'Resection') and (Deleted = 'false')]">
        <xsl:value-of select="Name"/>
      </xsl:for-each>
	</xsl:element>
                
      </xsl:element>
      
    </xsl:when>
       
  </xsl:choose>
      
</xsl:template>

<!-- **************************************************************** -->
<!-- ********************** New Line Output ************************* -->
<!-- **************************************************************** -->
<xsl:template name="NewLine">

	<xsl:text>&#10;</xsl:text>

</xsl:template>


<!-- **************************************************************** -->
<!-- *********** Pad a string to the left with spaces *************** -->
<!-- **************************************************************** -->
<xsl:template name="PadLeft">

  <xsl:param name="Text"/>
  <xsl:param name="Width"/>
  
	<xsl:variable name="KonvText">
		<xsl:choose>
	    <xsl:when test="$Text = 'NaN'">
	      <xsl:value-of select="$DefaultText"/>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$Text"/>
	    </xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:choose>
    <xsl:when test="$Width = 0">
      <xsl:value-of select="normalize-space($KonvText)"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="TempText" select="concat('                                       ', $KonvText)"/>
      <xsl:value-of select="substring($TempText, string-length($TempText) - $Width + 1)"/>
    </xsl:otherwise>
  </xsl:choose>

</xsl:template>


<!-- **************************************************************** -->
<!-- *********** Pad a string to the right with spaces ************** -->
<!-- **************************************************************** -->
<xsl:template name="PadRight"> 

  <xsl:param name="Text"/>
  <xsl:param name="Width"/>

	<xsl:variable name="KonvText">
		<xsl:choose>
	    <xsl:when test="$Text = 'NaN'">
	      <xsl:value-of select="$DefaultText"/>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$Text"/>
	    </xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
  
	<xsl:choose>
    <xsl:when test="$Width = 0">
      <xsl:value-of select="normalize-space($KonvText)"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="TempText" select="concat($KonvText, '                                       ')"/>
      <xsl:value-of select="substring($TempText, 1, $Width)"/>
    </xsl:otherwise>
  </xsl:choose>

</xsl:template>

</xsl:stylesheet>
<!-- Stylus Studio meta-information - (c) 2004-2005. Progress Software Corporation. All rights reserved.
<metaInformation>
<scenarios ><scenario default="yes" name="Scenario1" userelativepaths="yes" externalpreview="no" url="..\Test allnav.xml" htmlbaseurl="" outputurl="" processortype="internal" profilemode="0" profiledepth="" profilelength="" urlprofilexml="" commandline="" additionalpath="" additionalclasspath="" postprocessortype="none" postprocesscommandline="" postprocessadditionalpath="" postprocessgeneratedext=""/></scenarios><MapperMetaTag><MapperInfo srcSchemaPathIsRelative="yes" srcSchemaInterpretAsXML="no" destSchemaPath="" destSchemaRoot="" destSchemaPathIsRelative="yes" destSchemaInterpretAsXML="no"/><MapperBlockPosition><template match="/"></template><template name="Observation"></template><template match="StationRecord"></template></MapperBlockPosition></MapperMetaTag>
</metaInformation>
-->