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

<!--
   Name: 14_gmd:dataQualityInfo.xsl 
   Author:  Matthew J. McCready
   Date:  20110601
   Description:  XSLT stylesheet that transforms Section 2 of an FGDC format file into the gmd:DQ_DataQuality section of  an ISO file. This stylesheet is called by 
   the csdgm2iso19115-2_master.xslt stylesheet. This template calls on the dateTimeTemp template, located in this stylesheet, and the DataQualitySource template, 
   located in the DataQualitySource.xsl stylesheet. The DataQualitySource template handles all the gmd:lineage information.
   Modification History:
      Initial   Date      Change Request ID   Description
      MMC		07/07/2011            Added the gmd:DQ_Scope, md:level and gmd:MD_ScopeCode elements to comply with the ISO standard.
      MMC               07/07/2011            Added the gmd:DQ_CompletenessCommission to one of the gmd:report elements
      MMC		07/08/2011            Created the dateTimeTemp template to work with dates and times for the gmd:dateTime element. This 
                                               element has to be in the MMDDYYYYT00:00:00 format, even when oly a year is given in the 
                                               /metadata/dataqual/lineage/procstep/procdate FGDC element. To correctly format this ISO element, a default -01-01 for 
                                               the month is inserted and all 0, for the time when only a year is given in the FGDC XML. 
      MMC               07/08/2011            Inserted the CI_Date package under the gmd:source/CI_Citation section. Previously, the gco:date element was not in this
				               package.
      MMC               07/13/2011            Modified the dateTimeTemp template to include the actual time for the gmd:dateTime ISO element and not the default 
				               01T00:00:00 for transforming FGDC files that have the Process Time element. The correct ISO format was inserted for the                                                                                            Process Date elements that do not include the month or day.
      MMC               11/1/2011             Changed the gco:Date element to the gco:DateTime element in the dateTimeTemp template to better adhere to the standard. 
      MMC               03/1/2012             Insered a gmd:report/gmd:DQ_QuantitativeAttributeAccuracy for the FGDC Attribute Accuraccy Report element.  Also included a gmd:nameOfMeasure
                                               element for every gmd:report but gmd:DQ_CompletenessCommission and gmd:DQ_CompletenessOmmission. This was done to provide a description for 
                                               the test being reported.
      MMC               05/11/2012            Inserted a choose structure for the gmd:value of the DQ_AbsoluteExternalPositionalAccuracy/ DQ_QuantitativeResult package. Now, FGDC files that do 
                                               not have Horizontal Positional Accuraccy Values (horizpav) will have a gco:nilReason attribute inserted with a value of "missing" inserted for the
                                                gmd:value element. 
				       
-->

<!-- 
List of Related Templates and associated stylesheets
Template                                     Stylesheet/File
DataQualitySource                            DataQualitySource.xsl
-->

<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" 
xmlns:xlink="http://www.w3.org/1999/xlink"
>
<xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes"/>



<xsl:template name="dataQualityInfo" match="/">
<xsl:element name="gmd:dataQualityInfo">
<xsl:element name="gmd:DQ_DataQuality">

<xsl:element name="gmd:scope">
<xsl:element name="gmd:DQ_Scope">

<xsl:element name="gmd:level">
<xsl:element name="gmd:MD_ScopeCode">
<xsl:attribute name="codeList">http://www.isotc211.org/2005/#MD_ScopeCode</xsl:attribute>
<xsl:attribute name="codeListValue">dataset</xsl:attribute>
<xsl:attribute name="codeSpace">005</xsl:attribute>
dataset
</xsl:element><!-- gmd:MD_ScopeCode -->
</xsl:element><!-- gmd:level -->

</xsl:element><!-- gmd:DQ_Scope -->
</xsl:element><!-- gmd:scope -->


<!-- Attribute Accuraccy Report -->
<xsl:if test ="/metadata/dataqual/attracc/attraccr">
<xsl:element name="gmd:report">
<xsl:element name="gmd:DQ_QuantitativeAttributeAccuracy">

<xsl:element name="gmd:nameOfMeasure">
<xsl:element name="gco:CharacterString">Attribute Accuracy</xsl:element> <!-- gco:CharacterString -->
</xsl:element> <!-- gmd:nameOfMeasure -->

<xsl:element name="gmd:measureDescription">
<xsl:element name="gco:CharacterString"><xsl:value-of select="/metadata/dataqual/attracc/attraccr"/> </xsl:element> <!-- gco:CharacterString -->
</xsl:element><!-- gmd:measureDescription -->
 
