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

<!--
   Name:  05_gmd:contact.xsl
   Author:  Matthew J. McCready
   Date:  20111006
   Description:  XSLT stylesheet that transforms the  contact information from the 'Contact_Organization' elements with the 'metadata' attribute to the                          gmd:contact ISO element.
   Modification History:
      Initial   Date      Change Request ID   Description

-->

<xsl:stylesheet version = "1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gfc="http://www.isotc211.org/2005/gfc"
  xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:gmx="http://www.isotc211.org/2005/gmx"
  xmlns:gsr="http://www.isotc211.org/2005/gsr" xmlns:gss="http://www.isotc211.org/2005/gss"
  xmlns:gts="http://www.isotc211.org/2005/gts" xmlns:gml="http://www.opengis.net/gml/3.2"
  xmlns:xlink="http://www.w3.org/1999/xlink" 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"
  xmlns="http://www.isotc211.org/2005/gfc"
>
<xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes"/>



<xsl:template name="contact" match="/">
  <xsl:variable name="FullContact" select="/MIF/Contact_Organization[@section='metadata']"/>
  <xsl:variable name="postComma1" select="substring-after($FullContact,',')"/>
  <xsl:variable name="division" select="substring-after($postComma1,',')"/>
  <xsl:variable name="containsSlash" select="contains($division,'/')"></xsl:variable>

<!-- <xsl:comment>FullContact: <xsl:value-of select="$FullContact"/> </xsl:comment>
 <xsl:comment>postComma1: <xsl:value-of select="$postComma1"/> </xsl:comment>
  <xsl:comment>division:<xsl:value-of select="$division"/></xsl:comment> -->
  
  <xsl:choose>
    <xsl:when test="$containsSlash='true'">
      
      <xsl:variable name="postSlash" select="substring-after($division,'/')" />
      <xsl:variable name="Branch" select="substring($postSlash,1,3)"></xsl:variable>
      
      <!-- <xsl:comment>we have a slash</xsl:comment> 
      <xsl:comment>postSlash:<xsl:value-of select="$postSlash"/></xsl:comment>
      <xsl:comment>Branch:<xsl:value-of select="$Branch"/></xsl:comment>-->
      
      <xsl:choose>
        <xsl:when test="$Branch='Car'">
          <xsl:element name="gmd:contact">
          <xsl:attribute name="xlink:href"
            >https://www.ngdc.noaa.gov/docucomp/8dd6ee96-96e1-492c-be55-76cdde8f27f1</xsl:attribute>
            <xsl:attribute name="xlink:title">pointofContact - U.S. Department of Commerce, U.S. Census Bureau, Geography Division/Cartographic Products Branch</xsl:attribute>
       </xsl:element>
        </xsl:when>
        <xsl:when test="$Branch='Geo'">
          <xsl:element name="gmd:contact">
          <xsl:attribute name="xlink:href"
            >https://www.ngdc.noaa.gov/docucomp/c5ceb003-1ed6-4126-8a16-bc08ce8fc267</xsl:attribute>
          <xsl:attribute name="xlink:title">U.S. Department of Commerce, U.S. Census Bureau, Geography Division, Geographic Products Branch</xsl:attribute>
        </xsl:element>
        </xsl:when>
      </xsl:choose>
      
     
      
    </xsl:when>
    
    <xsl:otherwise>
  


  <xsl:if test="/MIF/Contact_Organization/@section='metadata'">
    <xsl:if test="/MIF/Contact_Organization/@section='metadata'">

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

<xsl:element name="gmd:organisationName">
<xsl:element name="gco:CharacterString">
<!-- <xsl:apply-templates select="//metadata/metainfo/Metadata_Language"/> -->
  <xsl:apply-templates select="/MIF/Contact_Organization[@section='metadata']"/>
</xsl:element><!-- gco:CharacterString -->
</xsl:element><!-- gmd:organisationName -->

<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:apply-templates select="/MIF/Contact_Voice_Telephone[@section='metadata']"/>
</xsl:element><!-- gco:CharacterString -->
</xsl:element><!-- gmd:voice -->

<xsl:element name="gmd:facsimile">
<xsl:element name="gco:CharacterString">
  <xsl:apply-templates select="/MIF/Contact_Facsimile_Telephone[@section='metadata']"/>
</xsl:element><!-- gco:CharacterString -->
</xsl:element><!-- gmd:fax -->

</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:apply-templates select="/MIF/Address[@section='metadata']"/>
</xsl:element><!-- gco:CharacterString -->
</xsl:element><!-- gmd:deliveryPoint -->

<xsl:element name="gmd:city">
<xsl:element name="gco:CharacterString">
  <xsl:apply-templates select="//MIF/City[@section='metadata']"/>
</xsl:element><!-- gco:CharacterString -->
</xsl:element><!-- gmd:city -->


<xsl:element name="gmd:administrativeArea">
<xsl:element name="gco:CharacterString">
  <xsl:apply-templates select="/MIF/State_or_Province[@section='metadata']"/>
</xsl:element><!-- gco:CharacterString -->
</xsl:element><!-- gmd:administrativeArea -->

<xsl:element name="gmd:postalCode">
<xsl:element name="gco:CharacterString">
  <xsl:apply-templates select="/MIF/Postal_Code[@section='metadata']"/>
</xsl:element><!-- gco:CharacterString -->
</xsl:element><!-- gmd:administrativeArea -->

<xsl:element name="gmd:country">
<xsl:element name="gco:CharacterString">
  <xsl:apply-templates select="/MIF/Country[@section='metadata']"/>
</xsl:element><!-- gco:CharacterString -->
</xsl:element><!-- gmd:country -->

<xsl:element name="gmd:electronicMailAddress">
<xsl:element name="gco:CharacterString">
  <xsl:apply-templates select="/MIF/Contact_Electronic_Mail_Address[@section='metadata']"/>
</xsl:element><!-- gco:CharacterString -->
</xsl:element><!-- electronicMailAddress -->


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


</xsl:element><!-- 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><!-- gmd:contact -->

</xsl:if>
</xsl:if>
</xsl:otherwise>
    
  </xsl:choose>
</xsl:template>
</xsl:stylesheet>

