﻿<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="SameCourse" match="Course" use="CourseVariation/CourseVariationId"/>  <!-- viide radade kirjeldusele, et leida samal rajal jooksvad klassid-->
    <xsl:key name="EventType" match="Event" use="@eventForm"/>   <!--viide radade kirjeldusele, et leida samal rajal jooksvad klassid-->
 	
 	<xsl:output method="text"/>
	<xsl:template match="/">
      		<xsl:choose>
   				<xsl:when test = 'key("EventType", "IndSingleDay")'>
   					<xsl:call-template name="FileHeader"/>
   					<xsl:apply-templates select = "ResultList/ClassResult" mode="SingleDay"/>
   				</xsl:when>
				<xsl:when test = 'key("EventType", "IndMultiDay")'>
   					<xsl:for-each select="ResultList/Event/EventRace">
						<xsl:call-template name="FileHeader"/>
	   					<xsl:apply-templates select = "../../ClassResult" mode="MultiDay">
      						<xsl:with-param name="DayNo" select='position()'/>
      					</xsl:apply-templates>
		   				<xsl:text>!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!&#13;</xsl:text>
		   			</xsl:for-each>
				</xsl:when>
		   		<xsl:otherwise>
		   			siia<xsl:call-template name="FileHeader"/>
		   		 	<xsl:apply-templates select = "ResultList/ClassResult" mode="SingleDay"/>
		   		</xsl:otherwise>
		   	</xsl:choose>	
      
    </xsl:template> 
      
<!-- *****************************************************************-->
<xsl:template name='FileHeader'>
<xsl:text>Stno,SI card,Database Id,Name,YB,Block,nc,Start,Finish,Time,Classifier,Club no.,Cl.name,City,Nat,Cl. no.,Short,Long,Num1,Num2,Num3,Text1,Text2,Text3,Adr. name,Street,Line2,Zip,City,Phone,Fax,EMail,Id/Club,Rented,Start fee,Paid,Course no.,Course,km,m,Course controls,Pl,Start punch,Finish punch,Control1,Punch1,Control2,Punch2,Control3,Punch3,Control4,Punch4,Control5,Punch5,Control6,Punch6,Control7,Punch7,Control8,Punch8,Control9,Punch9,Control10,Punch10&#13;</xsl:text>
</xsl:template> <!-- Faili paise trykkimine -->      
<!-- *****************************************************************-->
<xsl:template match="ClassResult" mode="SingleDay">
	<xsl:apply-templates select = "PersonResult" mode="SingleDay"/>
</xsl:template>
<!-- *****************************************************************-->

<xsl:template match="ClassResult" mode="MultiDay">
<xsl:param name="DayNo"/>
	<xsl:apply-templates select = "PersonResult" mode="MultiDay">
		<xsl:with-param name="DayNo" select='$DayNo'/>
	</xsl:apply-templates>