<xsl:element name="gmd:result">
<xsl:attribute name="gco:nilReason">unknown</xsl:attribute>
</xsl:element><!-- gmd:result -->



</xsl:element><!--gmd:DQ_QuantitativeAttributeAccuracy -->
</xsl:element><!-- gmd:report -->
</xsl:if>


<!-- horizontal Positional Accuraccy Report -->
<xsl:if test="/metadata/dataqual/posacc/horizpa"> 
<xsl:element name="gmd:report">
<xsl:element name="gmd:DQ_AbsoluteExternalPositionalAccuracy">

<xsl:element name="gmd:nameOfMeasure">
<xsl:element name="gco:CharacterString">Horizontal Positional Accuracy</xsl:element> <!-- gco:CharacterString -->
</xsl:element> <!-- gmd:nameOfMeasure -->

<xsl:element name="gmd:measureDescription">
<xsl:element name="gco:CharacterString"><xsl:value-of select="/metadata/dataqual/posacc/horizpa/qhorizpa/horizpae"/> </xsl:element>
</xsl:element><!-- gmd:measureDescription -->

<xsl:element name="gmd:evaluationMethodDescription">
<xsl:element name="gco:CharacterString"><xsl:value-of select="/metadata/dataqual/posacc/horizpa/horizpar"/> </xsl:element> <!-- gco:CharacterString -->
</xsl:element> <!-- gmd:evaluationMethodDescription -->

<xsl:element name="gmd:result">
<xsl:element name="gmd:DQ_QuantitativeResult">

<xsl:element name="gmd:valueUnit">

<xsl:element name="gml:BaseUnit"> 

<xsl:attribute name="gml:id">meters</xsl:attribute>
<xsl:element name="gml:identifier">
<xsl:attribute name="codeSpace">meters </xsl:attribute> 
meters</xsl:element>

<xsl:element name="gml:unitsSystem"> 
<xsl:attribute name="xlink:href">http://www.bipm.org/en/si/ </xsl:attribute> 
</xsl:element>   <!-- gml:unitsSystem -->
 
</xsl:element>  <!-- gml:BaseUnit -->
</xsl:element>  <!-- gmd:valueUnit -->

<xsl:choose>

<xsl:when test="/metadata/dataqual/posacc/horizpa/qhorizpa/horizpav">
<xsl:for-each select="/metadata/dataqual/posacc/horizpa/qhorizpa/horizpav">
<xsl:element name="gmd:value">
<xsl:element name="gco:Record"><xsl:value-of select="."/> </xsl:element>  <!-- gco:Record -->
</xsl:element>  <!-- gmd:value -->
</xsl:for-each>

</xsl:when>

<xsl:otherwise>
<xsl:element name="gmd:value">
<xsl:attribute name="gco:nilReason">missing</xsl:attribute>
</xsl:element>  <!-- gmd:value -->
</xsl:otherwise>
</xsl:choose>


</xsl:element><!-- gmd:DQ_QuantitativeResult -->
</xsl:element><!-- gmd:result -->

</xsl:element><!-- gmd:DQ_AbsoluteExternalPositionalAccuracy -->

</xsl:element><!-- gmd:report -->
</xsl:if><!-- /metadata/dataqual/posacc/horizpa -->

<!-- Notification of excess data presenet in the dataset beyond the extent defined in Scope -->
<xsl:element name="gmd:report">
<xsl:element name="gmd:DQ_CompletenessCommission">
<xsl:element name="gmd:result">
<xsl:attribute name="gco:nilReason">unknown</xsl:attribute>
</xsl:element><!-- gmd:result -->
</xsl:element><!-- gmd:DQ_CompletenessCommission -->
</xsl:element><!-- gmd:report -->


<!-- Notification of data absent from the dataset as defined by Scope -->
<xsl:if test="/metadata/dataqual/complete">
<xsl:element name="gmd:report">
<xsl:element name="gmd:DQ_CompletenessOmission">

<xsl:element name="gmd:evaluationMethodDescription">
<xsl:element name="gco:CharacterString"><xsl:value-of select="/metadata/dataqual/complete"/></xsl:element> <!-- gco:CharacterString -->
</xsl:element><!-- gmd:evaluationMethodDescription -->

<xsl:element name="gmd:result">
<xsl:attribute name="gco:nilReason">unknown</xsl:attribute>
</xsl:element><!-- gmd:result -->


