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

<!--
   Name: 08_gmd:dataSetURI.xsl 
   Author:  Matthew J. McCready
   Date:  20111012
   Description:  XSLT stylesheet that transforms the Online_Linkage element of the MIF standard to the gmd:dataSetURI ISO element.
   Modification History:
      Initial   Date      Change Request ID   Description

-->


<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="dataSetURI" match="/">
<!-- <xsl:comment> For the pareents!!!!!!!!!!!!!!!!</xsl:comment> -->

      <xsl:element name="gmd:dataSetURI">
         <xsl:element name="gco:CharacterString">
            <xsl:variable name="mifUrl" select="//MIF/Online_Linkage[@section='citation']"/>
            <xsl:variable name="hasZIP" select="contains($mifUrl,'.zip')"/>

            <xsl:choose>
               <xsl:when test="$hasZIP='true'">
                  <xsl:for-each select="//MIF/Online_Linkage[@section='citation']">
                     <xsl:apply-templates select="."/>
                  </xsl:for-each>
               </xsl:when>
               <xsl:otherwise>
                  <!-- <xsl:comment>2nd choice!!!!!!!</xsl:comment> -->
                  <xsl:variable name="FileId" select="/MIF/Metadata_File_Identifier[1]"/>

                  <xsl:variable name="type" select="substring($FileId,1,2)"/>
                  <xsl:variable name="hasUS" select="contains($FileId,'us')"/>
                  <xsl:variable name="postYear1" select="substring($FileId,9)"/>
                  <xsl:variable name="postYear2" select="substring-after($postYear1,'_')"/>
                 <!--  <xsl:comment>type:<xsl:value-of select="$type"/></xsl:comment> -->
                  <xsl:choose>
                     <xsl:when test="$type='tl' or $hasUS='true'">

<!-- <xsl:comment>in the TL !!!!!!!!!!!</xsl:comment> -->
                        <xsl:variable name="postYear3" select="substring-before($postYear2,'_')"/>
                        <xsl:variable name="theme" select="substring-before($postYear2,'_')"/>
                        <xsl:variable name="slashTheme" select="concat('/',$theme)"/>
                       
                        <xsl:variable name="hasTheme" select="contains($mifUrl,$slashTheme)"/>

                        <xsl:choose>
                           <xsl:when test="$type='tl'">
                              <xsl:variable name="zipFileId" select="string-length($FileId)-7"/>
                              <xsl:variable name="partFileId"
                                 select="substring($FileId,0,$zipFileId)"/>
                              <xsl:variable name="newZipFieId" select="concat($partFileId,'.zip')"/>
                                <!-- <xsl:comment>hastheme<xsl:value-of select="$hasTheme"/></xsl:comment> -->
                              <xsl:choose>
                                 <xsl:when test="$hasTheme='true'">

                                    <xsl:value-of select="$mifUrl"/>
                                 </xsl:when>
                                 <xsl:otherwise>
                                    <!--  <xsl:comment>Right here!!!!!!!!!!!!!!!!!!!!!!</xsl:comment> -->
                                    <xsl:variable name="fullURL" select="concat($mifUrl,$theme)"/>
                                    <xsl:value-of select="$fullURL"/>
                                 </xsl:otherwise>
                              </xsl:choose>
                           </xsl:when>
                           <xsl:otherwise>
                              <xsl:variable name="zipFileId" select="string-length($FileId)-3"/>
                              <xsl:variable name="partFileId"
                                 select="substring($FileId,0,$zipFileId)"/>
                              <xsl:variable name="newZipFieId" select="concat($partFileId,'.zip')"/>
                              <!--  <xsl:comment>hastheme<xsl:value-of select="$hasTheme"/></xsl:comment> -->
                              <xsl:choose>
                                 <xsl:when test="$hasTheme='true'">
                                   
                                    <xsl:value-of select="$mifUrl"/>
                                 </xsl:when>
                                 <xsl:otherwise>
                                    <!--    <xsl:comment>Right here!!!!!!!!!!!!!!!!!!!!!!</xsl:comment> -->
                                    
                                    <xsl:variable name="fullURL"
                                       select="concat($mifUrl,$theme)"/>
                                    <xsl:value-of select="$fullURL"/>
                                 </xsl:otherwise>
                              </xsl:choose>
                           </xsl:otherwise>
                        </xsl:choose>
                     </xsl:when>


                     <xsl:otherwise>
                        <xsl:variable name="zipFileId" select="string-length($FileId)-7"/>
                        <xsl:variable name="partFileId" select="substring($FileId,0,$zipFileId)"/>
                        <xsl:variable name="newZipFieId" select="concat($partFileId,'.zip')"/>
                        <xsl:variable name="theme" select="substring-before($postYear2,'_')"/>
                        <xsl:variable name="slashTheme" select="concat('/',$theme)"/>
                        <xsl:variable name="hasTheme" select="contains($mifUrl,$slashTheme)"/>

                        <xsl:choose>
                           <xsl:when test="$hasTheme='true'">

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

                             
                           </xsl:when>
                           <xsl:otherwise>

                              <xsl:variable name="fullURL"
                                 select="concat($mifUrl,$theme)"/>
                              <xsl:value-of select="$fullURL"/>
                           </xsl:otherwise>
                        </xsl:choose>

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

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