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

<!--
   Name: ExtraContent.xsl 
   Author:  Matthew J. McCready
   Date:  20120418
   Description:  XSLT stylesheet that provides the correct values for the gmd:featureTypes and gmd:title elements of the gmd:contentInfo section. This stylesheet is called by the 
                 12_gmd:contentInfo.xsl stylesheet.  
   Modification History:
   Date:                          Initials                           Description
   5/10/2012                       MJM                                Added code to insert the correct feature type and correct title for the Entity and Attribute File for TIGER, Cartographic and Censes 
   9/11/13                         MJM                                Added code to support 'county'. Now county can be spelled with an upper or lowercase case.
   11/22/13                        MJM                                Corrected the spelling of catalog 
-->

<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:template name="FeatureType">
  <!-- /metadata/eainfo[1]/detailed[1] -->
  <xsl:variable name="Theme" select="/metadata/idinfo/citation/citeinfo/title"/>
  <xsl:text>
</xsl:text>
  <xsl:choose>


   <xsl:when test="contains($Theme,'Census')">
    <xsl:variable name="PreTitle" select="substring-after($Theme,'-')"/>
    <xsl:variable name="Title" select="substring-before($PreTitle,'for')"/>

    <xsl:element name="gmd:featureTypes">
     <xsl:element name="gco:LocalName">
      <xsl:attribute name="codeSpace">unknown</xsl:attribute>
      <xsl:value-of select="$Title"/>
     </xsl:element>
     <!-- gco:LocalName -->
    </xsl:element>
    <!-- gmd:featureTypes -->
   </xsl:when>


   <xsl:when test="contains($Theme,'TIGER/Line')">

    <xsl:text>
