<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    exclude-result-prefixes="xs xd" version="2.0">
    <xd:doc scope="stylesheet">
        <xd:desc>
            <xd:p><xd:b>Created on:</xd:b> Feb 17, 2015</xd:p>
            <xd:p><xd:b>Author:</xd:b> matt</xd:p>
            <xd:p/>
        </xd:desc>
    </xd:doc>

    <xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes"/>
    <xsl:template match="/">
        <xsl:element name="PODVer1_1">
            <xsl:element name="type">dcat:Dataset</xsl:element>
            <xsl:element name="title">
                <xsl:value-of select="/MRF/Identification_Information/Citation/Title"/>
            </xsl:element>
            <xsl:element name="description">
                <xsl:value-of select="/MRF/Identification_Information/Description/Abstract"/>
            </xsl:element>

            <xsl:element name="keyword">

                <xsl:for-each select="/MRF/Identification_Information/Keywords/Theme/Theme_Keyword">
                    <xsl:element name="keywordItem">
                        <xsl:value-of select="."/>
                    </xsl:element>
                </xsl:for-each>
                <xsl:for-each select="/MRF/Identification_Information/Keywords/Place/Place_Keyword">
                    <xsl:element name="keywordItem">
                        <xsl:value-of select="."/>
                    </xsl:element>
                </xsl:for-each>

            </xsl:element>


            <xsl:element name="modified">
                <xsl:value-of
                    select="/MRF/Identification_Information[1]/Citation[1]/Publication_Date[1]"/>
            </xsl:element>

            <xsl:element name="publisher">
                <!-- <xsl:value-of select="/MRF/Identification_Information/Citation/Originator"/> -->
                <xsl:variable name="fullName" select="/MRF/Identification_Information/Citation/Originator"/>
                <xsl:variable name="dept" select="substring-before($fullName, ',')"/>
                <xsl:variable name="postDept" select="substring-after($fullName,',')"/>
                <xsl:variable name="Bureau" select="substring-before($postDept,',')"/>
                <xsl:variable name="DivBranch" select="substring-after($postDept,',')"/>

                <xsl:choose>
                    <xsl:when test="contains($DivBranch,'/')">
                        <xsl:variable name="Div" select="substring-before($DivBranch,'/')"/>
                        <xsl:variable name="Branch" select="substring-after($DivBranch, '/')"/>

                        <xsl:element name="name">
                            <xsl:value-of select="$Branch"/>
                        </xsl:element>
                        <xsl:element name="subOrganizationOf">
                            <xsl:value-of select="$Div"/>
                        </xsl:element>
                        <xsl:element name="subOrganizationOf">
                            <xsl:value-of select="$Bureau"/>
                        </xsl:element>
                        <xsl:element name="subOrganizationOf">
                            <xsl:value-of select="$dept"/>
                        </xsl:element>

                    </xsl:when>
                    <xsl:otherwise>
                        <!--  <xsl:comment>In the otherwise!!!!!!!!!!!!!!!!!!!!!!!!!!!</xsl:comment> -->
                        <xsl:element name="name">
                            <xsl:value-of select="$DivBranch"/>
                        </xsl:element>
                        <xsl:element name="subOrganizationOf">
                            <xsl:value-of select="$Bureau"/>
                        </xsl:element>
                        <xsl:element name="subOrganizationOf">
                            <xsl:value-of select="$dept"/>
                        </xsl:element>
                    </xsl:otherwise>
                </xsl:choose>



                <!--   <xsl:comment>fullName:<xsl:value-of select="$fullName"/></xsl:comment>
                <xsl:comment>dept: <xsl:value-of select="$dept"/></xsl:comment>
                <xsl:comment>postDept<xsl:value-of select="$postDept"/></xsl:comment>
                <xsl:comment>Bureau: <xsl:value-of select="$Bureau"/> </xsl:comment>
                <xsl:comment> DivBrach<xsl:value-of select="$DivBranch"/></xsl:comment>-->

            </xsl:element>

            <xsl:element name="contactPoint">
                <xsl:element name="fn">
                    <xsl:value-of
                        select="/MRF/Identification_Information[1]/Citation[1]/Originator[1]"/>
                </xsl:element>
                <xsl:element name="hasEmail">
                    <xsl:value-of
                        select="/MRF/Identification_Information/Point_of_Contact[1]/Contact_Electronic_Mail_Address[1]"/>
                </xsl:element>
            </xsl:element>

            <xsl:element name="identifier">
                <xsl:value-of select="/MRF/Metadata_Reference_Information/Metadata_File_Identifier"/>
            </xsl:element>
            <xsl:element name="accessLevel">public</xsl:element>
            <xsl:element name="bureauCode">006:07</xsl:element>
            <xsl:element name="programCode">006-010</xsl:element>

            <xsl:element name="spatial">
                <xsl:element name="spatialType">type</xsl:element>
                <xsl:element name="Polygon">Polygon</xsl:element>
                <xsl:element name="coordinates">coordinates</xsl:element>

                <xsl:element name="pair1">
                    <xsl:element name="west">
                        <xsl:value-of select="/MRF/Identification_Information/Spatial_Domain/West_Bounding_Coordinate"/>
                    </xsl:element>
                    <xsl:element name="north">
                        <xsl:value-of select="/MRF/Identification_Information/Spatial_Domain/North_Bounding_Coordinate"/>
                    </xsl:element>
                </xsl:element>

                <xsl:element name="pair2">
                    <xsl:element name="east">
                        <xsl:value-of select="/MRF/Identification_Information[1]/Spatial_Domain[1]/East_Bounding_Coordinate[1]"/>
                    </xsl:element>
                    <xsl:element name="south">
                        <xsl:value-of select="/MRF/Identification_Information[1]/Spatial_Domain[1]/South_Bounding_Coordinate[1]"/>
                    </xsl:element>
                </xsl:element>

                <xsl:element name="pair3">
                    <xsl:element name="east">
                        <xsl:value-of select="/MRF/Identification_Information[1]/Spatial_Domain[1]/East_Bounding_Coordinate[1]"/>
                    </xsl:element>
                    <xsl:element name="north">
                        <xsl:value-of select="/MRF/Identification_Information/Spatial_Domain/North_Bounding_Coordinate"/>
                    </xsl:element>
                </xsl:element>

                <xsl:element name="pair4">
                    <xsl:element name="west">
                        <xsl:value-of select="/MRF/Identification_Information/Spatial_Domain/West_Bounding_Coordinate"/>
                    </xsl:element>
                    <xsl:element name="south">
                        <xsl:value-of select="/MRF/Identification_Information[1]/Spatial_Domain[1]/South_Bounding_Coordinate[1]"/>
                    </xsl:element>
                </xsl:element>

                <xsl:element name="pair5">
                    <xsl:element name="west">
                        <xsl:value-of select="/MRF/Identification_Information/Spatial_Domain/West_Bounding_Coordinate"/>
                    </xsl:element>
                    <xsl:element name="north">
                        <xsl:value-of select="/MRF/Identification_Information/Spatial_Domain/North_Bounding_Coordinate"/>
                    </xsl:element>
                </xsl:element>


            </xsl:element>

            <xsl:element name="temporal">
                <xsl:element name="begin">
                    <xsl:value-of select="/MRF/Identification_Information/Time_Period_of_Content/Beginning_Date"/>
                </xsl:element>
                <xsl:element name="end">
                    <xsl:value-of select="/MRF/Identification_Information/Time_Period_of_Content/Ending_Date"/>
                </xsl:element>
            </xsl:element>

            <xsl:element name="accrualPeriodicity">irregular</xsl:element>

            <xsl:element name="language">en-US</xsl:element>

            <xsl:element name="distribution">
                <xsl:element name="type">dcat:Distribution</xsl:element>
                <xsl:element name="downloadURL">
                    <xsl:value-of select="/MRF/Distribution_Information/Standard_Order_Process/Digital_Form/Digital_Transfer_Option/Network_Resource_Name"/>
                </xsl:element>
                <xsl:element name="mediaType">
                    <xsl:value-of select="/MRF/Distribution_Information/Standard_Order_Process/Digital_Form/File_Decompression_Technique"/>
                </xsl:element>
                <xsl:element name="distTitle">
                    <xsl:variable name="URL" select="/MRF/Distribution_Information/Standard_Order_Process/Digital_Form/Digital_Transfer_Option/Network_Resource_Name"/>
                    <xsl:variable name="postG" select="substring-after($URL,'GENZ2014/')"/>
                    <xsl:value-of select="$postG"/>
                </xsl:element>
            </xsl:element>

            <xsl:variable name="title" select="/MRF/Identification_Information/Citation/Title"/>
            <xsl:choose>
                <xsl:when test="contains($title,'Series Information')"/>
                <xsl:otherwise>
                 <!--    <xsl:element name="isPartOf"> -->
                        <xsl:variable name="SeriesString">Series Information for the </xsl:variable>

                        <xsl:choose>
                            <!--         <xsl:when test="contains($title,'Nation')">
                        <xsl:variable name="preComma" select="substring-before($title,',')"/>
                        <xsl:variable name="post" select="substring-after($title,'for')"></xsl:variable>
                        <xsl:variable name="SeriesInfoTitle" select="concat($SeriesString,$preComma,$post)"/>
                        <xsl:value-of select="$SeriesInfoTitle"/>
                    </xsl:when>
                    <xsl:when test="contains($title,'United States')">
                        <xsl:variable name="preComma" select="substring-before($title,',')"/>
                        <xsl:variable name="post" select="substring-after($title,'for')"></xsl:variable>
                        <xsl:variable name="SeriesInfoTitle" select="concat($SeriesString,$preComma,$post)"/>
                        <xsl:value-of select="$SeriesInfoTitle"/>
                    </xsl:when> -->
                            <xsl:when test="contains($title,'State')">

                                <xsl:choose>
                                    <xsl:when test="contains($title,'Cartographic')">
                                        <xsl:variable name="preComma" select="substring-before($title,',')"/>
                                        <xsl:variable name="postState" select="substring-after($title,'State-')"/>
                                        <xsl:variable name="preFor" select="substring-before($postState,'for')"/>
                                        <xsl:variable name="postFor" select="substring-after($postState,'for')"/>
                                        <xsl:variable name="scale" select="substring-after($postFor,',')"/>
                                        <xsl:variable name="SeriesInfoTitle" select="concat($SeriesString,'2014 ',$preFor,$scale)"/>
                                        <xsl:element name="isPartOf">
                                        <xsl:value-of select="$SeriesInfoTitle"/>
                                        </xsl:element>    
                                    </xsl:when>
                                </xsl:choose>

                            </xsl:when>
                            <xsl:when test="contains($title,'county')">
                                <xsl:choose>
                                    <xsl:when test="contains($title,'TIGER')">
                                <xsl:variable name="preComma" select="substring-before($title,',')"/>
                                <xsl:variable name="postCounty" select="substring-after($title,'county,')"/>
                               <!--   <xsl:comment>post County:<xsl:value-of select="$postCounty"/></xsl:comment>-->
                                <xsl:variable name="postcomma1" select="substring-after($postCounty,',')"/>
                                <xsl:variable name="theme" select="substring-after($postcomma1,',')"/>
                                <!-- <xsl:comment>theme:<xsl:value-of select="$theme"/></xsl:comment>   -->      
                                <xsl:variable name="SeriesInfoTitle" select="concat($SeriesString,'2014 ',$theme)"/>
                                        <xsl:element name="isPartOf">
                                <xsl:value-of select="$SeriesInfoTitle"/>
                                      </xsl:element>      
                                    </xsl:when>
                                </xsl:choose>
                            </xsl:when>
                        </xsl:choose>

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

        </xsl:element>

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