<?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"
    xmlns:gfc="http://www.isotc211.org/2005/gfc" xmlns:gco="http://www.isotc211.org/2005/gco"
    xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:gml="http://www.opengis.net/gml/3.2"
    exclude-result-prefixes="xs xd" version="2.0">
    <xsl:import href="./UniversalUUIDTemplates.xsl"/>
    <xd:doc scope="stylesheet">
        <xd:desc>
            <xd:p><xd:b>Created on:</xd:b> May 20, 2014</xd:p>
            <xd:p><xd:b>Author:</xd:b> matt</xd:p>
            <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="MetaUUIDS">
            <xsl:for-each select="/dataroot/RangeDomains">
                <xsl:comment> The attribute Begins here!!!!!!!!!</xsl:comment>
                <xsl:element name="gfc:carrierOfCharacteristics">
                    <xsl:element name="gfc:FC_FeatureAttribute">
                        
                        <xsl:element name="gfc:memberName">
                            <xsl:element name="gco:LocalName"><xsl:value-of select="./Attribute_Label"/> </xsl:element>
                        </xsl:element>
                        
                        <xsl:element name="gfc:definition">
                            <xsl:element name="gco:CharacterString"><xsl:value-of select="./Attribute_Definition"/> </xsl:element>
                        </xsl:element>
                        
                        <xsl:call-template name="cardinality"/>
                        <xsl:call-template name="AttDefSource"/>
                        
                        <xsl:if test="./Attribute_Units_of_Measure">
                            <xsl:variable name="AttUnits" select="./Attribute_Units_of_Measure"/>
                            <xsl:variable name="AttLabel" select="./Attribute_Label"/>
                            <xsl:choose>
                              
                                <xsl:when test="contains($AttUnits,'square meters')">
                                    <xsl:variable name="gmlidDesc" select="concat('areaInMetersSquaredfor', $AttLabel)"/>
                                    <xsl:element name="gfc:valueMeasurementUnit">
                                        
                                        <xsl:element name="gml:DerivedUnit">
                                            <xsl:attribute name="gml:id"><xsl:value-of select="$gmlidDesc"/></xsl:attribute>
                                        <xsl:element name="gml:identifier">
                                            <xsl:attribute name="codeSpace">area</xsl:attribute>
                                        </xsl:element>
                                            <xsl:element name="gml:derivationUnitTerm">
                                                <xsl:attribute name="uom">m</xsl:attribute>
                                                <xsl:attribute name="exponent">2</xsl:attribute>
                                            </xsl:element>
                                        </xsl:element>
                                        
                                        
                                    </xsl:element>
                                </xsl:when>
                                <xsl:when test="contains($AttUnits,'Decimal degrees')">
                                    
                                    <xsl:comment>
                                        Do the following to convert from decimal degrees to degree minutes and seconds 
                                            1. The whole units of degrees will remain the same
                                            2. Multiply the decimal by 60 
                                            3. The whole number becomes the minutes 
                                            4. Take the remaining decimal and multiply by 60
                                            5. The resulting number becomes the seconds  Seconds can remain as a decimal.
                                            6. Take your three sets of numbers and put them together, using the symbols for degrees  minutes  and seconds 
                                    </xsl:comment>
                                    
                                    <xsl:element name="gml:BaseUnit">
                                        <xsl:attribute name="gml:id"><xsl:value-of select="$AttLabel"/></xsl:attribute>
                                        <xsl:element name="gml:identifier">
                                            <xsl:attribute name="codeSpace"><xsl:value-of select="$AttLabel"/></xsl:attribute>
                                            <xsl:value-of select="$AttUnits"/>
                                        </xsl:element>
                                        <xsl:element name="gml:unitsSystem">
                                            <xsl:attribute name="xlink:href">http://www.bipm.org/en/si/si_brochure/chapter4/table6.html</xsl:attribute>
                                        </xsl:element>
                                    </xsl:element>
                                </xsl:when>
                            </xsl:choose>
                        </xsl:if>
                        
                        <xsl:element name="gfc:listedValue">
                            <xsl:element name="gfc:FC_ListedValue">
                                <xsl:element name="gfc:label">
                                    <xsl:attribute name="gco:nilReason">inapplicable</xsl:attribute>
                                </xsl:element>
                                
                                <xsl:element name="gfc:definition">
                                    <xsl:element name="gco:CharacterString">
                                        Range Domain Minimum:<xsl:value-of select="./Range_Domain_Minimum"/>
                                        Range Domain Maximum:<xsl:value-of select="./Range_Domain_Maximum"/>
                                    </xsl:element>
                                </xsl:element>
                            </xsl:element>
                        </xsl:element>
                
                    </xsl:element>
                </xsl:element>
            </xsl:for-each>
        
        </xsl:element>
    </xsl:template>
    
    <xsl:template name="AttDefSource">
        <xsl:variable name="AttDefSource" select="./Attribute_Definition_Source"/>
        <xsl:choose>
            <xsl:when test="contains($AttDefSource,'U.S. Census Bureau')">
                <xsl:call-template name="Census"/>
            </xsl:when>
            <xsl:when test="contains($AttDefSource, 'U.S. Postal Service')">
                <xsl:call-template name="USPS"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:element name="gfc:definitionReference">
                    <xsl:element name="gfc:FC_DefinitionSource">
                        <xsl:element name="gfc:source">
                            <xsl:element name="gmd:CI_Citation">
                                
                                <xsl:call-template name="gmdTitle"/>
                                <xsl:call-template name="gmdDate"/>
                                
                                
                                <xsl:element name="gmd:citedResponsibleParty">
                                    <xsl:element name="gmd:CI_ResponsibleParty">
                                        <xsl:element name="gmd:organisationName">
                                            <xsl:element name="gco:CharacterString"><xsl:value-of select="$AttDefSource"/></xsl:element>
                                        </xsl:element>
                                        <xsl:call-template name="gmdRole"/>
                                        
                                    </xsl:element>
                                </xsl:element>
                                
                            </xsl:element>
                        </xsl:element>
                    </xsl:element>
                    
                </xsl:element>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
   
    
</xsl:stylesheet>