</xsl:text>

    <xsl:choose>
     <xsl:when test="contains($Theme,'County')">

      <xsl:variable name="PostFirstComma1" select="substring-after($Theme, ',')"/>
      <xsl:variable name="year" select="substring-before($PostFirstComma1,',')"/>
      <xsl:variable name="PostFirstComma2" select="substring-after($PostFirstComma1,',')"/>
      <xsl:variable name="PostFirstComma3" select="substring-after($PostFirstComma2,',')"/>
      <xsl:variable name="PostFirstComma4" select="substring-after($PostFirstComma3,',')"/>
      <xsl:variable name="PostFirstComma5" select="substring-after($PostFirstComma4,',')"/>
      <xsl:variable name="FinalTheme" select="substring-before($PostFirstComma5,'County')"/>
      <!--      <xsl:variable name="FinalTitle" select="concat($year, ' ' , $PostFirstComma5)"/> -->

      <xsl:element name="gmd:featureTypes">
       <xsl:element name="gco:LocalName">
        <xsl:attribute name="codeSpace">unknown</xsl:attribute>
        <xsl:value-of select="$FinalTheme"/>
       </xsl:element>
      </xsl:element>
     </xsl:when>

     <xsl:when test="contains($Theme,'county')">

      <xsl:variable name="PostFirstComma1" select="substring-after($Theme, ',')"/>
      <xsl:variable name="year" select="substring-before($PostFirstComma1,',')"/>
      <xsl:variable name="PostFirstComma2" select="substring-after($PostFirstComma1,',')"/>
      <xsl:variable name="PostFirstComma3" select="substring-after($PostFirstComma2,',')"/>
      <xsl:variable name="PostFirstComma4" select="substring-after($PostFirstComma3,',')"/>
      <xsl:variable name="PostFirstComma5" select="substring-after($PostFirstComma4,',')"/>
      <xsl:variable name="FinalTheme" select="substring-before($PostFirstComma5,'County')"/>
      <!--      <xsl:variable name="FinalTitle" select="concat($year, ' ' , $PostFirstComma5)"/> -->

      <xsl:element name="gmd:featureTypes">
       <xsl:element name="gco:LocalName">
        <xsl:attribute name="codeSpace">unknown</xsl:attribute>
        <xsl:value-of select="$FinalTheme"/>
       </xsl:element>
      </xsl:element>
     </xsl:when>


     <xsl:when test="contains($Theme,'state')">

      <!-- <xsl:comment> In the state section B</xsl:comment> -->
      <xsl:variable name="PostFirstComma1" select="substring-after($Theme,',')"/>
      <xsl:variable name="PostFirstComma2" select="substring-after($PostFirstComma1,',')"/>
      <xsl:variable name="year" select="substring-before($PostFirstComma1,',')"/>
      <xsl:variable name="PostFirstComma3" select="substring-after($PostFirstComma2,',')"/>
      <xsl:variable name="PostFirstComma4" select="substring-after($PostFirstComma3,',')"/>

      <xsl:element name="gmd:featureTypes">
       <xsl:element name="gco:LocalName">
        <xsl:attribute name="codeSpace">unknown</xsl:attribute>
        <xsl:value-of select="$PostFirstComma4"/>
       </xsl:element>
      </xsl:element>
     </xsl:when>

     <xsl:when test="contains($Theme, 'nation')">

      <xsl:variable name="PostFirstComma1" select="substring-after($Theme,',')"/>

      <xsl:variable name="PostFirstComma2" select="substring-after($PostFirstComma1,',')"/>
      <xsl:variable name="year" select="substring-before($PostFirstComma1,',')"/>
      <xsl:variable name="PostFirstComma3" select="substring-after($PostFirstComma2,',')"/>
      <xsl:variable name="PostFirstComma4" select="substring-after($PostFirstComma3,',')"/>
      <xsl:variable name="FinalTheme" select="substring-before($PostFirstComma4,'National')"/>

      <xsl:element name="gmd:featureTypes">
       <xsl:element name="gco:LocalName">
        <xsl:attribute name="codeSpace">unknown</xsl:attribute>
        <xsl:value-of select="$FinalTheme"/>
       </xsl:element>
      </xsl:element>

     </xsl:when>


     <xsl:when test="contains($Theme,'Series Information File')">

      <xsl:variable name="PostComma1" select="substring-after($Theme,',')"/>
      <xsl:variable name="PostComma2" select="substring-after($PostComma1,',')"/>
      <xsl:variable name="FinalTheme" select="substring-after($PostComma2,'for the')"/>

      <xsl:element name="gmd:featureTypes">
       <xsl:element name="gco:LocalName">
        <xsl:attribute name="codeSpace">unknown</xsl:attribute>
        <xsl:value-of select="$FinalTheme"/>
       </xsl:element>
      </xsl:element>

     </xsl:when>
    </xsl:choose>

   </xsl:when>


   <xsl:when test="contains($Theme,'Cartographic')">
    <xsl:variable name="PreTitle" select="substring-after($Theme,'-')"/>
    <xsl:variable name="Title" select="substring-before($PreTitle,'for')"/>
    <xsl:variable name="type" select="substring-before($Theme,',')"/>
    <xsl:variable name="first" select="substring-after($Theme,',')"/>
    <xsl:variable name="EAyear" select="substring-before($first,',')"/>
    <xsl:variable name="second" select="substring-after($first,',')"/>
    <xsl:variable name="third" select="substring-after($second,',')"/>
    <xsl:variable name="fourth" select="substring-after($third,',')"/>
    <xsl:variable name="finalTheme" select="substring-after($fourth,',')"/>
    <xsl:variable name="featureTypeA" select="substring-after($Theme, 'File,')"/>
    <xsl:variable name="featureTypeB" select="substring-before($featureTypeA, ',')"/>
    <xsl:variable name="featureType" select="substring-before($featureTypeB,'for')"/>
    <xsl:variable name="finalTitle" select="concat($EAyear, ' ', $type, $finalTheme)"/>

    <xsl:element name="gmd:featureTypes">
     <xsl:element name="gco:LocalName">
      <xsl:attribute name="codeSpace">unknown</xsl:attribute>
      <xsl:value-of select="$featureType"/>
     </xsl:element>
    </xsl:element>

   </xsl:when>

   <xsl:when test="contains($Theme,'Series Information File')">


    <xsl:element name="gmd:featureTypes">
     <xsl:element name="gco:LocalName">
      <xsl:attribute name="codeSpace">unknown</xsl:attribute>
      <xsl:value-of select="$Theme"/>
     </xsl:element>
    </xsl:element>

   </xsl:when>

   <xsl:otherwise>
    <xsl:element name="gmd:featureTypes">
     <xsl:element name="gco:LocalName">
      <xsl:attribute name="codeSpace">unknown</xsl:attribute>

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

  </xsl:choose>
 </xsl:template>



 <xsl:template name="ContentTitle">
  <!-- <xsl:comment>In the Content Title template!!!!!!!!!!!!!</xsl:comment> -->
  <xsl:variable name="FeatureCatolog">Feature Catalog for the</xsl:variable>
  <xsl:variable name="Theme" select="/metadata/idinfo/citation/citeinfo/title"/>
  <!-- <xsl:comment>Theme: <xsl:value-of select="$Theme"/> </xsl:comment> -->

  <xsl:choose>

   <xsl:when test="contains($Theme,'Census')">
   <!--  <xsl:comment> In the Census Section </xsl:comment> -->

    <xsl:choose>
     <xsl:when test="contains($Theme,'state')">
      <xsl:variable name="PostFirstComma1" select="substring-after($Theme, ',')"/>
      <xsl:variable name="year" select="substring-before($PostFirstComma1,',')"/>
      <xsl:variable name="PostFirstComma2" select="substring-after($PostFirstComma1,',')"/>
      <xsl:variable name="PostFirstComma3" select="substring-after($PostFirstComma2,',')"/>
      <xsl:variable name="PostFirstComma4" select="substring-after($PostFirstComma3,',')"/>
      <xsl:variable name="FinalTitle"
       select="concat($FeatureCatolog, ' ', $year, ' ' , $PostFirstComma4)"/>

      <xsl:element name="gmd:title">
       <xsl:element name="gco:CharacterString">

        <xsl:value-of select="$FinalTitle"/>
       </xsl:element>
      </xsl:element>

     </xsl:when>

     <xsl:when test="contains($Theme,'TIGER')">
      <xsl:choose>


       <xsl:when test="contains($Theme, 'nation')">

        <xsl:text>