</xsl:element><!-- gmd:DQ_CompletenessOmission -->
</xsl:element><!-- gmd:report -->
</xsl:if><!-- /metadata/dataqual/complete -->

<!-- Logical Consistency Report -->
<xsl:if test="/metadata/dataqual/logic">
<xsl:element name="gmd:report">
<xsl:element name="gmd:DQ_ConceptualConsistency">

<xsl:element name="gmd:nameOfMeasure">
<xsl:element name="gco:CharacterString">Logical Consistency Report</xsl:element> <!-- gco:CharacterString -->
</xsl:element> <!-- gmd:nameOfMeasure -->

<xsl:element name="gmd:measureDescription">
<xsl:element name="gco:CharacterString"><xsl:value-of select="/metadata/dataqual/logic"/></xsl:element> <!-- gco:CharacterString -->
</xsl:element><!-- gmd:measureDescription -->

<xsl:element name="gmd:result">
<xsl:attribute name="gco:nilReason">unknown</xsl:attribute>
</xsl:element><!-- gmd:result -->

</xsl:element><!-- gmd:DQ_ConceptualConsistency -->

</xsl:element><!-- gmd:report -->


</xsl:if><!-- /metadata/dataqual[1]/logic[1] -->
<!-- <xsl:comment> This is February !!!!!!!!!!!!!!!!!!</xsl:comment> -->
<xsl:element name="gmd:lineage">
<xsl:element name="gmd:LI_Lineage">
<xsl:element name="gmd:processStep">
<xsl:element name="gmd:LI_ProcessStep">

<xsl:element name="gmd:description">
<xsl:element name="gco:CharacterString"><xsl:value-of select="/metadata/dataqual/lineage/procstep/procdesc"/></xsl:element> <!-- gco:CharacterString -->
</xsl:element><!-- gmd:description -->

<xsl:element name="gmd:dateTime">
<!-- <xsl:comment> calling the "dateTimeTemp template for the first time</xsl:comment> -->
<xsl:call-template name="dateTimeTemp"/>

<!-- <xsl:element name="gco:DateTime"><xsl:value-of select="/metadata/dataqual/lineage/procstep/procdate"/>  </xsl:element>--><!-- gco:DateTime -->
</xsl:element><!-- gmd:dateTime -->

<xsl:element name="gmd:source">
<!-- <xsl:comment> This is February !!!!!!!!!!!!!!!!!!</xsl:comment> -->
<xsl:element name="gmd:LI_Source">
<xsl:element name="gmd:sourceCitation">
<xsl:element name="gmd:CI_Citation">
<!-- <xsl:comment> In the Citation Package </xsl:comment> -->
<xsl:element name="gmd:title">
<xsl:element name="gco:CharacterString"><xsl:value-of select="/metadata/dataqual/lineage/procstep/srcused"/></xsl:element> <!-- gco:CharacterString -->
</xsl:element><!-- gmd:title -->

<xsl:element name="gmd:date">
<xsl:element name="gmd:CI_Date">
<xsl:element name="gmd:date">
<!-- <xsl:comment> IN THE gmd:date section </xsl:comment> -->
<xsl:element name="gco:Date"><xsl:value-of select="/metadata/dataqual/lineage/procstep/procdate"/> </xsl:element> <!-- gco:Date -->
</xsl:element><!-- gmd:date -->
<!-- <xsl:comment> calling the "dateTimeTemp template for the second time</xsl:comment> -->
<!-- <xsl:call-template name="dateTimeTemp"/> -->
<!-- </xsl:element> --><!-- gmd:CI_Date -->

<!-- <xsl:comment> This is February !!!!!!!!!!!!!!!!!!</xsl:comment> -->
<xsl:element name="gmd:dateType">
<xsl:element name="gmd:CI_DateTypeCode">
<xsl:attribute name="codeList">http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_DateTypeCode</xsl:attribute>
<xsl:attribute name="codeListValue">revision</xsl:attribute>
<xsl:attribute name="codeSpace">003</xsl:attribute>
revision
</xsl:element> <!-- gmd:CI_DateTypeCode -->
</xsl:element><!-- gmd:CI_Date -->
</xsl:element><!-- gmd:date -->

<!-- </xsl:element> --><!-- gmd:CI_Date -->
</xsl:element><!-- gmd:date -->

</xsl:element><!-- gmd:CI_Citation -->
</xsl:element><!-- gmd:sourceCitation -->
</xsl:element><!-- gmd:LI_Source -->
</xsl:element><!-- gmd:source -->