</xsl:template>
<!-- *****************************************************************-->
<xsl:template match="PersonResult" mode="SingleDay">
  <xsl:if test="Result/CompetitorStatus/@value = 'OK' and Result/CCardId != 0" >
   	<xsl:value-of select='concat(normalize-space(Result/StartNumber),",")'/><!-- stardinumber? -->
	<xsl:value-of select='concat(normalize-space(Result/CCardId),",")'/><!-- SI/EMIT'i number -->
   	<xsl:value-of select='concat(" ",",")'/><!-- ei tea?? -->
   	<xsl:value-of select='concat(normalize-space(Person/PersonName/Given)," ",normalize-space(Person/PersonName/Family),",")'/>
   	<xsl:value-of select='concat("0",",")'/><!-- synniaasta? -->
   	<xsl:value-of select='concat("0",",")'/><!-- ?? -->
   	<xsl:value-of select='concat("0",",")'/><!-- ?? -->
   	<xsl:call-template name="get-xsd-datetime-hour">
		<xsl:with-param name="xsd-date-time" 
						select="normalize-space(Result/StartTime/Clock)"/>
	</xsl:call-template><xsl:text>,</xsl:text><!-- stardiaeg -->
   	<xsl:value-of select="normalize-space(Result/FinishTime/Clock)"/><xsl:text>,</xsl:text><!-- lõpetamise aeg -->
   	<xsl:value-of select="normalize-space(Result/Time)"/><xsl:text>,</xsl:text><!-- jooksuaeg -->
   	<xsl:value-of select='concat("0",",")'/><!-- ?? -->
   	<xsl:value-of select='concat("0",",")'/><!-- klubi nr? -->
   	<xsl:value-of select="normalize-space(Club/ShortName)"/><xsl:text>,</xsl:text><!-- Klubi lühinimetus -->
   	<xsl:text>,</xsl:text><!-- linn -->
   	<xsl:value-of select="normalize-space(Club/CountryId/@value)"/><xsl:text>,</xsl:text><!-- riigi tunnus -->
   	<xsl:value-of select="normalize-space(../ClassShortName)"/><xsl:text>,</xsl:text><!-- klassi number -->
   	<xsl:value-of select="normalize-space(../ClassShortName)"/><xsl:text>,</xsl:text><!-- klassi lühinimetus -->
   	<xsl:value-of select="normalize-space(../ClassShortName)"/><xsl:text>,</xsl:text><!-- klassi täisnimetus -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- number 1 -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- number 2 -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- number 3 -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- tekst 1 -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- tekst 2 -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- tekst 3 -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- Addr.name? -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- Street -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- Line 2 -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- zip, postiindeks -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- Linn -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- telefoni nr -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- Faks -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- e-posti aadress -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- Id/club -->
   	 <xsl:value-of select='concat("0",",")'/><!-- Rented -->
   	 <xsl:value-of select='concat("0",",")'/><!-- Start fee -->
   	 <xsl:value-of select='concat("0",",")'/><!-- Makstud -->
   	 <xsl:value-of select='concat(normalize-space(Result/CourseVariationId),",")'/><!-- Raja variant -->
   	 <xsl:call-template name="SamalRajal">
		<xsl:with-param name="RajaId" select="normalize-space(Result/CourseVariationId)"/>
	 </xsl:call-template><xsl:text>,</xsl:text><!-- samal rajal jooksnud klassid -->
   	 <xsl:call-template name="RajaPikkus">
		<xsl:with-param name="RajaId" select="normalize-space(Result/CourseVariationId)"/>
	 </xsl:call-template><xsl:text>,</xsl:text><!-- raja pikkus -->
   	 <xsl:call-template name="TousuSumma">
		<xsl:with-param name="RajaId" select="normalize-space(Result/CourseVariationId)"/>
	 </xsl:call-template><xsl:text>,</xsl:text><!-- tousu kogusummma -->
   	 <xsl:call-template name="KPArv">
		<xsl:with-param name="RajaId" select="normalize-space(Result/CourseVariationId)"/>
	 </xsl:call-template><xsl:text>,</xsl:text><!-- KP arv rajal, tuleks votta rajakirjeldusest -->
   	 <xsl:value-of select='concat("0",",")'/><!-- koht antud jooksus? -->
   	 <xsl:call-template name="get-xsd-datetime-hour">
		<xsl:with-param name="xsd-date-time" 
						select="normalize-space(Result/StartTime/Clock)"/>
	</xsl:call-template><xsl:text>,</xsl:text><!-- stardiaeg --> 
   	<xsl:value-of select="normalize-space(Result/Time)"/>
   	<xsl:for-each select="Result/SplitTime">
			<xsl:if test="@status != 'NIL'">  <!-- Liigsed punktid (NIL) jäävad failist välja, MP puhul vaid KP number-->
   			<xsl:choose>
   				<xsl:when test = 'number(substring-before(normalize-space(Time), ":")) &lt; 0'>
   					<xsl:value-of select='concat(",",normalize-space(ControlCode),","," ")'/>
   				</xsl:when>
   				<xsl:when test = 'normalize-space(Time) = "00:00"'>
   					<xsl:value-of select='concat(",",normalize-space(ControlCode),","," ")'/>
   				</xsl:when>
		   		<xsl:when 
		   			test='number(substring-before(normalize-space(Time), ":")) &lt;number(substring-before(normalize-space(../Time), ":"))'>
						<xsl:value-of select='concat(",",normalize-space(ControlCode),",",normalize-space(Time))'/>
		   		</xsl:when>
		   	  	<xsl:when 
		   			test='number(substring-before(normalize-space(Time), ":")) = number(substring-before(normalize-space(../Time), ":"))'>
						<xsl:value-of select='concat(",",normalize-space(ControlCode),",",normalize-space(Time))'/>
		   		</xsl:when>
		   		<xsl:otherwise>
		   		 	<xsl:value-of select='concat(",",normalize-space(ControlCode),","," ")'/>
		   		</xsl:otherwise>
		   	</xsl:choose>
			</xsl:if>  <!-- staatus = OK lõpp -->	
    </xsl:for-each>  	 
	<xsl:text>&#13;</xsl:text>
  </xsl:if>
 </xsl:template><!-- match="PersonResult" mode="SingleDay" -->

