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

<!--
   Name:  12_gmd:contentInfo.xsl
   Author:  Matthew J. McCready
   Date:  20110601
   Description:  XSLT stylesheet that provides the link to the feature catolog, which contains the entity and attribute information.
   Modification History:
      Initial   Date      Change Request ID   Description
      MMC          07/07/2011                 Changed the contentInfo to gmd:contentInfo
      MMC          07/07/2011                 Created the featureCatalogueCitationDate template to handle the date under the CI_Citation package
                                               changed CI_Citation gmd:CI_Citation.
      MMC          07/07/2011                 Added the gco:CharacterString under the gmd:title element.
      MMC          05/31/2012                 Added the FC_CitedResponsibleParty template. This template contains the URL link for the EA file.
                                               This section is basically CI_ResponsibleParty package. 
      MMC          03/22/2013                 Corrected the url in the ContentInfo section to reflect the new metadata server    
      MMC          05/20/2013                 Corrected the URL of the ContentInfo section. The EA file will have the same URL as the main metadata file. The only difference
                                               will be the filename.
-->


<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:xlink="http://www.w3.org/1999/xlink"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:fn="http://www.w3.org/2005/xpath-functions"
        xmlns:grp="http://www.altova.com/Mapforce/grouping"
        exclude-result-prefixes="fn grp xs xsi xsl">
        <xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes"/>



        <xsl:template name="contentInfo" match="/">

                <xsl:element name="gmd:contentInfo">
                        <xsl:element name="gmd:MD_FeatureCatalogueDescription">
                                 <!-- <xsl:comment> calling the includedWithDataset Template </xsl:comment> -->
                                <xsl:element name="gmd:includedWithDataset">

                                        <xsl:call-template name="includedWithDatasetNew"/>
                                </xsl:element>
                                <!-- gmd:includedWithDataset -->

                                <!-- <xsl:comment> calling the FeatureType Template </xsl:comment>
                                <xsl:comment> Now calling the FeatureType template </xsl:comment> -->
                                <xsl:text>
</xsl:text>
                                <xsl:call-template name="FeatureType"/>



                                <xsl:element name="gmd:featureCatalogueCitation">
                                        <xsl:element name="gmd:CI_Citation">

                                                <!-- <xsl:comment> calling the ContentTitle Template </xsl:comment> -->
                                                <xsl:call-template name="ContentTitle"/>
                                                <xsl:element name="gmd:date">
                                                  <xsl:element name="gmd:CI_Date">
                                                  <xsl:element name="gmd:date">

                                                  <xsl:call-template
                                                  name="featureCatalogueCitationDate"/>
                                                  </xsl:element>
                                                  <!-- gmd:date -->


                                                  <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">publication</xsl:attribute>
                                                  <xsl:attribute name="codeSpace"
                                                  >002</xsl:attribute>
                                                  </xsl:element>
                                                  <!-- gmd:CI_DateTypeCode -->

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

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

                                                <!-- <xsl:comment>Calling the Responsible Party
                                                  Template</xsl:comment> -->
                                                <xsl:text>
