<?xml version="1.0" encoding="UTF-8" ?>

<!--
   Name:  DateFormat.xsl 
   Author:  Matthew J. McCready
   Date:  20110608
   Description: This stylesheet consists of two templates: the BegDateTemp template and the EndDateTemp template. The BegDateTemp formats the date stored in the         metadata/idinfo/timeperd/timeinfo/rngdates/begdate FGDC element and inserts in the gml:beginPosition ISO element. The EndDateTemp template formats the value stored  in the metadata/idinfo/timeperd/timeinfo/rngdates/enddate FGDC element and inserts in the gml:endPosition ISO element.
   Modification History:
      Initial   Date         Change Request ID   Description
      MMC       07/15/2011                       The gml:beginPosition was changed to gml:endPosition for the '$dayLength > 0' condition under the EndDateTemp template
                                                  This was done to comply with the ISO standard.
-->

<xsl:stylesheet version = "1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:gmd="http://www.isotc211.org/2005/gmd"
   xmlns:gco="http://www.isotc211.org/2005/gco" 
   xmlns:gml="http://www.opengis.net/gml/3.2"
>
<xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes"/>
<xsl:variable name ="MetDate"   select= "//metadata/metainfo/metd"/>
<!-- <xsl:variable name="SDTSPntVot" select="//metadata/spdoinfo/ptvctinf/sdtsterm/sdtstype"/> -->



<xsl:template name="BegDateTemp">
  <!-- <xsl:comment>In the Template BegDateTemp </xsl:comment> -->
<xsl:variable name ="begDate" select="//metadata/idinfo/timeperd/timeinfo/rngdates/begdate"/>
<xsl:variable name="year"  select ="substring($begDate,1,4)"/>
<xsl:variable name="month" select ="substring($begDate,5,2)"/>
<xsl:variable name="monthLength" select="string-length($month)"/>
<xsl:variable name="day"   select ="substring($begDate,7,2)"/>
<xsl:variable name="dayLength" select="string-length($day)"/>
<!-- <xsl:comment><xsl:value-of select="$begDate"/> </xsl:comment> -->
<!-- <xsl:comment><xsl:value-of select="$dayLength"/> </xsl:comment> -->
<xsl:choose>

<xsl:when test="$dayLength > 0">
<!-- <xsl:comment>In the day </xsl:comment> -->
<xsl:variable name="BegdateFormat" select="concat($year, '-',$month,'-',$day)"/>
<xsl:element name="gml:beginPosition"><xsl:value-of select="$BegdateFormat"/></xsl:element>
</xsl:when>

<xsl:when test="$monthLength >0">
 <!-- <xsl:comment>In the month for the beginning date </xsl:comment> -->
<xsl:variable name="BegdateFormat" select="concat($year, '-',$month)"/>
 <xsl:element name="gml:beginPosition"><xsl:value-of select="$BegdateFormat"/></xsl:element> 
<!-- <xsl:comment>In the Month Section!!!!!!!!!!!!!!!!</xsl:comment> -->
</xsl:when>

<xsl:otherwise>
<!-- <xsl:comment>In the otherwise </xsl:comment> -->
<xsl:variable name="BegdateFormat" select="$year"/>
<xsl:element name="gml:beginPosition"><xsl:value-of select="$BegdateFormat"/></xsl:element>
</xsl:otherwise>
  


</xsl:choose>
  <!-- <xsl:comment>Exiting the Template BegDateTemp </xsl:comment> -->
</xsl:template>



<xsl:template name="EndDateTemp">
<xsl:variable name ="endDate" select="//metadata/idinfo/timeperd/timeinfo/rngdates/enddate"/>
<xsl:variable name="Endyear"  select ="substring($endDate,1,4)"/>
<xsl:variable name="Endmonth" select ="substring($endDate,5,2)"/>
<xsl:variable name="EndmonthLength" select="string-length($Endmonth)"/>
<xsl:variable name="Endday"   select ="substring($endDate,7,2)"/>
<xsl:variable name="EnddayLength" select="string-length($Endday)"/>
<!-- <xsl:comment><xsl:value-of select="$begDate"/> </xsl:comment> -->
<!-- <xsl:comment><xsl:value-of select="$dayLength"/> </xsl:comment> -->
<xsl:choose>

<xsl:when test="$EnddayLength > 0">
<!-- <xsl:comment>In the day </xsl:comment> -->
<xsl:variable name="EnddateFormat" select="concat($Endyear, '-',$Endmonth,'-',$Endday)"/>
<xsl:element name="gml:endPosition"><xsl:value-of select="$EnddateFormat"/></xsl:element>
</xsl:when>

<xsl:when test="$EndmonthLength >0">
<!-- <xsl:comment>In the month for the end data </xsl:comment> -->
<xsl:variable name="EnddateFormat" select="concat($Endyear, '-',$Endmonth)"/>
<xsl:element name="gml:endPosition"><xsl:value-of select="$EnddateFormat"/></xsl:element>
<!-- <xsl:variable name="BegdateFormat" select="concat($year, '-',$month")/> -->
</xsl:when>

<xsl:otherwise>
<!-- <xsl:comment>In the otherwise </xsl:comment> -->
<xsl:variable name="EnddateFormat" select="$Endyear"/>
<xsl:element name="gml:endPosition"><xsl:value-of select="$EnddateFormat"/></xsl:element>
</xsl:otherwise>



</xsl:choose>
</xsl:template>

</xsl:stylesheet>
