<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:template name="IndentificationInformation">
        <xsl:element name="idinfo">
            <xsl:call-template name="Citation"/>
            <xsl:call-template name="Description"/>
            <xsl:call-template name="TimePeriod"/>
            <xsl:call-template name="Status"/>
            <xsl:call-template name="BoundingCoordinates"/>
            <xsl:call-template name="Keywords"/>
            <xsl:call-template name="Constraints"/>
            <xsl:call-template name="PointOfContact"/>
        </xsl:element>
    </xsl:template>

    <xsl:template name="Citation">
        <xsl:element name="citation">

            <xsl:element name="citeinfo">

                <xsl:element name="origin">
                    <xsl:value-of select="/metadata/idinfo/citation/citeinfo/origin"/>
                </xsl:element>

                <xsl:element name="pubdate">
                    <xsl:value-of select="/metadata/idinfo/citation/citeinfo/pubdate"/>
                </xsl:element>

                <xsl:element name="title">
                    <xsl:variable name="SeriesInsert"> Series Information File for the</xsl:variable>
                    <xsl:variable name="WholeTitle"
                        select="/metadata/idinfo/citation/citeinfo/title"/>
                <!--   <xsl:comment>
                        <xsl:value-of select="$WholeTitle"/>
                    </xsl:comment> -->
                    <xsl:variable name="Title1" select="substring-before($WholeTitle,',')"/>
                   <!-- <xsl:comment> Title 1= <xsl:value-of select="$Title1"/></xsl:comment> -->
                    <xsl:variable name="PostTitle1" select="substring-after($WholeTitle,',')"/>
                    
                    <xsl:choose>
                        <xsl:when test="$Title1 = 'TIGER/Line Shapefile'">