<!-- *****************************************************************-->

<xsl:template match="PersonResult" mode="MultiDay">
<xsl:param name="DayNo"/>
  <xsl:if test="RaceResult[$DayNo]/Result/CompetitorStatus/@value = 'OK'  and RaceResult[$DayNo]/Result/CCardId != 0">
   	<xsl:value-of select='concat(normalize-space(Result/StartNumber),",")'/><!-- stardinumber? -->
	<xsl:value-of select='concat(normalize-space(RaceResult[$DayNo]/Result/CCardId),",")'/><!-- SI/EMIT'i number -->
   	<xsl:value-of select='concat(" ",",")'/><!-- ei tea?? -->
   	<xsl:value-of select='concat(normalize-space(Person/PersonName/Given)," ",normalize-space(Person/PersonName/Family),",")'/>
   	<xsl:value-of select='concat("0",",")'/><!-- synniaasta? -->
   	<xsl:value-of select='concat("0",",")'/><!-- ?? -->
   	<xsl:value-of select='concat("0",",")'/><!-- ?? -->
   	<xsl:call-template name="get-xsd-datetime-hour">
		<xsl:with-param name="xsd-date-time" 
						select="normalize-space(RaceResult[$DayNo]/Result/StartTime/Clock)"/>
	</xsl:call-template><xsl:text>,</xsl:text><!-- stardiaeg -->
   	<xsl:value-of select="normalize-space(RaceResult[$DayNo]/Result/FinisTime/Clock)"/><xsl:text>,</xsl:text><!-- lõpetamise aeg -->
   	<xsl:value-of select="normalize-space(RaceResult[$DayNo]/Result/Time)"/><xsl:text>,</xsl:text><!-- jooksuaeg -->
   	<xsl:value-of select='concat("0",",")'/><!-- ?? -->
   	<xsl:value-of select='concat("0",",")'/><!-- klubi nr? -->
   	<xsl:value-of select="normalize-space(Club/ShortName)"/><xsl:text>,</xsl:text><!-- Klubi lühinimetus -->
   	<xsl:text>,</xsl:text><!-- linn -->
   	<xsl:value-of select="normalize-space(Club/CountryId/@value)"/><xsl:text>,</xsl:text><!-- riigi tunnus -->
   	<xsl:value-of select="normalize-space(../ClassShortName)"/><xsl:text>,</xsl:text><!-- klassi number -->
   	<xsl:value-of select="normalize-space(../ClassShortName)"/><xsl:text>,</xsl:text><!-- klassi lühinimetus -->
   	<xsl:value-of select="normalize-space(../ClassShortName)"/><xsl:text>,</xsl:text><!-- klassi täisnimetus -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- number 1 -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- number 2 -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- number 3 -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- tekst 1 -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- tekst 2 -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- tekst 3 -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- Addr.name? -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- Street -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- Line 2 -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- zip, postiindeks -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- Linn -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- telefoni nr -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- Faks -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- e-posti aadress -->
   	 <xsl:value-of select='concat(" ",",")'/><!-- Id/club -->
   	 <xsl:value-of select='concat("0",",")'/><!-- Rented -->
   	 <xsl:value-of select='concat("0",",")'/><!-- Start fee -->
   	 <xsl:value-of select='concat("0",",")'/><!-- Makstud -->
   	 <xsl:value-of select='concat(normalize-space(RaceResult[$DayNo]/Result/CourseVariationId),",")'/><!-- Raja variant -->
   	 <xsl:call-template name="SamalRajal">
		<xsl:with-param name="RajaId" select="normalize-space(RaceResult[$DayNo]/Result/CourseVariationId)"/>
	 </xsl:call-template><xsl:text>,</xsl:text><!-- samal rajal jooksnud klassid -->
   	 <xsl:call-template name="RajaPikkus">
		<xsl:with-param name="RajaId" select="normalize-space(RaceResult[$DayNo]/Result/CourseVariationId)"/>
	 </xsl:call-template><xsl:text>,</xsl:text><!-- raja pikkus -->
   	 <xsl:value-of select='concat("0",",")'/><!-- tõusu kogusummma, vist -->
   	 <xsl:call-template name="KPArv">
		<xsl:with-param name="RajaId" select="normalize-space(RaceResult[$DayNo]/Result/CourseVariationId)"/>
	 </xsl:call-template><xsl:text>,</xsl:text><!-- KP arv rajal, tuleks võtta rajakirjeldusest -->
   	 <xsl:value-of select='concat("0",",")'/><!-- koht antud jooksus? -->
   	 <xsl:call-template name="get-xsd-datetime-hour">
		<xsl:with-param name="xsd-date-time" 
						select="normalize-space(RaceResult[$DayNo]/Result/StartTime/Clock)"/>
	</xsl:call-template><xsl:text>,</xsl:text><!-- stardiaeg --> 
   	<xsl:value-of select="normalize-space(RaceResult[$DayNo]/Result/Time)"/>
   	<xsl:for-each select="RaceResult[$DayNo]/Result/SplitTime">
   			<xsl:if test="@status != 'NIL'">  <!-- Liigsed punktid (NIL) jäävad failist välja, MP puhul vaid KP number-->
   			<xsl:choose>
   				<xsl:when test = 'number(substring-before(normalize-space(Time), ":")) &lt; 0'>
   					<xsl:value-of select='concat(",",normalize-space(ControlCode),","," ")'/>
   				</xsl:when>
   				<xsl:when test = 'normalize-space(Time) = "00:00"'>
   					<xsl:value-of select='concat(",",normalize-space(ControlCode),","," ")'/>
   				</xsl:when>
		   		<xsl:when 
		   			test='number(substring-before(normalize-space(Time), ":")) &lt;number(substring-before(normalize-space(../Time), ":"))'>
						<xsl:value-of select='concat(",",normalize-space(ControlCode),",",normalize-space(Time))'/>
		   		</xsl:when>
		   	  	<xsl:when 
		   			test='number(substring-before(normalize-space(Time), ":")) = number(substring-before(normalize-space(../Time), ":"))'>
						<xsl:value-of select='concat(",",normalize-space(ControlCode),",",normalize-space(Time))'/>
		   		</xsl:when>
		   		<xsl:otherwise>
		   		 	<xsl:value-of select='concat(",",normalize-space(ControlCode),","," ")'/>
		   		</xsl:otherwise>
		   	</xsl:choose>	
			</xsl:if>  <!-- staatus = OK lõpp -->
    </xsl:for-each>  	 
	<xsl:text>&#13;</xsl:text>
  </xsl:if>
 </xsl:template><!-- match="PersonResult" mode="MultiDay" -->