</xsl:text>
                                                <xsl:call-template name="FC_CitedResponsibleParty"/>
                                                <xsl:variable name="FullTitle"
                                                  select="/metadata/idinfo/citation/citeinfo/title"/>
                                                <!-- <xsl:comment> <xsl:value-of select="$FullTitle"/></xsl:comment> -->
                                                <xsl:choose>


                                                  <xsl:when
                                                  test="contains($FullTitle,'Series Information File')">
                                                  <xsl:variable name="postFor"
                                                  select="substring-after($FullTitle,'for the')"/>
                                                  <xsl:comment>
                                                  <xsl:value-of select="$postFor"/>
                                                  </xsl:comment>

                                                  </xsl:when>


                                                  <xsl:when
                                                  test="metadata/idinfo[1]/citation[1]/citeinfo[1]/onlink[1]">

                                                  <!-- <xsl:comment>The file has a weblink!!!!!!!!!!!!!!</xsl:comment> -->
                                                  <xsl:element name="gmd:otherCitationDetails">
                                                  <xsl:element name="gco:CharacterString">

                                                  <!-- <xsl:variable name="webSite"
                                                  >http://web8.geo.census.gov:7777/metaFiles/existing/decennial/GEO/GPMB/TIGERline/</xsl:variable>-->
                                                 <xsl:choose>
                                                         <xsl:when test="contains($FullTitle,'Census')">
                                                          <xsl:variable name="onlink"
                                                  select="/metadata/idinfo[1]/citation[1]/citeinfo[1]/onlink[1]"/>
                                                 <!-- <xsl:variable name="onlink2"
                                                 select="substring-before($onlink,'tl_')"/> -->
                                                          <xsl:variable name="onlink2">http://meta.geo.census.gov/data/existing/decennial/GEO/GPMB/TIGERline/TIGER2013/</xsl:variable>
                                                  <xsl:variable name="postTiger"
                                                  select="substring-after($onlink,'tiger')"/>
                                                  <xsl:variable name="postTiger2"
                                                  select="substring-after($postTiger,'/')"/>
                                                  <xsl:variable name="postTiger3"
                                                  select="substring-after($postTiger2,'/')"/>
                                                  <xsl:variable name="filename"
                                                  select="substring-after($postTiger3,'/')"/>
                                                  <xsl:variable name="postDash1"
                                                  select="substring-after($filename,'_')"/>
                                                  <xsl:variable name="year"
                                                  select="substring-before($postDash1,'_')"/>
                                                  <xsl:variable name="postDash2"
                                                  select="substring-after($postDash1,'_')"/>
                                                  <xsl:variable name="postDash3"
                                                  select="substring-after($postDash2,'_')"/>
                                                  <xsl:variable name="theme"
                                                  select="substring-before($postDash3,'.')"/>

                                                      
                                                  <xsl:variable name="path"
                                                  select="concat($onlink2, $theme, '/', $year,'_',$theme,'.ea.iso.xml')"/>


                                                  <xsl:value-of select="$path"/>
                                                         </xsl:when>
                                                         <xsl:otherwise>
                                                                 <xsl:comment>Put location of EA Catalog here!!!!!!!!!!!!!!</xsl:comment>
                                                         </xsl:otherwise>
                                                 </xsl:choose>
                                                  </xsl:element>
                                                  </xsl:element>
                                                  </xsl:when>




                                                </xsl:choose>
                                        </xsl:element>
                                        <!-- CI_Citation -->
                                </xsl:element>
                                <!-- gmd:featureCatalogueCitation -->


                        </xsl:element>
                        <!-- gmd:MD_FeatureCatalogueDescription -->
                </xsl:element>
                <!--  contentInfo -->

        </xsl:template>

       <xsl:template name="includedWithDatasetNew">
                <!-- <xsl:comment>In the includedWithDataset template</xsl:comment>  -->
                <xsl:variable name="SeriesCheck" select="/metadata/idinfo/citation/citeinfo/title"/>
                <xsl:variable name="LabelLength"
                        select="string-length(/metadata/eainfo[1]/detailed[1]/enttyp[1]/enttypl[1])"/>
                <!-- <xsl:comment>SeriesCheck:<xsl:value-of select="$SeriesCheck"/></xsl:comment>
                 <xsl:comment>LabelLength <xsl:value-of select="$LabelLength"/></xsl:comment> -->
                <xsl:choose>
                        
                        <xsl:when test="/metadata/eainfo[1]/detailed[1]">
                                <xsl:element name="gco:Boolean">true</xsl:element>
                        </xsl:when>

                        <xsl:when test="$LabelLength > 0">
                                <xsl:element name="gco:Boolean">true</xsl:element>
                        </xsl:when>
                        
                        <xsl:when test="contains($SeriesCheck,'Series Information File')">
                                <xsl:element name="gco:Boolean">true</xsl:element>
                        </xsl:when>

                        <xsl:otherwise>
                                <xsl:element name="gco:Boolean">false</xsl:element>
                        </xsl:otherwise>
                </xsl:choose>
        </xsl:template> 

        <xsl:template name="featureCatalogueCitationDate">
                <xsl:variable name="MetDate" select="//metadata/metainfo/metd"/>
                <xsl:variable name="year" select="substring($MetDate,1,4)"/>
                <xsl:variable name="month" select="substring($MetDate,5,2)"/>
                <xsl:variable name="monthLength" select="string-length($month)"/>
                <xsl:variable name="day" select="substring($MetDate,7,2)"/>
                <xsl:variable name="dayLength" select="string-length($day)"/>

                <xsl:choose>
                        <xsl:when test="$dayLength>0">

                                <xsl:variable name="dateFormat"
                                        select="concat($year, '-',$month,'-',$day)"/>
                                <xsl:element name="gco:Date">
                                        <xsl:value-of select="$dateFormat"/>
                                </xsl:element>
                        </xsl:when>

                        <xsl:when test="$monthLength >0">
                                <xsl:variable name="dateFormat" select="concat($year, '-',$month)"/>
                                <xsl:element name="gco:Date">
                                        <xsl:value-of select="$dateFormat"/>
                                </xsl:element>
                        </xsl:when>
                        <xsl:otherwise>
                                <xsl:variable name="dateFormat" select="$year"/>
                                <xsl:element name="gco:Date">
                                        <xsl:value-of select="$dateFormat"/>
                                </xsl:element>

                        </xsl:otherwise>
                </xsl:choose>

        </xsl:template>



        <xsl:template name="FC_CitedResponsibleParty">
                <!-- <xsl:comment>in FC Responsible Party</xsl:comment> -->
                <xsl:variable name="FullOrg" select="/metadata/idinfo/citation/citeinfo/origin"/>
                <xsl:variable name="postComma1" select="substring-after($FullOrg,',')"/>
                <xsl:variable name="division" select="substring-after($postComma1,',')"/>
                <xsl:variable name="finalDivision" select="substring-before($division,' Division')"/>
                <xsl:variable name="finalDivision2" select="substring-before($finalDivision,'g')"/>

                <!-- <xsl:comment> The division is:<xsl:value-of select="$finalDivision"/></xsl:comment>
                <xsl:comment> The new division:<xsl:value-of select="$finalDivision2"/>:The
                End</xsl:comment> -->

                <xsl:choose>
                        <xsl:when test="$finalDivision =' Geography '">

                                <xsl:attribute name="xlink:href"
                                        >https://www.ngdc.noaa.gov/docucomp/186104a8-278b-412d-9b61-d4d488142f3c </xsl:attribute>
                                <xsl:attribute name="xlink:title">pointofContact - U.S. Department
                                        of Commerce, U.S. Census Bureau, Geography
                                        Division</xsl:attribute>
                        </xsl:when>

                        <xsl:when test="$finalDivision2=' Geo' ">
                                <xsl:element name="gmd:citedResponsibleParty">
                                        <xsl:attribute name="xlink:href"
                                                >https://www.ngdc.noaa.gov/docucomp/186104a8-278b-412d-9b61-d4d488142f3c </xsl:attribute>
                                        <xsl:attribute name="xlink:title">pointofContact - U.S.
                                                Department of Commerce, U.S. Census Bureau,
                                                Geography Division</xsl:attribute>
                                </xsl:element>
                        </xsl:when>
                        <xsl:otherwise>


                                <xsl:element name="gmd:citedResponsibleParty">
                                        <xsl:element name="gmd:CI_ResponsibleParty">

                                                <xsl:element name="gmd:organisationName">
                                                  <xsl:element name="gco:CharacterString">
                                                  <xsl:value-of
                                                  select="/metadata/idinfo/citation/citeinfo/origin"
                                                  />
                                                  </xsl:element>
                                                </xsl:element>


                                                <xsl:element name="gmd:contactInfo">
                                                  <xsl:element name="gmd:CI_Contact">

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

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

                                                  <xsl:element name="gmd:voice">
                                                  <xsl:element name="gco:CharacterString">
                                                  <xsl:value-of
                                                  select="/metadata/idinfo/ptcontac/cntinfo/cntvoice"
                                                  />
                                                  </xsl:element>
                                                  </xsl:element>
                                                  <!-- gmd:voice -->

                                                  <xsl:element name="gmd:facsimile">
                                                  <xsl:element name="gco:CharacterString">
                                                  <xsl:value-of
                                                  select="/metadata/idinfo/ptcontac/cntinfo/cntfax"
                                                  />
                                                  </xsl:element>
                                                  </xsl:element>
                                                  <!-- gmd:facsimile -->


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

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

                                                  <xsl:element name="gmd:address">
                                                  <xsl:element name="gmd:CI_Address">

                                                  <xsl:element name="gmd:deliveryPoint">
                                                  <xsl:element name="gco:CharacterString">
                                                  <xsl:value-of
                                                  select="/metadata/idinfo/ptcontac/cntinfo/cntaddr/address"
                                                  />
                                                  </xsl:element>
                                                  </xsl:element>
                                                  <!-- gmd:deliveryPoint -->

                                                  <xsl:element name="gmd:city">
                                                  <xsl:element name="gco:CharacterString">
                                                  <xsl:value-of
                                                  select="/metadata/idinfo/ptcontac/cntinfo/cntaddr/city"
                                                  />
                                                  </xsl:element>
                                                  </xsl:element>
                                                  <!-- gmd:city -->

                                                  <xsl:element name="gmd:administrativeArea">
                                                  <xsl:element name="gco:CharacterString">
                                                  <xsl:value-of
                                                  select="/metadata/idinfo/ptcontac/cntinfo/cntaddr/state"
                                                  />
                                                  </xsl:element>
                                                  </xsl:element>
                                                  <!-- gmd:administrativeArea -->

                                                  <xsl:element name="gmd:postalCode">
                                                  <xsl:element name="gco:CharacterString">
                                                  <xsl:value-of
                                                  select="/metadata/idinfo/ptcontac/cntinfo/cntaddr/postal"
                                                  />
                                                  </xsl:element>
                                                  </xsl:element>
                                                  <!-- gmd:postalCode -->

                                                  <xsl:element name="gmd:country">
                                                  <xsl:element name="gco:CharacterString">
                                                  <xsl:value-of
                                                  select="/metadata/idinfo/ptcontac/cntinfo/cntaddr/country"
                                                  />
                                                  </xsl:element>
                                                  </xsl:element>
                                                  <!-- gmd:country -->

                                                  <xsl:element name="gmd:electronicMailAddress">
                                                  <xsl:element name="gco:CharacterString">
                                                  <xsl:value-of
                                                  select="/metadata/idinfo/ptcontac/cntinfo/cntemail"
                                                  />
                                                  </xsl:element>
                                                  </xsl:element>
                                                  <!-- gmd:electronicMailAddress -->

                                                  </xsl:element>
                                                  <!-- gmd:CI_Address -->
                                                  </xsl:element>
                                                  <!-- gmd:address -->

                                                  <xsl:element name="gmd:onlineResource">
                                                  <xsl:element name="gmd:CI_OnlineResource">

                                                  <xsl:element name="gmd:linkage">
                                                  <xsl:element name="gmd:URL"> </xsl:element>
                                                  </xsl:element>
                                                  <!-- gmd:linkage -->


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

                                                  </xsl:element>
                                                  <!-- gmd:CI_Contact -->
                                                </xsl:element>
                                                <!-- gmd:contactInfo -->

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

                                                  <xsl:element name="gmd:CI_RoleCode">
                                                  <xsl:attribute name="codeList"
                                                          >http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_RoleCode</xsl:attribute>
                                                  <xsl:attribute name="codeListValue"
                                                  >pointofContact</xsl:attribute> pointofContact </xsl:element>
                                                  <!-- gmd:CI_RoleCode -->

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



                                        </xsl:element>
                                        <!-- gmd:CI_ResponsibleParty -->
                                </xsl:element>
                                <!--  citedResponsibleParty -->
                        </xsl:otherwise>
                </xsl:choose>
        </xsl:template>






</xsl:stylesheet>
<!-- End Heere !!!!!!!!!!!!!!!!!!!!!!!!! -->
