<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gfc="http://www.isotc211.org/2005/gfc"
    xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:gmx="http://www.isotc211.org/2005/gmx"
    xmlns:gsr="http://www.isotc211.org/2005/gsr" xmlns:gss="http://www.isotc211.org/2005/gss"
    xmlns:gts="http://www.isotc211.org/2005/gts" xmlns:gml="http://www.opengis.net/gml/3.2"
    xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" xmlns:fn="http://www.w3.org/2005/xpath-functions"
    xmlns:grp="http://www.altova.com/Mapforce/grouping"
    xmlns:mdb="	http://www.isotc211.org/schemas/2012/mdb"
    xmlns:ci="http://www.isotc211.org/schemas/2012/ci"
    exclude-result-prefixes="fn grp xs xsi xsl xd" xmlns="http://www.isotc211.org/2005/gfc">

    <xd:doc scope="stylesheet">
        <xd:desc>
            <xd:p><xd:b>Created on:</xd:b> Jun 21, 2013</xd:p>
            <xd:p><xd:b>Author:</xd:b> Matthew McCready</xd:p>
            <xd:p> This stylesheet replaces the 02_gmd_language and 03_gmd_chararacterSet stylesheet. This change allows the  description of the character set
            along with the language. There is also an option to describe the country of the language.</xd:p>
        </xd:desc>
    </xd:doc>


    <xsl:template name="defaultLocale">
        <xsl:element name="mdb:defaultLocale">
            <xsl:element name="mdb:PT_Locale">


                <xsl:element name="mdb:language">
                    <xsl:element name="mdb:language">
                        <xsl:choose>
                            <xsl:when test="/MIF/Metadata_Language">
                                <xsl:element name="gco:CharacterString">
                                    <xsl:apply-templates select="//MIF/Metadata_Language"/>
                                </xsl:element>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:element name="gco:CharacterString">eng</xsl:element>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:element>
                </xsl:element>

                <xsl:element name="mdb:country">
                    <xsl:element name="gco:CharacterString">US</xsl:element>
                </xsl:element>

                <xsl:element name="mdb:characterEncoding">
                    <xsl:if test="/MIF/Metadata_Character_Set">

                        <xsl:element name="gmd:characterSet">
                            <xsl:choose>
                                <xsl:when test="/MIF/Metadata_Character_Set">
                                    <xsl:element name="gmd:MD_CharacterSetCode">
                                        <xsl:attribute name="codeList">http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_CharacterSetCode</xsl:attribute>
                                        <xsl:attribute name="codeListValue">
                                            <xsl:apply-templates
                                                select="/MIF/Metadata_Character_Set"/>
                                        </xsl:attribute>
                                        <xsl:apply-templates select="/MIF/Metadata_Character_Set"/>
                                    </xsl:element>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:element name="gmd:MD_CharacterSetCode">
                                        <xsl:attribute name="codeList">http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_CharacterSetCode</xsl:attribute>
                                        <xsl:attribute name="codeListValue">8859part1</xsl:attribute>
                                        8859part1
                                    </xsl:element>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:element>

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


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