</xsl:text>
        <xsl:variable name="PostFirstComma1" select="substring-after($Theme,',')"/>
        <xsl:text>
</xsl:text>
        <xsl:variable name="PostFirstComma2" select="substring-after($PostFirstComma1,',')"/>
        <xsl:variable name="year" select="substring-before($PostFirstComma1,',')"/>
        <xsl:variable name="PostFirstComma3" select="substring-after($PostFirstComma2,',')"/>
        <xsl:variable name="PostFirstComma4" select="substring-after($PostFirstComma3,',')"/>
        <xsl:variable name="FinalTitle"
         select="concat('Feature Catalog for',$year, ' ', $PostFirstComma4)"/>
        <xsl:element name="gmd:title">
         <xsl:element name="gco:CharacterString">
          <xsl:value-of select="$FinalTitle"/>
         </xsl:element>
        </xsl:element>
       </xsl:when>
      </xsl:choose>
     </xsl:when>


     <xsl:otherwise>
      <xsl:variable name="PreTitle" select="substring-after($Theme,'-')"/>
      <xsl:variable name="TitleA" select="substring-before($PreTitle,'for')"/>
      <xsl:variable name="Title" select="concat($FeatureCatolog, $TitleA)"/>
      <xsl:element name="gmd:title">
       <xsl:element name="gco:CharacterString">
        <xsl:value-of select="$Title"/>
       </xsl:element>
      </xsl:element>
     </xsl:otherwise>

    </xsl:choose>
   </xsl:when>

   <xsl:when test="contains($Theme,'Cartographic')">
    <xsl:variable name="type" select="substring-before($Theme,',')"/>
    <xsl:variable name="EAyear" select="substring-before($Theme,'Cartographic')"/>
    <xsl:variable name="second" select="substring-after($Theme,',')"/>
    <xsl:variable name="finalTheme" select="substring-before($second,'for')"/>
    <xsl:variable name="finalTitle" select="concat($FeatureCatolog,' ', $EAyear, $finalTheme)"/>
    <xsl:element name="gmd:title">
     <xsl:element name="gco:CharacterString">
      <xsl:value-of select="$finalTitle"/>
     </xsl:element>
    </xsl:element>
   </xsl:when>

   <xsl:when test="contains($Theme,'TIGER')">

   <!--  <xsl:comment> In the TIGER section!!!!!!!!!</xsl:comment> -->
    <xsl:choose>
     <xsl:when test="contains($Theme,'County')">
      <!-- <xsl:comment>In the county section</xsl:comment> -->
      <xsl:variable name="PostFirstComma1" select="substring-after($Theme, ',')"/>
      <xsl:variable name="year" select="substring-before($PostFirstComma1,',')"/>
      <xsl:variable name="PostFirstComma2" select="substring-after($PostFirstComma1,',')"/>


      <xsl:choose>
       <xsl:when test="contains($Theme,'state')">

        <xsl:variable name="PostFirstComma3" select="substring-after($PostFirstComma2,',')"/>
        <xsl:variable name="PostFirstComma4" select="substring-after($PostFirstComma3,',')"/>
        <xsl:variable name="FinalTitle"
         select="concat($FeatureCatolog, ' ', $year, ' ' , $PostFirstComma4)"/>


        <xsl:element name="gmd:title">
         <xsl:element name="gco:CharacterString">

          <xsl:value-of select="$FinalTitle"/>
         </xsl:element>
        </xsl:element>

       </xsl:when>

       <xsl:when test="contains($Theme, 'Series Information File')">
        <xsl:variable name="PostFor" select="substring-after($PostFirstComma2,'for the')"/>
        <xsl:variable name="FinalTitle" select="concat($FeatureCatolog, ' ', $year, ' ' , $PostFor)"/>

        <xsl:element name="gmd:title">
         <xsl:element name="gco:CharacterString">

          <xsl:value-of select="$FinalTitle"/>
         </xsl:element>
        </xsl:element>
       </xsl:when>

       <xsl:otherwise>
        <xsl:variable name="PostFirstComma3" select="substring-after($PostFirstComma2,',')"/>
        <xsl:variable name="PostFirstComma4" select="substring-after($PostFirstComma3,',')"/>
        <xsl:variable name="PostFirstComma5" select="substring-after($PostFirstComma4,',')"/>
        <xsl:variable name="FinalTitle"
         select="concat($FeatureCatolog, ' ', $year, ' ' , $PostFirstComma5)"/>

        <xsl:element name="gmd:title">
         <xsl:element name="gco:CharacterString">

          <xsl:value-of select="$FinalTitle"/>
         </xsl:element>
        </xsl:element>
       </xsl:otherwise>
      </xsl:choose>

     </xsl:when>

     <xsl:when test="contains($Theme,'county')">
      <!-- <xsl:comment>In the county section (lowercase)</xsl:comment> -->
      <xsl:variable name="PostFirstComma1" select="substring-after($Theme, ',')"/>
      <xsl:variable name="year" select="substring-before($PostFirstComma1,',')"/>
      <xsl:variable name="PostFirstComma2" select="substring-after($PostFirstComma1,',')"/>
      <xsl:variable name="PostFirstComma3" select="substring-after($PostFirstComma2,',')"/>
      <xsl:variable name="PostFirstComma4" select="substring-after($PostFirstComma3,',')"/>
      <xsl:variable name="PostFirstComma5" select="substring-after($PostFirstComma4,',')"/>
      <xsl:variable name="FinalTitle"
       select="concat($FeatureCatolog, ' ', $year, ' ' , $PostFirstComma5)"/>

      <xsl:element name="gmd:title">
       <xsl:element name="gco:CharacterString">

        <xsl:value-of select="$FinalTitle"/>
       </xsl:element>
      </xsl:element>
     </xsl:when>

     <xsl:when test="contains($Theme,'state')">
      <!--  <xsl:comment>in the state section </xsl:comment> -->
      <xsl:text>
