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

<!--
Name:  01_gmd:FileIdentifier.xsl
   Author:  Matthew J. McCready
   Date:  20110601
   Description:  XSLT stylesheet that transforms the  Metadata_File_Identifier from Section 7 of the FGDC standard into the gmd:fileIdentifier  
                 ISO element.
   Modification History:
      Initial   Date      Change Request ID   Description
      12/05/2013                              Modified to insert an element when no language is given. It automatically inserts 'eng' 
-->


<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="FileIdentifier" match="/">

      <xsl:choose>

         <xsl:when test="//MIF/Metadata_File_Identifier">
            <xsl:variable name="fullFileName" select="//MIF/Metadata_File_Identifier"/>
            <xsl:variable name="hasUS" select="contains($fullFileName,'us')"/>
            <xsl:variable name="firstTwo" select="substring($fullFileName,1,2)"/>
           <!--  <xsl:comment>firstTwo:<xsl:value-of select="$firstTwo"/></xsl:comment> -->
            <xsl:choose>
               <xsl:when test="$firstTwo='cb'">
                  <!-- <xsl:comment>in the cb</xsl:comment> -->
                  <xsl:element name="gmd:fileIdentifier">

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

                        <xsl:choose>
                           <xsl:when test="$hasUS='true'">
                              <xsl:variable name="preUS"
                                 select="substring-before($fullFileName,'us')"/>
                              <xsl:variable name="postUS"
                                 select="substring-after($fullFileName,'us_')"/>
                              <xsl:variable name="newFilename"
                                 select="concat('SeriesCollection_',$preUS,$postUS)"/>
                              <xsl:value-of select="$newFilename"/>
                           </xsl:when>
                           <xsl:otherwise>
                              <xsl:variable name="postcb"
                                 select="substring-after($fullFileName,'cb_')"/>
                              <xsl:variable name="preUnderscore"
                                 select="substring-before($postcb,'_')"/>
                              <xsl:variable name="postUnder1" select="substring-after($postcb,'_')"/>
                              <!-- <xsl:comment>postUnder:<xsl:value-of select="$postUnder1"/></xsl:comment> -->
                              <xsl:variable name="postUnder2"
                                 select="substring-after($postUnder1,'_')"/>
                              <xsl:variable name="newFilename"
                                 select="concat('SeriesCollection_',$firstTwo,'_',$preUnderscore,'_',$postUnder2)"/>
                              <xsl:value-of select="$newFilename"/>

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


                     </xsl:element>

                  </xsl:element>
               </xsl:when>
               <xsl:when test="$firstTwo='tl'">
                  <xsl:element name="gmd:fileIdentifier">
                     
                     <xsl:element name="gco:CharacterString">
                  <xsl:choose>
                     <xsl:when test="$hasUS='true'">
                        <xsl:variable name="preUS" select="substring-before($fullFileName,'us')"/>
                        <xsl:variable name="postUS" select="substring-after($fullFileName,'us_')"/>
                        <xsl:variable name="newFilename"
                           select="concat('SeriesCollection_',$preUS,$postUS)"/>
                        <xsl:value-of select="$newFilename"/>
                     </xsl:when>
                     <xsl:otherwise>
                        <xsl:variable name="posttl" select="substring-after($fullFileName,'tl')"/>
                        <xsl:variable name="preUnderscore"
                           select="substring-before($posttl,'_')"/>
                        <xsl:variable name="postUnder1" select="substring-after($posttl,'_')"/>
                        <xsl:variable name="year" select="substring-before($postUnder1,'_')"/>
                        
                         <!-- <xsl:comment>postUnder1:<xsl:value-of select="$postUnder1"/></xsl:comment>
                        <xsl:comment>year<xsl:value-of select="$year"/></xsl:comment> -->
                        
                        <xsl:variable name="postUnder2"
                           select="substring-after($postUnder1,'_')"/>
                        <xsl:variable name="postUnder3" select="substring-after($postUnder2,'_')"/>
                        <xsl:variable name="newFilename"
                           select="concat('SeriesCollection_',$firstTwo,'_',$year,$preUnderscore,'_',$postUnder3)"/>
                        <xsl:value-of select="$newFilename"/>
                        
                     </xsl:otherwise>
                  </xsl:choose>
                     </xsl:element>
                     </xsl:element>
               </xsl:when>
            </xsl:choose>


         </xsl:when>
      </xsl:choose>
   </xsl:template>
</xsl:stylesheet>
