<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" xmlns:gmd="http://www.isotc211.org/2005/gmd"
    xmlns:gco="http://www.isotc21.org/2005/gco" xmlns:gml="http://www.opengis.net/gml/3.2"
    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"
    xmlns="http://www.isotc211.org/2005/gfc" version="1.0">
    <xd:doc scope="stylesheet">
        <xd:desc>
            <xd:p><xd:b>Created on:</xd:b> Apr 10, 2013</xd:p>
            <xd:p><xd:b>Author:</xd:b> Matthew McCready</xd:p>
            <xd:p>This stylesheet handles the transform for the gmd:Contact element for the
                identificationInformation section. If the Responsible party is either Geographic
                Products Branch or the Cartographic Products Branch, the appropriate UUID is
                inserted into the metadata. Otherwise, the all the elements in Section 1 of the
                contact information section of the Identification Information section of the FGDC
                file will be transformted.</xd:p>
        </xd:desc>
    </xd:doc>

    <xsl:template name="IdPointOfContact">


        <xsl:variable name="FullOrg" select="/metadata/idinfo/ptcontac/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:choose>
            <xsl:when test="$branch=' Geographic Products Branch'">
                <xsl:element name="gmd:pointOfContact">
                    <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:pointOfContact">

                    <xsl:element name="gmd:CI_ResponsibleParty">
                        <xsl:comment> This Contact Information comes from section 1 of the FGDC
                            Standard </xsl:comment>
                        <xsl:text>
</xsl:text>

                        <xsl:text>
</xsl:text>

                        <xsl:element name="gmd:organisationName">
                            <xsl:element name="gco:CharacterString">
                                <xsl:value-of
                                    select="//metadata/idinfo/ptcontac/cntinfo/cntorgp/cntorg"/>
                            </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:value-of
                                                  select="//metadata/idinfo/ptcontac/cntinfo/cntvoice"
                                                />
                                            </xsl:element>
                                            <!-- gco:CharacterString -->
                                        </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>
                                            <!-- gco:CharacterString -->
                                        </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>
                                            <!-- gco:CharacterString -->
                                        </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>
                                            <!-- gco:CharacterString -->
                                        </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>
                                            <!-- gco:CharacterString -->
                                        </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>
                                            <!-- gco:CharacterString -->
                                        </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>
                                            <!-- gco:CharacterString -->
                                        </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>
                                            <!-- gco:CharacterString -->
                                        </xsl:element>
                                        <!-- gmd:electronicMailAddress -->

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

                            </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>
                                <xsl:attribute name="codeSpace">007</xsl:attribute> pointOfContact </xsl:element>
                            <!-- gmd:CI_RoleCode -->

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

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

    <xsl:template name="IDOriginator">

        <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($division,2,3)"/>

        <!-- <xsl:comment>Division:<xsl:value-of select="$division"/></xsl:comment>
        <xsl:comment>FinalDivision:<xsl:value-of select="$FinalDivision"/></xsl:comment> -->

        <xsl:choose>
            <xsl:when test="$FinalDivision='Geo'">
                <xsl:element name="gmd:citedResponsibleParty">
                    <xsl:attribute name="xlink:href"
                        >https://www.ngdc.noaa.gov/docucomp/cf2b3bf2-5dd9-4fed-a495-1ddfb9a41de2</xsl:attribute>
                    <xsl:attribute name="xlink:title">originator - 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: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">originator</xsl:attribute>
                            <xsl:attribute name="codeSpace">006</xsl:attribute>originator
                        </xsl:element>
                    </xsl:element> </xsl:element>
            </xsl:otherwise>

        </xsl:choose>
        <!--<> -->
    </xsl:template>
</xsl:stylesheet>