<xsl:variable name="year" select="substring-before($PostTitle1,',')"/>
                    <xsl:variable name="Title2" select="substring-after($PostTitle1, ',')"/>
                    <xsl:variable name="Geo" select="substring-before($Title2, ',')"/>
                    <xsl:variable name="Geo2" select="substring($Geo,2,5)"/>
             <!--   <xsl:comment>Geo2= <xsl:value-of select="$Geo2"/></xsl:comment> -->
                            <xsl:choose>
                                <xsl:when test="$Geo2= 'count'">
                                <!--   <xsl:comment>in the countA</xsl:comment> -->
                                    <xsl:variable name="Title3"
                                        select="substring-after($Title2, ',')"/>
                                    <xsl:variable name="Title4"
                                        select="substring-after($Title3, ',')"/>
                                    <xsl:variable name="FileType"
                                        select="substring-after($Title4 , ',')"/>
                                    <xsl:variable name="newTitle"
                                        select="concat($Title1, ',', $year, ',',  $SeriesInsert, $FileType, 's' )"/>
                                    <xsl:value-of select="$newTitle"/>

                                </xsl:when>
                                <xsl:when test="$Geo2= ' coun'">
                                  <!-- <xsl:comment>in the countB</xsl:comment> -->
                                    <xsl:variable name="Title3"
                                        select="substring-after($Title2, ',')"/>
                                    <xsl:variable name="Title4"
                                        select="substring-after($Title3, ',')"/>
                                    <xsl:variable name="FileType"
                                        select="substring-after($Title4 , ',')"/>
                                    <xsl:variable name="newTitle"
                                        select="concat($Title1, ',', $year, ',',  $SeriesInsert, $FileType, 's' )"/>
                                    <xsl:value-of select="$newTitle"/>

                                </xsl:when>
                                <xsl:when test="$Geo2=' stat'">
                                  <!--  <xsl:comment>In the State</xsl:comment> -->
                                    <xsl:variable name="Title3"
                                        select="substring-after($Title2,',')"/>
                                    <xsl:variable name="FileType"
                                        select="substring-after($Title3,',')"/>
                                    <xsl:variable name="newTitle"
                                        select="concat($Title1, ',', $year, ',',  $SeriesInsert, $FileType, 's' )"/>
                                    <xsl:value-of select="$newTitle"/>
                                </xsl:when>
                                <xsl:otherwise>
                                   <!-- <xsl:comment>in 2010</xsl:comment>
                                    <xsl:comment>The value of Title2 is <xsl:value-of select="$Title2"/></xsl:comment> -->
                                    <xsl:variable name="Title3" select="substring-before($Title2,',')"/>
                                    <xsl:variable name="Title4" select="substring-after($Title2,',')"></xsl:variable>
                                    <xsl:variable name="lastPart" select="substring-after($Title4,',')"></xsl:variable>
                                    <xsl:variable name="newTitle" select="concat($Title1, ',', $year, ',',$SeriesInsert, $Title3,',', $lastPart)"></xsl:variable>
                                   <!-- <xsl:comment>Title3 = <xsl:value-of select="$Title3"/></xsl:comment>
                                    <xsl:comment> Title4 = <xsl:value-of select="$Title4"/></xsl:comment>
                                    <xsl:comment> last part = <xsl:value-of select="$lastPart"/></xsl:comment> -->
                                    <xsl:value-of select="$newTitle"/>
                               </xsl:otherwise>
                            </xsl:choose>
                        </xsl:when>
                        <xsl:otherwise>
                           <!-- <xsl:comment> In the MIF Files!!!!!!!!!!</xsl:comment> -->
                            <xsl:variable name="PostComma" select="substring-after($WholeTitle,',')"/>
                            <!-- <xsl:comment>PostComma= <xsl:value-of select="$PostComma"/> </xsl:comment> -->
                            <xsl:variable name="PreFor" select="substring-before($PostComma,' for')"/>
                        <!--    <xsl:comment> PreFor <xsl:value-of select="$PreFor"/></xsl:comment> -->
                            <xsl:variable name="Scale" select="substring-after($PostComma,',')"/>
                      <!--      <xsl:comment> Scale = <xsl:value-of select="$Scale"/></xsl:comment> -->
                            <xsl:variable name="newTitle" select="concat($SeriesInsert,' ',$Title1,',',$PreFor,',',$Scale)"/>
                            <xsl:value-of select="$newTitle"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:element>

                <xsl:element name="edition">
                    <xsl:value-of select="/metadata/idinfo/citation/citeinfo/edition"/>
                </xsl:element>

                <xsl:element name="geoform">
                    <xsl:value-of select="/metadata/idinfo/citation/citeinfo/geoform"/>
                </xsl:element>

               
                <xsl:comment>The location of the collection in the Census Metadata Server </xsl:comment>    
                
                <xsl:element name="onlink">
                    <xsl:variable name="FTP"> http://www2.census.gov/geo/datadotgov/</xsl:variable>
                    <xsl:variable name="FullLinkB"
                        select="/metadata/idinfo[1]/citation[1]/citeinfo[1]/onlink[1]"/>
                    <xsl:variable name="postSlashA" select="substring-after($FullLinkB,'tiger')"/>
                    <xsl:variable name="tlSpot" select="substring-after($postSlashA,'tl_')"/>
                    <xsl:variable name="tlSpotLength" select="string-length($tlSpot)"/>
                    
                    
                    <xsl:choose>
                        <xsl:when test="$tlSpotLength >0"> 
                            
                            <xsl:variable name="year"  select="substring-before($tlSpot,'_')"/>
                            <xsl:variable name="TlYear" select="concat('TL',$year)"/>
                            
                            
                            <xsl:variable name="FullLinkNew"
                                select="/metadata/idinfo[1]/citation[1]/citeinfo[1]/onlink[1]"/>
                            <xsl:variable name="postDash" select="substring-after($FullLinkNew,'_')"/>
                            <xsl:variable name="yearB" select="substring-before($postDash,'_')"/>
                            <xsl:variable name="postDashB" select="substring-after($postDash,'_')"/>
                            <xsl:variable name="postDashC" select="substring-after($postDashB,'_')"/>
                            <xsl:variable name="Theme" select="substring-before($postDashC,'.')"/>
                            <xsl:variable name="filename" select="concat('SeriesCollection_tl_', $yearB, '_',$Theme,'.shp.xml')"/>
                            
                            <xsl:variable name="path" select="concat($FTP,$Theme)"/>
                            
                            <xsl:value-of select="$path"/>
                        </xsl:when>
                    </xsl:choose>
                    </xsl:element>
                
            
       <!--          <xsl:element name="onlink">
                   <xsl:variable name="WebLink">http://web8.geo.census.gov:7777/metaFiles/existing/decennial/GEO/GPMB/TIGERline/</xsl:variable>
                   <xsl:variable name="FullLinkB"
                        select="/metadata/idinfo[1]/citation[1]/citeinfo[1]/onlink[1]"/>
                      <xsl:variable name="postSlashA" select="substring-after($FullLinkB,'tiger')"/>
                      <xsl:variable name="tlSpot" select="substring-after($postSlashA,'tl_')"/>
                      <xsl:variable name="tlSpotLength" select="string-length($tlSpot)"/>
              

                    

                     
                     <xsl:choose>
                         <xsl:when test="$tlSpotLength >0"> 
                           
                             <xsl:variable name="year"  select="substring-before($tlSpot,'_')"/>
                             <xsl:variable name="TlYear" select="concat('TL',$year)"/>
                             
                    
                               <xsl:variable name="FullLinkNew"
                                 select="/metadata/idinfo[1]/citation[1]/citeinfo[1]/onlink[1]"/>
                             <xsl:variable name="postDash" select="substring-after($FullLinkNew,'_')"/>
                             <xsl:variable name="yearB" select="substring-before($postDash,'_')"/>
                             <xsl:variable name="postDashB" select="substring-after($postDash,'_')"/>
                             <xsl:variable name="postDashC" select="substring-after($postDashB,'_')"/>
                             <xsl:variable name="Theme" select="substring-before($postDashC,'.')"/>
                               <xsl:variable name="filename" select="concat('SeriesCollection_tl_', $yearB, '_',$Theme,'.shp.xml')"/>
                          
                             <xsl:variable name="path" select="concat($WebLink,$TlYear,'/FGDC/',$Theme)"/>
                            
                             <xsl:value-of select="$path"/>
                             </xsl:when>
                     </xsl:choose>
                     

                 </xsl:element> -->
                <xsl:comment>The location of this collection on the Census TIGER website</xsl:comment>
                <xsl:element name="onlink">
                    <xsl:variable name="FullLink"
                        select="/metadata/idinfo[1]/citation[1]/citeinfo[1]/onlink[1]"/>
                    <xsl:variable name="Onlink1" select="substring-before($FullLink,'_')"/>
                    <xsl:variable name="OnlinkLength" select="string-length($Onlink1)-2"/>
                    <xsl:variable name="FinalOnlink" select="substring($Onlink1,0,$OnlinkLength)"/>
                    <xsl:value-of select="$FinalOnlink"/>
                </xsl:element>
          <xsl:comment>This data can also be viewed with TIGERWeb</xsl:comment>

                <xsl:element name="onlink"
                    >http://tigerweb.geo.census.gov/tigerwebmain/TIGERweb_main.html</xsl:element>
            </xsl:element>

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

    <xsl:template name="Description">

        <xsl:element name="descript">

            <xsl:element name="abstract">
                <xsl:value-of select="/metadata/idinfo/descript/abstract"/>
            </xsl:element>

            <xsl:element name="purpose">
                <xsl:value-of select="/metadata/idinfo/descript/purpose"/>
            </xsl:element>

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

    <xsl:template name="TimePeriod">
        <xsl:element name="timeperd">

            <xsl:element name="timeinfo">
                <xsl:element name="rngdates">

                    <xsl:element name="begdate">
                        <xsl:value-of select="/metadata/idinfo/timeperd/timeinfo/rngdates/begdate"/>
                    </xsl:element>

                    <xsl:element name="enddate">
                        <xsl:value-of select="/metadata/idinfo/timeperd/timeinfo/rngdates/enddate"/>
                    </xsl:element>

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

            <xsl:element name="current">
                <xsl:value-of select="/metadata/idinfo/timeperd/current"/>
            </xsl:element>
        </xsl:element>
    </xsl:template>

    <xsl:template name="Status">
        <xsl:element name="status">

            <xsl:element name="progress">
                <xsl:value-of select="/metadata/idinfo/status/progress"/>
            </xsl:element>

            <xsl:element name="update">
                <xsl:value-of select="/metadata/idinfo/status/update"/>
            </xsl:element>

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

    <xsl:template name="BoundingCoordinates">
        <xsl:element name="spdom">
            <xsl:element name="bounding">
                <xsl:element name="westbc">-179.231086</xsl:element>
                <xsl:element name="eastbc">179.859681</xsl:element>
                <xsl:element name="northbc">71.441059</xsl:element>
                <xsl:element name="southbc">-14.601813</xsl:element>
            </xsl:element>
        </xsl:element>
    </xsl:template>

    <xsl:template name="Keywords">
        <xsl:element name="keywords">

            <xsl:for-each select="//metadata/idinfo/keywords/theme">
                <xsl:element name="theme">

                    <xsl:element name="themekt">
                        <xsl:value-of select="./themekt"/>
                    </xsl:element>

                    <xsl:for-each select="./themekey">
                        <xsl:element name="themekey">
                            <xsl:value-of select="."/>
                        </xsl:element>
                    </xsl:for-each>
                </xsl:element>
            </xsl:for-each>

            <xsl:element name="place">
                <xsl:element name="placekt">
                    <xsl:value-of select="/metadata/idinfo[1]/keywords[1]/place[1]/placekt[1]"/>
                </xsl:element>
                <xsl:element name="placeke">
                    <xsl:value-of select="/metadata/idinfo[1]/keywords[1]/place[1]/placekey[1]"/>
                </xsl:element>
                <xsl:element name="placekey">
                    <xsl:value-of select="/metadata/idinfo[1]/keywords[1]/place[1]/placekey[2]"/>
                </xsl:element>
                <xsl:element name="placekey">
                    <xsl:value-of select="/metadata/idinfo[1]/keywords[1]/place[1]/placekey[3]"/>
                </xsl:element>
            </xsl:element>
        </xsl:element>
    </xsl:template>

    <xsl:template name="Constraints">

        <xsl:element name="accconst">
            <xsl:value-of select="/metadata/idinfo/accconst"/>
        </xsl:element>

        <xsl:element name="useconst">
            <xsl:value-of select="/metadata/idinfo/useconst"/>
        </xsl:element>
    </xsl:template>

    <xsl:template name="PointOfContact">
        <xsl:element name="ptcontac">
            <xsl:element name="cntinfo">

                <xsl:element name="cntorgp">
                    <xsl:element name="cntorg">
                        <xsl:value-of select="/metadata/idinfo/ptcontac/cntinfo/cntorgp/cntorg"/>
                    </xsl:element>
                </xsl:element>

                <xsl:element name="cntaddr">
                    <xsl:element name="addrtype">
                        <xsl:value-of select="/metadata/idinfo/ptcontac/cntinfo/cntaddr/addrtype"/>
                    </xsl:element>

                    <xsl:element name="address">
                        <xsl:value-of select="/metadata/idinfo/ptcontac/cntinfo/cntaddr/address"/>
                    </xsl:element>

                    <xsl:element name="city">
                        <xsl:value-of select="/metadata/idinfo/ptcontac/cntinfo/cntaddr/city"/>
                    </xsl:element>

                    <xsl:element name="state">
                        <xsl:value-of select="/metadata/idinfo/ptcontac/cntinfo/cntaddr/state"/>
                    </xsl:element>

                    <xsl:element name="postal">
                        <xsl:value-of select="/metadata/idinfo/ptcontac/cntinfo/cntaddr/state"/>
                    </xsl:element>

                    <xsl:element name="country">
                        <xsl:value-of select="/metadata/idinfo/ptcontac/cntinfo/cntaddr/postal"/>
                    </xsl:element>
                </xsl:element>

                <xsl:element name="cntvoice">
                    <xsl:value-of select="/metadata/idinfo/ptcontac/cntinfo/cntvoice"/>
                </xsl:element>

                <xsl:element name="cntfax">
                    <xsl:value-of select="/metadata/idinfo/ptcontac/cntinfo/cntfax"/>
                </xsl:element>

                <xsl:element name="cntemail">
                    <xsl:value-of select="/metadata/idinfo/ptcontac/cntinfo/cntemail"/>
                </xsl:element>
            </xsl:element>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>