</xsl:element><!-- gmd:LI_ProcessStep -->
</xsl:element><!-- gmd:processStep -->
<!-- <xsl:comment> This is February !!!!!!!!!!!!!!!!!!</xsl:comment> -->
<!-- <xsl:comment> calling the DataQualitySource template !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</xsl:comment> -->
<xsl:call-template name="DataQualitySource"/>
<!-- <xsl:comment>Done with the DataQualitySource template !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</xsl:comment> -->

</xsl:element><!-- gmd:LI_Lineage -->
</xsl:element><!-- gmd:lineage -->

</xsl:element><!-- gmd:DQ_DataQuality -->
</xsl:element><!-- gmd:dataQualityInfo -->

</xsl:template>

<xsl:template name="dateTimeTemp">
<!-- <xsl:comment> This is February !!!!!!!!!!!!!!!!!!</xsl:comment> -->
<xsl:variable name ="ProcDate" select= "/metadata/dataqual/lineage/procstep/procdate"/>
<xsl:variable name ="ProcTime" select= "/metadata/dataqual/lineage/procstep/proctime"/>
<xsl:variable name ="ProcTimeLength" select= "string-length(/metadata/dataqual/lineage/procstep/proctime)"/>
<xsl:variable name="year"  select ="substring($ProcDate,1,4)"/>
<xsl:variable name="month" select ="substring($ProcDate,5,2)"/>
<xsl:variable name="monthLength" select="string-length($month)"/>
<xsl:variable name="day"   select ="substring($ProcDate,7,2)"/>
<xsl:variable name="dayLength" select="string-length($day)"/>

<xsl:choose>
<!-- <xsl:comment> This is February !!!!!!!!!!!!!!!!!!</xsl:comment> -->
<xsl:when test="$ProcTimeLength=0"> 
<!-- <xsl:comment>Here 1!!!!!!!!!!!!!!!</xsl:comment> -->
<xsl:choose>
<xsl:when test="$dayLength>0">
<!-- <xsl:comment>Here 2!!!!!!!!!!!!!!!</xsl:comment> -->
<xsl:variable name="dateFormat" select="concat($year, '-',$month,'-',$day ,'-','T00:00:00')"/>
<xsl:element name="gco:Date">
	<xsl:value-of select ="$dateFormat"/>
</xsl:element>
</xsl:when>

<xsl:when test="$monthLength >0">
<!-- <xsl:comment> This is February !!!!!!!!!!!!!!!!!!</xsl:comment> -->
<!-- <xsl:comment>Here 3!!!!!!!!!!!!!!!</xsl:comment> -->
<xsl:variable name="dateFormat" select="concat($year, '-',$month,'-','01T00:00:00')"/>
<xsl:element name="gco:DateTime">
	<xsl:value-of select ="$dateFormat"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<!-- <xsl:comment> Last Resort</xsl:comment> -->
<!-- <xsl:comment>Here 4!!!!!!!!!!!!!!!</xsl:comment> -->
<!-- YYYY-MM-DDThh:mm:ss -->
<xsl:variable name="dateFormatA" select="concat($year,'-','01','-','01T00:00:00')"/>

<xsl:element name="gco:DateTime">
<!--     <xsl:comment> I hope this is correct! it is!!!!!!!</xsl:comment> -->
	<xsl:value-of select ="$dateFormatA"/>
</xsl:element>
<!-- <xsl:comment> End of Last Resort</xsl:comment> -->
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:when test="$ProcTimeLength>0">
<!-- <xsl:comment>Here 5!!!!!!!!!!!!!!!</xsl:comment> -->
<!-- Thh:mm:ss -->
<xsl:variable name="Hour" select= "substring($ProcTime,1,2)"/>
<xsl:variable name="Min" select= "substring($ProcTime,3,2)"/>
<xsl:variable name="Sec" select= "substring($ProcTime,5,2)"/>
<xsl:variable name="dateFormatA" select="concat($year, '-',$month,'-',$day,'T',$Hour,':',$Min,':',$Sec )"/>
<xsl:element name="gco:DateTime">
        <!-- <xsl:comment>Here 6!!!!!!!!!!!!!!!</xsl:comment>
     <xsl:comment> I hope this is correct! (TIME) it is!!!!!!!</xsl:comment> -->
	<xsl:value-of select ="$dateFormatA"/>
</xsl:element>
</xsl:when>
</xsl:choose> 
</xsl:template>
</xsl:stylesheet>