<!-- *****************************************************************-->
<xsl:template name="SamalRajal">
<xsl:param name="RajaId"/>
<xsl:for-each select="key('SameCourse', $RajaId)">
         <!--<xsl:value-of select="CourseName"/>-->
         <xsl:for-each select = "ClassShortName">
         	<xsl:value-of select="normalize-space(.)"/>
         </xsl:for-each>
</xsl:for-each>
</xsl:template><!-- name="SamalRajal"-->
<!--************************************************************************* -->
<xsl:template name="KPArv">
<xsl:param name="RajaId"/>
<xsl:value-of select="count(key('SameCourse', $RajaId)/CourseVariation/CourseControl)"/>
</xsl:template><!-- name="KPArv"-->
<!--************************************************************************* -->
<xsl:template name="RajaPikkus">
<xsl:param name="RajaId"/>
<xsl:value-of select='substring-before(normalize-space(key("SameCourse", $RajaId)/CourseVariation/CourseLength), ",")'/>
</xsl:template><!-- name="RajaPikkus"-->
<!--************************************************************************* -->
<xsl:template name="TousuSumma">
<xsl:param name="RajaId"/>
<xsl:value-of select='substring-before(normalize-space(key("SameCourse", $RajaId)/CourseVariation/CourseClimb), ",")'/>
</xsl:template><!-- name="TousuSumma"-->
<!--************************************************************************* -->

