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

<!--
   Name:  05_gmd:contact.xsl
   Author:  Matthew J. McCready
   Date:  20110601
   Description:  XSLT stylesheet that transforms the  contact information from Section 1 of FGDC format to the gmd:contact ISO element.
   Modification History:
      Initial   Date      Change Request ID   Description
        MMC     4/12/2013                      Modified to include UUIDS for the Geographic and Catrographic Products branches. If the metadata does not refer to one
                                                of these branches, the full CI_ResponsibleParty is included
-->


<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:comment> In the Contact Template</xsl:comment> -->
    <xsl:if test="metadata/idinfo/ptcontac">
      <xsl:for-each select="metadata/idinfo/ptcontac">




        <xsl:variable name="FullOrg" select="./cntinfo/cntorgp/cntorg"/>
        <xsl:variable name="postComma1" select="substring-after($FullOrg,',')"/>
        <xsl:variable name="division" select="substring-after($postComma1,',')"/>
        <xsl:variable name="finalDivision" select="substring-before($division,',')"/>
        <xsl:variable name="branch" select="substring-after($division,',')"/>

        <!-- <xsl:comment> postComma1=<xsl:value-of select="$postComma1"/></xsl:comment>
  <xsl:comment> division = <xsl:value-of select="$division"/></xsl:comment>
  <xsl:comment> final division= <xsl:value-of select="$finalDivision"/></xsl:comment>
        <xsl:comment> branch=<xsl:value-of select="$branch"/></xsl:comment>-->

        <xsl:choose>

          <xsl:when test="$branch=' Geographic Products Branch'">
            <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>
            <!-- gmd:contact -->
          </xsl:when>

          <xsl:when test="$branch = 'Cartographic Products Branch'">
            <xsl:attribute name="xlink:href"
              >http://www.ngdc.noaa.gov/docucomp/8dd6ee96-96e1-492c-be55-76cdde8f27f1</xsl:attribute>
            <xsl:attribute name="xlink:title">U.S. Department of Commerce, U.S. Census Bureau,
              Geography Division/Cartographic Products Branch</xsl:attribute>

          </xsl:when>
          <xsl:otherwise>
            <xsl:element name="gmd:contact">
              <xsl:element name="gmd:CI_ResponsibleParty">

                <xsl:element name="gmd:organisationName">
                  <xsl:element name="gco:CharacterString">
                    <xsl:comment> This is the one!!!!!!!!!!!!!!!!!!</xsl:comment>
                    <!-- /metadata/metainfo[1]/metc[1]/cntinfo[1]/cntperp[1]/cntorg[1] -->
                    
                    <xsl:choose>
                      <xsl:when test="./cntinfo/cntorgp/cntorg">
                        <xsl:apply-templates select="./cntinfo/cntorgp/cntorg"/>
                      </xsl:when>
                      <xsl:when test="./cntinfo/cntperp/cntorg"></xsl:when>
                    </xsl:choose>
                    
                  </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:apply-templates select="./cntinfo/cntvoice"/>
                          </xsl:element>
                        </xsl:element>

                        <xsl:if test="./cntinfo/cntfax">
                          <xsl:element name="gmd:facsimile">
                            <xsl:element name="gco:CharacterString">
                              <xsl:apply-templates select="./cntinfo/cntfax"/>
                            </xsl:element>
                          </xsl:element>
                        </xsl:if>
                        
                      </xsl:element>
                    </xsl:element>


                    <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="./cntinfo/cntaddr/address"/>
                          </xsl:element>
                        </xsl:element>
                        <xsl:element name="gmd:city">
                          <xsl:element name="gco:CharacterString">
                            <xsl:apply-templates select="./cntinfo/cntaddr/city"/>
                          </xsl:element>
                        </xsl:element>


                        <xsl:element name="gmd:administrativeArea">
                          <xsl:element name="gco:CharacterString">
                            <xsl:apply-templates select="./cntinfo/cntaddr/state"/>
                          </xsl:element>
                        </xsl:element>
                        <xsl:element name="gmd:postalCode">
                          <xsl:element name="gco:CharacterString">
                            <xsl:apply-templates select="./cntinfo/cntaddr/postal"/>
                          </xsl:element>
                        </xsl:element>

                        <xsl:element name="gmd:country">
                          <xsl:element name="gco:CharacterString">
                            <xsl:apply-templates select="./cntinfo/cntaddr/country"/>
                          </xsl:element>
                        </xsl:element>

                        <xsl:element name="gmd:electronicMailAddress">
                          <xsl:element name="gco:CharacterString">
                            <xsl:apply-templates select="./cntinfo/cntemail"/>
                          </xsl:element>
                        </xsl:element>


                      </xsl:element>
                    </xsl:element>


                  </xsl:element>
                </xsl:element>

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

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



      </xsl:for-each>
    </xsl:if>

  </xsl:template>
</xsl:stylesheet>