</xsl:text>

      <xsl:variable name="PostFirstComma1" select="substring-after($Theme,',')"/>
      <xsl:variable name="PostFirstComma2" select="substring-after($PostFirstComma1,',')"/>
      <xsl:variable name="year" select="substring-before($PostFirstComma1,',')"/>
      <xsl:variable name="PostFirstComma3" select="substring-after($PostFirstComma2,',')"/>
      <xsl:variable name="PostFirstComma4" select="substring-after($PostFirstComma3,',')"/>
      <xsl:variable name="FinalTitle" select="concat($FeatureCatolog, $year, $PostFirstComma4)"/>
      <xsl:element name="gmd:title">
       <xsl:element name="gco:CharacterString">
        <xsl:value-of select="$FinalTitle"/>
       </xsl:element>
      </xsl:element>

     </xsl:when>



     <xsl:when test="contains($Theme, 'nation')">

      <xsl:text>
</xsl:text>
      <xsl:variable name="PostFirstComma1" select="substring-after($Theme,',')"/>
      <xsl:text>
</xsl:text>
      <xsl:variable name="PostFirstComma2" select="substring-after($PostFirstComma1,',')"/>
      <xsl:variable name="year" select="substring-before($PostFirstComma1,',')"/>
      <xsl:variable name="PostFirstComma3" select="substring-after($PostFirstComma2,',')"/>
      <xsl:variable name="PostFirstComma4" select="substring-after($PostFirstComma3,',')"/>
      <xsl:variable name="FinalTitle"
       select="concat('Feature Catalog for',$year, ' ', $PostFirstComma4)"/>
      <xsl:element name="gmd:title">
       <xsl:element name="gco:CharacterString">
        <xsl:value-of select="$FinalTitle"/>
       </xsl:element>
      </xsl:element>
     </xsl:when>

     <xsl:when test="contains($Theme, 'Series')">
      <xsl:variable name="PostFirstComma1" select="substring-after($Theme, ',')"/>
      <xsl:variable name="year" select="substring-before($PostFirstComma1,',')"/>
      <xsl:variable name="PostFirstComma2" select="substring-after($PostFirstComma1,',')"/>
      <xsl:variable name="PostComma2" select="substring-after($PostFirstComma1,',')"/>
      <xsl:variable name="PostFor" select="substring-after($PostComma2,'for the')"/>

      <xsl:variable name="FinalTitle" select="concat($FeatureCatolog, ' ', $year, ' ' , $PostFor)"/>
      <xsl:element name="gmd:title">
       <xsl:element name="gco:CharacterString">
        <xsl:value-of select="$FinalTitle"/>
       </xsl:element>
      </xsl:element>
     </xsl:when>


    </xsl:choose>

   </xsl:when>

  </xsl:choose>
 </xsl:template>


</xsl:stylesheet>