<xsl:template name='get-xsd-datetime-hour'>
    <xsl:param name='xsd-date-time'/>

    <xsl:choose>
      <xsl:when test='contains($xsd-date-time, "T")'>
        <xsl:call-template name='get-xsd-datetime-hour'>
          <xsl:with-param name='xsd-date-time' select='substring-after($xsd-date-time, "T")'/>
        </xsl:call-template>
      </xsl:when>

      <!-- Check for time -->
      <xsl:when test='substring($xsd-date-time, 3, 1) = ":"'>
        <!--<xsl:value-of select='substring($xsd-date-time, 1, 2)'/>-->
        <xsl:call-template name='get-xsd-datetime-minute'>
          <xsl:with-param name='xsd-date-time' select='$xsd-date-time'/>
          <xsl:with-param name='H_Time' select='substring($xsd-date-time, 1, 2)'/>
        </xsl:call-template>
      </xsl:when>

      <xsl:otherwise>
        <!-- This is a date -->
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template> <!-- name='get-xsd-datetime-hour'-->

<!--************************************************************************* -->

  <xsl:template name='get-xsd-datetime-minute'>
    <xsl:param name='xsd-date-time'/>
    <xsl:param name='H_Time'/>
    <xsl:choose>
      <xsl:when test='contains($xsd-date-time, "T")'>
        <xsl:call-template name='get-xsd-datetime-minute'>
          <xsl:with-param name='xsd-date-time' select='substring-after($xsd-date-time, "T")'/>
        </xsl:call-template>
      </xsl:when>

      <!-- Check for time -->
      <xsl:when test='substring($xsd-date-time, 3, 1) = ":"'>
        <xsl:value-of 
        	select='concat((number(substring($xsd-date-time, 4, 2))+number($H_Time)*60), ":")'/>
        <xsl:call-template name='get-xsd-datetime-second'>
          <xsl:with-param name='xsd-date-time' select='$xsd-date-time'/>
        </xsl:call-template>
      </xsl:when>

      <xsl:otherwise>
        <!-- This is a date -->
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
<!--************************************************************************* -->
<xsl:template name='get-xsd-datetime-second'>
    <xsl:param name='xsd-date-time'/>

    <xsl:choose>
      <xsl:when test='contains($xsd-date-time, "T")'>
        <xsl:call-template name='get-xsd-datetime-second'>
          <xsl:with-param name='xsd-date-time' select='substring-after($xsd-date-time, "T")'/>
        </xsl:call-template>
      </xsl:when>

      <!-- Check for time -->
      <xsl:when test='substring($xsd-date-time, 3, 1) = ":"'>
        <xsl:variable name='part' select='substring($xsd-date-time, 7)'/>
        <xsl:choose>
          <xsl:when test='contains($part, "Z")'>
            <xsl:value-of select='substring-before($part, "Z")'/>
          </xsl:when>
          <xsl:when test='contains($part, "+")'>
            <xsl:value-of select='substring-before($part, "+")'/>
          </xsl:when>
          <xsl:when test='contains($part, "-")'>
            <xsl:value-of select='substring-before($part, "-")'/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select='$part'/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>

      <xsl:otherwise>
        <!-- This is a date -->
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
<!--************************************************************************* -->
</xsl:stylesheet>