<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:asn="http://purl.org/ASN/schema/core/" 
    xmlns:cc="http://creativecommons.org/ns#" 
    xmlns:dc="http://purl.org/dc/elements/1.1/" 
    xmlns:dcterms="http://purl.org/dc/terms/" 
    xmlns:foaf="http://xmlns.com/foaf/0.1/" 
    xmlns:gemq="http://purl.org/gem/qualifiers/" 
    xmlns:loc="http://www.loc.gov/loc.terms/relators/" 
    xmlns:owl="http://www.w3.org/2002/07/owl#" 
    xmlns:skos="http://www.w3.org/2004/02/skos/core#" 
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xml="http://www.w3.org/XML/1998/namespace"
    xmlns="http://purl.org/net/inloc/"
    version="1.0"
    exclude-result-prefixes="rdf asn cc dc dcterms foaf gemq loc owl skos rdfs">
    <xsl:output method="xml" encoding="utf-8" omit-xml-declaration="no" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="rdf:RDF">
        <xsl:processing-instruction name="xml-stylesheet">href="http://purl.org/net/inloc/inloc-xml-to-html.xsl" type="text/xsl"</xsl:processing-instruction>
        <xsl:element name="LOCstructure">
            
            <xsl:attribute name="xsi:schemaLocation">http://purl.org/net/inloc/ http://purl.org/net/inloc/inloc_schema.xsd</xsl:attribute>
            <xsl:attribute name="id">
                <xsl:value-of select="rdf:Description/foaf:primaryTopic/@rdf:resource"/>
            </xsl:attribute>
            <xsl:attribute name="xml:lang">
                <xsl:value-of select="substring(substring-after(asn:StandardDocument/dcterms:language/@rdf:resource, 'http://id.loc.gov/vocabulary/iso639-2/'),1,2)"/>
            </xsl:attribute>

            <xsl:apply-templates select="rdf:Description"/>
            <xsl:apply-templates select="asn:StandardDocument"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="rdf:Description">
        <title><xsl:value-of select="../asn:StandardDocument/dc:title"/></title>
        <description><xsl:value-of select="../asn:StandardDocument/dcterms:description"/></description>
        <furtherInformation><xsl:value-of select="foaf:primaryTopic/@rdf:resource"/></furtherInformation>
        <rights><xsl:value-of select="cc:license/@rdf:resource"/></rights>
        <created><xsl:value-of select="dcterms:created"/></created>
        <modified><xsl:value-of select="dcterms:modified"/></modified>
        <version><xsl:value-of select="asn:exportVersion/@rdf:resource"/></version>

        <LOCassociation type="http://purl.org/net/inloc/by">
            <xsl:element name="subject">
                <xsl:attribute name="id"><xsl:value-of select="foaf:primaryTopic/@rdf:resource"/></xsl:attribute>
            </xsl:element>
            <scheme id="http://purl.org/net/inloc/publisher">
                <label>Publisher</label>
            </scheme>
            <object id="">
                <label><xsl:value-of select="cc:attributionName"/></label>
            </object>
        </LOCassociation>
    </xsl:template>
    
    <xsl:template match="asn:StandardDocument">
        <xsl:for-each select="dcterms:educationLevel">
            <LOCassociation type="http://purl.org/net/inloc/level">
                <xsl:element name="subject">
                    <xsl:attribute name="id"><xsl:value-of select="../@rdf:about"/></xsl:attribute>
                </xsl:element>
                <xsl:element name="scheme">
                    <xsl:attribute name="id">http://purl.org/ASN/scheme/ASNEducationLevel</xsl:attribute>
                    <label>ASNEducationLevel</label>
                </xsl:element>
                <xsl:element name="object">
                    <xsl:attribute name="id"><xsl:value-of select="@rdf:resource"/></xsl:attribute>
                    <label>ASNEducationLevel <xsl:value-of select="substring-after(@rdf:resource,'http://purl.org/ASN/scheme/ASNEducationLevel/')"/></label>
                </xsl:element>
                <xsl:if test="substring-after(@rdf:resource,'http://purl.org/ASN/scheme/ASNEducationLevel/') &gt;= 0">
                    <number>
                        <xsl:value-of select="substring-after(@rdf:resource,'http://purl.org/ASN/scheme/ASNEducationLevel/')"/>
                    </number>
                </xsl:if>
            </LOCassociation>
        </xsl:for-each>
        <xsl:for-each select="gemq:hasChild">
            <LOCassociation type="http://purl.org/net/inloc/LOCrel">
                <xsl:element name="subject">
                    <xsl:attribute name="id"><xsl:value-of select="../asn:identifier/@rdf:resource"/></xsl:attribute>
                </xsl:element>
                <scheme id="http://purl.org/net/inloc/hasLOCpart"></scheme>
                <xsl:element name="object">
                    <xsl:attribute name="id"><xsl:value-of select="@rdf:resource"/></xsl:attribute>
                </xsl:element>
            </LOCassociation>
            <xsl:call-template name="asn:Statement">
                <xsl:with-param name="id"><xsl:value-of select="@rdf:resource"/></xsl:with-param>
            </xsl:call-template>
        </xsl:for-each>
    </xsl:template>


    <xsl:template name="asn:Statement">
        <xsl:param name="id"/>
        <xsl:for-each select="/rdf:RDF/asn:Statement[@rdf:about=$id]">
            <xsl:element name="LOCdefinition">
                <xsl:attribute name="id"><xsl:value-of select="$id"/></xsl:attribute>
                <description><xsl:value-of select="dcterms:description"/></description>
            </xsl:element>
        </xsl:for-each>
        <xsl:for-each select="/rdf:RDF/asn:Statement[@rdf:about=$id]/gemq:hasChild">
            <LOCassociation type="http://purl.org/net/inloc/LOCrel">
                <xsl:element name="subject">
                    <xsl:attribute name="id"><xsl:value-of select="$id"/></xsl:attribute>
                </xsl:element>
                <scheme id="http://purl.org/net/inloc/hasLOCpart"></scheme>
                <xsl:element name="object">
                    <xsl:attribute name="id"><xsl:value-of select="@rdf:resource"/></xsl:attribute>
                </xsl:element>
            </LOCassociation>
            <xsl:call-template name="asn:Statement">
                <xsl:with-param name="id"><xsl:value-of select="@rdf:resource"/></xsl:with-param>
            </xsl:call-template>
        </xsl:for-each>
    </xsl:template>
    
</xsl:stylesheet>