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

<!--
   Name:NonISOKeywords.xsl   
   Author:  Matthew J. McCready
   Date:  20111012
   Description:  XSLT stylesheet that transforms the values stored in the Theme_Keyword and Place_Keyword MIF elements to the gmd:descriptiveKeywords element. This 
   stylesheet consists of three templates: NonISOKeywords, NonThemeISOKeywords and PlaceKeywords. The NonISOKeywords template is the template called by the
   11_gmd:identificationInfo.xsl stylesheet. This template consists of two other templates: NonThemeISOKeywords and PlaceKeywords. The NonThemeISOKeywords template
   takes all the values in the MIF/Theme_Keyword element that are not ISO Topic Category Keywords and transforms them into the 
   gmd:descriptiveKeywords/gmd:MD_Keywords/gmd:keyword/gco:CharacterString ISO element. It also provides the correct value of 'theme' for the MD_KeywordTypeCode
   codelist. The PlaceKeywords template takes all the values in the MIF/Place_Keyword  element and transforms it into the
   gmd:descriptiveKeywords/gmd:MD_Keywords/gmd:keyword/gco:CharacterString ISO element. It also provides the correct value of 'place' for the MD_KeywordTypeCode codelist
   Modification History:
      Initial   Date      Change Request ID   Description
      MMC        4/23/2013                    Changed the formatting on the gmd:keyword/gco:characterstring element so that the entire element appear on one line.
-->

<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">
 <xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes"/>
 <xsl:variable name="MetDate" select="//metadata/metainfo/metd"/>

 <xsl:template name="NonISOKeywords">
 <!-- <xsl:comment> In the NonISOKeywords template</xsl:comment> -->
  <xsl:call-template name="NonThemeISOKeywords"/>
  <xsl:call-template name="PlaceKeywords"/>
 </xsl:template>

 <xsl:template name="NonThemeISOKeywords">
 <!--  <xsl:comment>In the NonThemeISOKeywords template</xsl:comment> -->
  <xsl:variable name="CodeListLoc"
   >http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_KeywordTypeCode</xsl:variable>
  <xsl:variable name="NonISOKeywordsTheme" select="//MIF/Theme_Keyword_Thesaurus"/>

  <xsl:for-each-group select="//MIF/Theme_Keyword_Thesaurus, //MIF/Theme_Keyword" group-by="@gid1">
   <!-- group-by="@gid1" -->


   <xsl:sort select="current-grouping-key()" data-type="number"/>
   <xsl:sort select="current-grouping-key()" data-type="text"/>

   <xsl:for-each select="current-group()">

    <xsl:if test="name()='Theme_Keyword_Thesaurus'">

     <xsl:variable name="NonISOKeywordsThemeA" select="."/>


     <xsl:variable name="ThemeType" select="."/>
     <xsl:text>
</xsl:text>

     <xsl:if test="$ThemeType !='ISO 19115 Topic Categories'">

      <xsl:element name="gmd:descriptiveKeywords">
       <xsl:element name="gmd:MD_Keywords">

        <xsl:for-each select="//MIF/Theme_Keyword">
         <xsl:variable name="ThemeKey" select="."/>


         <xsl:variable name="BoundCheck" select="contains($ThemeKey, 'boundaries')"/>


         <xsl:if test="$ThemeKey !='farming'">
          <xsl:if test="$BoundCheck != 'true'">
           <xsl:if test="$ThemeKey !='biota'">
            <xsl:if test="$ThemeKey !='Boundaries'">
             <xsl:if test="$ThemeKey !='boundaries'">
              <xsl:if test="$ThemeKey !='transportation' ">
               <xsl:if test="$ThemeKey !='climatologyMeteorologyAtmosphere' ">
                <xsl:if test="$ThemeKey !='economy' ">
                 <xsl:if test="$ThemeKey !='elevation'">
                  <xsl:if test="$ThemeKey !='environment'">
                   <xsl:if test="$ThemeKey !='geoscientificInformation'">
                    <xsl:if test="$ThemeKey !='health'">
                     <xsl:if test="$ThemeKey !='imageryBaseMapsEarthCover'">
                      <xsl:if test="$ThemeKey !='intelligenceMilitary'">
                       <xsl:if test="$ThemeKey !='inlandWaters'">
                        <xsl:if test="$ThemeKey !='location'">
                         <xsl:if test="$ThemeKey !='oceans'">
                          <xsl:if test="$ThemeKey !='planningCadastre'">
                           <xsl:if test="$ThemeKey !='society'">
                            <xsl:if test="$ThemeKey !='structure'">
                             <xsl:if test="$ThemeKey !='utilitiesCommunication'">
                              <xsl:element name="gmd:keyword">
                               <xsl:element name="gco:CharacterString">
                                <xsl:value-of select="."/>
                               </xsl:element>
                              </xsl:element>
                             </xsl:if>
                            </xsl:if>
                           </xsl:if>
                          </xsl:if>
                         </xsl:if>
                        </xsl:if>
                       </xsl:if>
                      </xsl:if>
                     </xsl:if>
                    </xsl:if>
                   </xsl:if>
                  </xsl:if>
                 </xsl:if>
                </xsl:if>
               </xsl:if>
              </xsl:if>
             </xsl:if>
            </xsl:if>
           </xsl:if>
          </xsl:if>
         </xsl:if>

        </xsl:for-each>

        <xsl:element name="gmd:type">
         <xsl:element name="gmd:MD_KeywordTypeCode">
          <xsl:attribute name="codeList"
           >http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_KeywordTypeCode</xsl:attribute>
          <xsl:attribute name="codeListValue">theme</xsl:attribute>
          <xsl:attribute name="codeSpace">005</xsl:attribute> theme </xsl:element>
        </xsl:element>


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

          <xsl:element name="gmd:title">
           <xsl:element name="gco:CharacterString">
            <xsl:value-of select="$NonISOKeywordsThemeA"/>
           </xsl:element>
          </xsl:element>

          <xsl:element name="gmd:date">
           <xsl:attribute name="gco:nilReason">unknown</xsl:attribute>
          </xsl:element>

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

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

  </xsl:for-each-group>
 </xsl:template>

 <xsl:template name="PlaceKeywords">
  <!-- <xsl:comment>in the PlaceKeywords template</xsl:comment> -->
  <xsl:element name="gmd:descriptiveKeywords">
   <xsl:element name="gmd:MD_Keywords">

    <xsl:element name="gmd:keyword">
     <xsl:element name="gco:CharacterString">US</xsl:element>
    </xsl:element>
    <!-- gmd:keyword  -->

    <xsl:element name="gmd:type">
     <xsl:element name="gmd:MD_KeywordTypeCode">
      <xsl:attribute name="codeList"
       >http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_KeywordTypeCode</xsl:attribute>
      <xsl:attribute name="codeListValue">place</xsl:attribute>
      <xsl:attribute name="codeSpace">002</xsl:attribute> place </xsl:element>
    </xsl:element>

    <xsl:element name="gmd:thesaurusName">
     <xsl:element name="gmd:CI_Citation">
      <xsl:element name="gmd:title">
       <xsl:element name="gco:CharacterString">None</xsl:element>
      </xsl:element>

      <xsl:element name="gmd:date">
       <xsl:attribute name="gco:nilReason">unknown</xsl:attribute>
      </xsl:element>
     </xsl:element>
    </xsl:element>

   </xsl:element>
   <!-- gmd:MD_Keywords  -->
  </xsl:element>
  <!-- gmd:descriptiveKeywords-->

 </xsl:template>

</xsl:stylesheet>
