<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="http://purl.org/net/inloc/"
    xmlns:inloc="http://purl.org/net/inloc/" version="2.0" exclude-result-prefixes="xsi xml">
    <xsl:output method="text" encoding="utf-8" omit-xml-declaration="yes" indent="no"/>
    <xsl:strip-space elements="*"/>

    <xsl:variable name="ns" select="translate(tokenize(inloc:LOCstructure/@id,'/')[last()],'#','')"/>
    <xsl:variable name="globalprefix">
        <xsl:choose>
            <xsl:when test="$ns!=''">
                <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                <xsl:value-of select="$ns"/>
                <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text disable-output-escaping="yes">&lt;source&gt;</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="baseuri">
        <xsl:value-of
            select="substring(inloc:LOCstructure/@id,1,string-length(inloc:LOCstructure/@id)-string-length(tokenize(inloc:LOCstructure/@id,'/')[last()]))"
        />
    </xsl:variable>

    <xsl:template match="/inloc:LOCstructure">

        <xsl:for-each select="inloc:LOCassociation[@type='http://purl.org/net/inloc/LOCrel']">
            <xsl:variable name="valschemeid">
                <xsl:value-of select="inloc:scheme/@id"/>
            </xsl:variable>
            <xsl:if test="inloc:scheme/inloc:label">
                <xsl:if
                    test="count(../inloc:LOCassociation[@type='http://purl.org/net/inloc/LOCrel']/inloc:scheme[@id=$valschemeid]/inloc:label)>1">
                    <xsl:if
                        test="not(following-sibling::inloc:LOCassociation[@type='http://purl.org/net/inloc/LOCrel']/inloc:scheme[@id=$valschemeid]/inloc:label!='')">
                        <xsl:if
                            test="not(inloc:scheme[@id=$valschemeid]/inloc:label=preceding-sibling::inloc:LOCassociation[@type='http://purl.org/net/inloc/LOCrel']/inloc:scheme[@id=$valschemeid]/inloc:label)">
                            <xsl:if
                                test="inloc:scheme[@id=$valschemeid]/inloc:label/@xml:lang=preceding-sibling::inloc:LOCassociation[@type='http://purl.org/net/inloc/LOCrel']/inloc:scheme[@id=$valschemeid]/inloc:label/@xml:lang">
                                <xsl:text>## WARNING Test 1 LOCassociation "LOCrel" type: Multiple label for scheme id "</xsl:text>
                                <xsl:value-of select="$valschemeid"/>
                                <xsl:text> - </xsl:text>
                                <xsl:value-of select="inloc:scheme[@id=$valschemeid]/inloc:label"/>
                                <xsl:text> lang( </xsl:text>
                                <xsl:value-of
                                    select="inloc:scheme[@id=$valschemeid]/inloc:label/@xml:lang"/>
                                <xsl:text> ) = </xsl:text>
                                <xsl:value-of
                                    select="preceding-sibling::inloc:LOCassociation[@type='http://purl.org/net/inloc/LOCrel']/inloc:scheme[@id=$valschemeid]/inloc:label"/>
                                <xsl:text> lang( </xsl:text>
                                <xsl:value-of
                                    select="preceding-sibling::inloc:LOCassociation[@type='http://purl.org/net/inloc/LOCrel']/inloc:scheme[@id=$valschemeid]/inloc:label/@xml:lang"/>
                                <xsl:text>)", the last one will be transformed into RDF and all other discarded
</xsl:text>
                            </xsl:if>
                        </xsl:if>
                    </xsl:if>
                </xsl:if>
            </xsl:if>
        </xsl:for-each>

        <xsl:for-each select="inloc:LOCassociation[@type='http://purl.org/net/inloc/by']">
            <xsl:variable name="valschemeid">
                <xsl:value-of select="inloc:scheme/@id"/>
            </xsl:variable>
            <xsl:if test="inloc:scheme/inloc:label">
                <xsl:if
                    test="count(../inloc:LOCassociation[@type='http://purl.org/net/inloc/by']/inloc:scheme[@id=$valschemeid]/inloc:label[@xml:lang=inloc:scheme[@id=$valschemeid]/inloc:label/@xml:lang])>1">
                    <xsl:if
                        test="not(following-sibling::inloc:LOCassociation[@type='http://purl.org/net/inloc/by']/inloc:scheme[@id=$valschemeid]/inloc:label[@xml:lang=inloc:scheme[@id=$valschemeid]/inloc:label/@xml:lang])">
                        <xsl:text>## WARNING Test 2 LOCassociation "by" type: Multiple label for scheme id "</xsl:text>
                        <xsl:value-of select="$valschemeid"/>
                        <xsl:text>", the last one will be transformed into RDF and all other discarded
</xsl:text>
                    </xsl:if>
                </xsl:if>
            </xsl:if>
        </xsl:for-each>

        <xsl:text disable-output-escaping="yes">## prefixes frequently seen elsewhere
@prefix rdf: &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; .
@prefix rdfs: &lt;http://www.w3.org/2000/01/rdf-schema#&gt; .
@prefix xsd: &lt;http://www.w3.org/2001/XMLSchema#&gt; .
</xsl:text>
        <xsl:text disable-output-escaping="yes">## the main inloc prefix for InLOC concepts
@prefix inloc: &lt;http://purl.org/net/inloc/&gt; .
</xsl:text>
        <xsl:text disable-output-escaping="yes">## any more prefixes from xmlns
## guessing base from LOCstructure path (or could work it out)
@base </xsl:text>
        <xsl:text>&lt;</xsl:text>
        <xsl:value-of select="$baseuri"/>
        <xsl:text>&gt; .

</xsl:text>


        <xsl:text disable-output-escaping="yes">
## the main structure
</xsl:text>

        <xsl:call-template name="LOCstructure"/>

    </xsl:template>

    <!-- ********* TEMPLATE LOCstructure ************** -->
    <xsl:template name="LOCstructure">

        <xsl:variable name="LOCstructureid">
            <xsl:value-of
                select="substring(@id,1,string-length(@id)-string-length(tokenize(@id,'/')[last()]))"
            />
        </xsl:variable>

        <xsl:variable name="LOCstructureprefix">
            <!--
                <xsl:value-of select="$globalprefix"/>
                -->
            <xsl:choose>
                <xsl:when test="contains(@id,$baseuri)">
                    <xsl:variable name="idlocal" select="tokenize(@id,'/')[last()]"/>
                    <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                    <xsl:value-of select="$idlocal"/>
                    <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                </xsl:when>
                <xsl:when test="contains(@id,'http')">
                    <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                    <xsl:value-of select="@id"/>
                    <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                </xsl:when>
                <xsl:when test="@id!=''">
                    <xsl:text>_:</xsl:text>
                    <xsl:value-of select="replace(@id,'#','\\#')"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>_:blankstructureobject</xsl:text>
                    <xsl:value-of select="position()"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:value-of select="$LOCstructureprefix"/>
        <xsl:text> a inloc:LOCstructure .
</xsl:text>

        <xsl:call-template name="LOCtypeLOC">
            <xsl:with-param name="LOCprefix" select="$LOCstructureprefix"/>
        </xsl:call-template>

        <xsl:for-each select="inloc:combinationRules">
            <xsl:call-template name="multilinguallabel">
                <xsl:with-param name="id" select="$LOCstructureprefix"/>
                <xsl:with-param name="type" select="' inloc:combinationRules '"/>
            </xsl:call-template>
        </xsl:for-each>

        <xsl:for-each select="inloc:LOCstructure">
            <xsl:call-template name="LOCstructure"/>
        </xsl:for-each>

        <xsl:for-each select="inloc:LOCdefinition">
            <xsl:call-template name="LOCdefinition"/>
        </xsl:for-each>

        <xsl:for-each select="inloc:LOCassociation">
            <xsl:call-template name="LOCassociation"/>
        </xsl:for-each>

    </xsl:template>


    <!-- ********* TEMPLATE LOCtypeLOC ************** -->
    <xsl:template name="LOCtypeLOC">
        <xsl:param name="LOCprefix"/>

        <xsl:if test="@xml:lang!=''">
            <xsl:value-of select="$LOCprefix"/>
            <xsl:text> inloc:language "</xsl:text>
            <xsl:value-of select="@xml:lang"/>
            <xsl:text>" .
</xsl:text>
        </xsl:if>

        <xsl:variable name="LOCstructureid">
            <xsl:value-of
                select="substring(@id,1,string-length(@id)-string-length(tokenize(@id,'/')[last()]))"
            />
        </xsl:variable>

        <xsl:for-each select="inloc:extraID">
            <xsl:value-of select="$LOCprefix"/>
            <xsl:text> inloc:extraID </xsl:text>
            <xsl:choose>
                <xsl:when test="contains(.,$LOCstructureid)">
                    <xsl:variable name="idlocal" select="tokenize(.,'/')[last()]"/>
                    <xsl:value-of select="$ns"/>
                    <xsl:text>:</xsl:text>
                    <xsl:value-of select="$idlocal"/>
                </xsl:when>
                <xsl:when test="contains(.,'http')">
                    <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                    <xsl:value-of select="."/>
                    <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                </xsl:when>
                <xsl:when test=".!=''">
                    <xsl:text>"</xsl:text>
                    <xsl:value-of select="."/>
                    <xsl:text>"</xsl:text>
                </xsl:when>
            </xsl:choose>
            <xsl:text> .
</xsl:text>
        </xsl:for-each>
        <xsl:for-each select="inloc:title">
            <xsl:call-template name="multilinguallabel">
                <xsl:with-param name="id" select="$LOCprefix"/>
                <xsl:with-param name="type" select="' inloc:title '"/>
            </xsl:call-template>
        </xsl:for-each>
        <xsl:for-each select="inloc:abbr">
            <xsl:call-template name="multilinguallabel">
                <xsl:with-param name="id" select="$LOCprefix"/>
                <xsl:with-param name="type" select="' inloc:abbr '"/>
            </xsl:call-template>
        </xsl:for-each>
        <xsl:for-each select="inloc:description">
            <xsl:call-template name="multilinguallabel">
                <xsl:with-param name="id" select="$LOCprefix"/>
                <xsl:with-param name="type" select="' inloc:description '"/>
            </xsl:call-template>
        </xsl:for-each>
        <xsl:for-each select="inloc:furtherInformation">
            <xsl:call-template name="multilinguallabel">
                <xsl:with-param name="id" select="$LOCprefix"/>
                <xsl:with-param name="type" select="' inloc:furtherInformation '"/>
            </xsl:call-template>
        </xsl:for-each>
        <xsl:for-each select="inloc:rights">
            <xsl:call-template name="multilinguallabel">
                <xsl:with-param name="id" select="$LOCprefix"/>
                <xsl:with-param name="type" select="' inloc:rights '"/>
            </xsl:call-template>
        </xsl:for-each>
        <xsl:if test="inloc:created">
            <xsl:value-of select="$LOCprefix"/>
            <xsl:text> inloc:created "</xsl:text>
            <xsl:value-of select="inloc:created"/>
            <xsl:text>" .
</xsl:text>
        </xsl:if>
        <xsl:for-each select="inloc:modified">
            <xsl:value-of select="$LOCprefix"/>
            <xsl:text> inloc:modified "</xsl:text>
            <xsl:value-of select="."/>
            <xsl:text>" .
</xsl:text>
        </xsl:for-each>
        <xsl:if test="inloc:issued">
            <xsl:value-of select="$LOCprefix"/>
            <xsl:text> inloc:issued "</xsl:text>
            <xsl:value-of select="inloc:issued"/>
            <xsl:text>" .
</xsl:text>
        </xsl:if>
        <xsl:if test="inloc:validityStart">
            <xsl:value-of select="$LOCprefix"/>
            <xsl:text> inloc:validityStart "</xsl:text>
            <xsl:value-of select="inloc:validityStart"/>
            <xsl:text>" .
</xsl:text>
        </xsl:if>
        <xsl:if test="inloc:validityEnd">
            <xsl:value-of select="$LOCprefix"/>
            <xsl:text> inloc:validityEnd "</xsl:text>
            <xsl:value-of select="inloc:validityEnd"/>
            <xsl:text>" .
</xsl:text>
        </xsl:if>
        <xsl:if test="inloc:version!=''">
            <xsl:value-of select="$LOCprefix"/>
            <xsl:text> inloc:version "</xsl:text>
            <xsl:value-of select="inloc:version"/>
            <xsl:text>" .
</xsl:text>
        </xsl:if>
    </xsl:template>


    <!-- ******************** TEMPLATE LOCDefinition *************************** -->
    <xsl:template name="LOCdefinition">

        <xsl:variable name="idlocal" select="tokenize(@id,'/')[last()]"/>

        <xsl:variable name="LOCdefprefix">
            <!--
                <xsl:value-of select="$ns"/>
            -->
            <xsl:text>&lt;</xsl:text>
            <xsl:value-of select="$idlocal"/>
            <xsl:text>&gt;</xsl:text>
        </xsl:variable>
        <xsl:value-of select="$LOCdefprefix"/>
        <xsl:text> a inloc:LOCdefinition .
</xsl:text>

        <xsl:call-template name="LOCtypeLOC">
            <xsl:with-param name="LOCprefix" select="$LOCdefprefix"/>
        </xsl:call-template>

        <xsl:variable name="LOCstructureid">
            <xsl:value-of
                select="substring(../@id,1,string-length(../@id)-string-length(tokenize(../@id,'/')[last()]))"
            />
        </xsl:variable>

        <xsl:if test="inloc:primaryStructure">
            <xsl:value-of select="$LOCdefprefix"/>
            <xsl:text> inloc:primaryStructure </xsl:text>
            <xsl:choose>
                <xsl:when test="contains(inloc:primaryStructure,$LOCstructureid)">
                    <xsl:variable name="idlocal"
                        select="tokenize(inloc:primaryStructure,'/')[last()]"/>
                    <xsl:text>&lt;</xsl:text>
                    <xsl:value-of select="$idlocal"/>
                    <xsl:text>&gt;</xsl:text>
                </xsl:when>
                <xsl:when test="contains(inloc:primaryStructure,'http')">
                    <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                    <xsl:value-of select="inloc:primaryStructure"/>
                    <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                </xsl:when>
                <xsl:when test="inloc:primaryStructure!=''">
                    <xsl:text>_:</xsl:text>
                    <xsl:value-of select="replace(inloc:primaryStructure,'#','\\#')"/>
                </xsl:when>
            </xsl:choose>
            <xsl:text> .
</xsl:text>
        </xsl:if>

    </xsl:template>

    <!-- *********** ******** TEMPLATE LOC ASSOCIATION ***************************-->
    <xsl:template name="LOCassociation">
        <xsl:variable name="LOCstructureid">
            <xsl:value-of
                select="substring(../@id,1,string-length(../@id)-string-length(tokenize(../@id,'/')[last()]))"/>
            <!--            <xsl:value-of select="parent::inloc:LOCstructure/@id"/>-->
        </xsl:variable>
        <xsl:variable name="subjectid">
            <xsl:choose>
                <xsl:when test="contains(inloc:subject/@id,$LOCstructureid)">
                    <xsl:variable name="idlocal" select="tokenize(inloc:subject/@id,'/')[last()]"/>
                    <xsl:text>&lt;</xsl:text>
                    <xsl:value-of select="$idlocal"/>
                    <xsl:text>&gt;</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                    <xsl:value-of select="inloc:subject/@id"/>
                    <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:choose>
            <!-- LOC ASSOCIATION BY -->
            <xsl:when test="@type ='http://purl.org/net/inloc/by'">
                <!--                <xsl:value-of select="$LOCstructureid"/>
                <xsl:text> 
                </xsl:text>
                -->
                <xsl:value-of select="$subjectid"/>
                <xsl:text> inloc:</xsl:text>
                <xsl:value-of
                    select="substring-after(inloc:scheme/@id,'http://purl.org/net/inloc/')"/>
                <xsl:text> </xsl:text>
                <xsl:variable name="objectid">
                    <xsl:choose>
                        <xsl:when test="contains(inloc:object/@id,$LOCstructureid)">
                            <xsl:variable name="idlocal"
                                select="tokenize(inloc:object/@id,'/')[last()]"/>
                            <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                            <xsl:value-of select="$idlocal"/>
                            <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                        </xsl:when>
                        <xsl:when test="contains(inloc:object/@id,'http')">
                            <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                            <xsl:value-of select="inloc:object/@id"/>
                            <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                        </xsl:when>
                        <xsl:when test="inloc:object/@id!=''">
                            <xsl:text>_:</xsl:text>
                            <xsl:value-of select="replace(inloc:object/@id,'#','\\#')"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:text>_:blankbyobject</xsl:text>
                            <xsl:value-of select="position()"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:value-of select="$objectid"/>
                <xsl:text> .
</xsl:text>

                <xsl:for-each select="inloc:object/inloc:label">
                    <xsl:call-template name="multilinguallabel">
                        <xsl:with-param name="id" select="$objectid"/>
                        <xsl:with-param name="type" select="' inloc:label '"/>
                    </xsl:call-template>
                </xsl:for-each>

            </xsl:when>

            <!-- LOC ASSOCIATION LOCREL -->
            <xsl:when test="@type ='http://purl.org/net/inloc/LOCrel'">
                <xsl:value-of select="$subjectid"/>
                <xsl:text> inloc:</xsl:text>
                <xsl:value-of
                    select="substring-after(inloc:scheme/@id,'http://purl.org/net/inloc/')"/>
                <xsl:text> </xsl:text>
                <xsl:variable name="objectid">
                    <xsl:choose>
                        <xsl:when test="contains(inloc:object/@id,$LOCstructureid)">
                            <xsl:variable name="idlocal"
                                select="tokenize(inloc:object/@id,'/')[last()]"/>
                            <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                            <xsl:value-of select="$idlocal"/>
                            <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                        </xsl:when>
                        <xsl:when test="contains(inloc:object/@id,'http')">
                            <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                            <xsl:value-of select="inloc:object/@id"/>
                            <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                        </xsl:when>
                        <xsl:when test="inloc:object/@id!=''">
                            <xsl:text>_:</xsl:text>
                            <xsl:value-of select="replace(inloc:object/@id,'#','\\#')"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:text>_:blankLOCRelobject</xsl:text>
                            <xsl:value-of select="position()"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:if test="inloc:object/@id!=''">
                    <xsl:value-of select="$objectid"/>
                    <xsl:text> .
</xsl:text>
                </xsl:if>

                <xsl:variable name="valschemeid">
                    <xsl:value-of select="inloc:scheme/@id"/>
                </xsl:variable>
                <!--                 <xsl:value-of select="$valschemeid"/>
                <xsl:text> .
</xsl:text>
-->
                <xsl:choose>
                    <xsl:when
                        test="following-sibling::inloc:LOCassociation/inloc:scheme[@id=$valschemeid]/inloc:label!=''"/>
                    <xsl:when test="not(inloc:scheme/inloc:label)"/>
                    <xsl:otherwise>
                        <xsl:text>inloc:</xsl:text>
                        <xsl:value-of
                            select="substring-after(inloc:scheme/@id,'http://purl.org/net/inloc/')"/>
                        <xsl:text> inloc:label "</xsl:text>
                        <xsl:value-of select="inloc:scheme/inloc:label"/>
                        <xsl:text>" .
</xsl:text>
                    </xsl:otherwise>
                </xsl:choose>
                <!--
                    <xsl:if test="following::inloc:scheme[id=$valschemeid]/inloc:label!=''">
                    <xsl:text>inloc:</xsl:text>
                    <xsl:value-of select="substring-after(inloc:scheme/@id,'http://purl.org/net/inloc/')"/>
                    <xsl:text> inloc:label "</xsl:text>
                    <xsl:value-of select="inloc:scheme/inloc:label"/>
                    <xsl:text>" .
</xsl:text>
                </xsl:if>
                -->

                <xsl:for-each select="inloc:object/inloc:label">
                    <xsl:call-template name="multilinguallabel">
                        <xsl:with-param name="id" select="$objectid"/>
                        <xsl:with-param name="type" select="' inloc:label '"/>
                    </xsl:call-template>
                </xsl:for-each>

                <xsl:if test="inloc:number!=''">
                    <xsl:value-of select="$objectid"/>
                    <xsl:text> inloc:number "</xsl:text>
                    <xsl:value-of select="inloc:number"/>
                    <xsl:text>"^^xsd:decimal .
</xsl:text>
                </xsl:if>
            </xsl:when>


            <!-- LOC ASSOCIATION TOPIC -->
            <xsl:when test="@type ='http://purl.org/net/inloc/topic'">
                <xsl:value-of select="$subjectid"/>
                <xsl:text> inloc:topic </xsl:text>
                <xsl:variable name="idblank">
                    <xsl:text>_:blanktopic</xsl:text>
                    <xsl:value-of select="position()"/>
                </xsl:variable>
                <xsl:value-of select="$idblank"/>
                <xsl:text> .
</xsl:text>
                <xsl:value-of select="$idblank"/>
                <xsl:text> inloc:hasScheme </xsl:text>
                <xsl:variable name="schemeid">
                    <xsl:choose>
                        <xsl:when test="contains(inloc:scheme/@id,'http')">
                            <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                            <xsl:value-of select="inloc:scheme/@id"/>
                            <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                        </xsl:when>
                        <xsl:when test="inloc:scheme/@id!=''">
                            <xsl:text>_:</xsl:text>
                            <xsl:value-of select="replace(inloc:scheme/@id,'#','\\#')"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:text>_:blanktopicscheme</xsl:text>
                            <xsl:value-of select="position()"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:value-of select="$schemeid"/>
                <xsl:text> .
</xsl:text>
                <xsl:for-each select="inloc:scheme/inloc:label">
                    <xsl:call-template name="multilinguallabel">
                        <xsl:with-param name="id" select="$schemeid"/>
                        <xsl:with-param name="type" select="' inloc:label '"/>
                    </xsl:call-template>
                </xsl:for-each>

                <xsl:value-of select="$idblank"/>
                <xsl:text> inloc:hasObject </xsl:text>
                <xsl:variable name="objectid">
                    <xsl:text>_:blanktopicobject</xsl:text>
                    <xsl:value-of select="position()"/>
                </xsl:variable>
                <xsl:value-of select="$objectid"/>
                <xsl:text> .
</xsl:text>
                <xsl:value-of select="$objectid"/>
                <xsl:text> inloc:id </xsl:text>
                <xsl:choose>
                    <xsl:when test="contains(inloc:object/@id,$LOCstructureid)">
                        <xsl:variable name="idlocal" select="tokenize(inloc:object/@id,'/')[last()]"/>
                        <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                        <xsl:value-of select="$idlocal"/>
                        <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                    </xsl:when>
                    <xsl:when test="contains(inloc:object/@id,'http')">
                        <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                        <xsl:value-of select="inloc:object/@id"/>
                        <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                    </xsl:when>
                    <xsl:when test="inloc:object/@id!=''">
                        <xsl:text>"</xsl:text>
                        <xsl:value-of select="inloc:object/@id"/>
                        <xsl:text>"</xsl:text>
                    </xsl:when>
                </xsl:choose>
                <xsl:text> .
</xsl:text>

                <xsl:for-each select="inloc:object/inloc:label">
                    <xsl:call-template name="multilinguallabel">
                        <xsl:with-param name="id" select="$objectid"/>
                        <xsl:with-param name="type" select="' inloc:label '"/>
                    </xsl:call-template>
                </xsl:for-each>

            </xsl:when>

            <!-- LOC ASSOCIATION CATEGORY -->
            <xsl:when test="@type ='http://purl.org/net/inloc/category'">
                <xsl:value-of select="$subjectid"/>
                <xsl:text> inloc:category </xsl:text>
                <xsl:variable name="idblank">
                    <xsl:text>_:blankcategory</xsl:text>
                    <xsl:value-of select="position()"/>
                </xsl:variable>
                <xsl:value-of select="$idblank"/>
                <xsl:text> .
</xsl:text>
                <xsl:value-of select="$idblank"/>
                <xsl:text> inloc:hasScheme </xsl:text>
                <xsl:variable name="schemeid">
                    <xsl:choose>
                        <xsl:when test="contains(inloc:scheme/@id,'http')">
                            <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                            <xsl:value-of select="inloc:scheme/@id"/>
                            <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                        </xsl:when>
                        <xsl:when test="inloc:scheme/@id!=''">
                            <xsl:text>_:</xsl:text>
                            <xsl:value-of select="replace(inloc:scheme/@id,'#','\\#')"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:text>_:blankcategoryscheme</xsl:text>
                            <xsl:value-of select="position()"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:value-of select="$schemeid"/>
                <xsl:text> .
</xsl:text>
                <xsl:for-each select="inloc:scheme/inloc:label">
                    <xsl:call-template name="multilinguallabel">
                        <xsl:with-param name="id" select="$schemeid"/>
                        <xsl:with-param name="type" select="' inloc:label '"/>
                    </xsl:call-template>
                </xsl:for-each>

                <xsl:value-of select="$idblank"/>
                <xsl:text> inloc:hasObject </xsl:text>
                <xsl:variable name="objectid">
                    <xsl:choose>
                        <xsl:when test="contains(inloc:object/@id,$LOCstructureid)">
                            <xsl:variable name="idlocal"
                                select="tokenize(inloc:object/@id,'/')[last()]"/>
                            <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                            <xsl:value-of select="$idlocal"/>
                            <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                        </xsl:when>
                        <xsl:when test="contains(inloc:object/@id,'http')">
                            <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                            <xsl:value-of select="inloc:object/@id"/>
                            <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                        </xsl:when>
                        <xsl:when test="inloc:object/@id!=''">
                            <xsl:text>_:</xsl:text>
                            <xsl:value-of select="replace(inloc:object/@id,'#','\\#')"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:text>_:blankcategoryobject</xsl:text>
                            <xsl:value-of select="position()"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:value-of select="$objectid"/>
                <xsl:text> .
</xsl:text>

                <xsl:for-each select="inloc:object/inloc:label">
                    <xsl:call-template name="multilinguallabel">
                        <xsl:with-param name="id" select="$objectid"/>
                        <xsl:with-param name="type" select="' inloc:label '"/>
                    </xsl:call-template>
                </xsl:for-each>
            </xsl:when>

            <!-- LOC ASSOCIATION CREDIT -->
            <xsl:when test="@type ='http://purl.org/net/inloc/credit'">
                <xsl:value-of select="$subjectid"/>
                <xsl:text> inloc:credit </xsl:text>
                <xsl:variable name="idblank">
                    <xsl:text>_:blankcredit</xsl:text>
                    <xsl:value-of select="position()"/>
                </xsl:variable>
                <xsl:value-of select="$idblank"/>
                <xsl:text> .
</xsl:text>
                <xsl:value-of select="$idblank"/>
                <xsl:text> inloc:hasScheme </xsl:text>
                <xsl:variable name="schemeid">
                    <xsl:choose>
                        <xsl:when test="contains(inloc:scheme/@id,'http')">
                            <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                            <xsl:value-of select="inloc:scheme/@id"/>
                            <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                        </xsl:when>
                        <xsl:when test="inloc:scheme/@id!=''">
                            <xsl:text>_:</xsl:text>
                            <xsl:value-of select="replace(inloc:scheme/@id,'#','\\#')"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:text>_:blankcreditscheme</xsl:text>
                            <xsl:value-of select="position()"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:value-of select="$schemeid"/>
                <xsl:text> .
</xsl:text>

                <xsl:for-each select="inloc:scheme/inloc:label">
                    <xsl:call-template name="multilinguallabel">
                        <xsl:with-param name="id" select="$schemeid"/>
                        <xsl:with-param name="type" select="' inloc:label '"/>
                    </xsl:call-template>
                </xsl:for-each>

                <xsl:if test="inloc:object/@id!=''">
                    <xsl:value-of select="$idblank"/>
                    <xsl:text> inloc:hasObject </xsl:text>
                    <xsl:variable name="objectid">
                        <xsl:choose>
                            <xsl:when test="contains(inloc:object/@id,$LOCstructureid)">
                                <xsl:variable name="idlocal"
                                    select="tokenize(inloc:object/@id,'/')[last()]"/>
                                <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                                <xsl:value-of select="$idlocal"/>
                                <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                            </xsl:when>
                            <xsl:when test="contains(inloc:object/@id,'http')">
                                <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                                <xsl:value-of select="inloc:object/@id"/>
                                <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                            </xsl:when>
                            <xsl:when test="inloc:object/@id!=''">
                                <xsl:text>_:</xsl:text>
                                <xsl:value-of select="replace(inloc:object/@id,'#','\\#')"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:text>_:blankcreditobject</xsl:text>
                                <xsl:value-of select="position()"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:variable>
                    <xsl:value-of select="$objectid"/>
                    <xsl:text> .
</xsl:text>

                    <xsl:for-each select="inloc:object/inloc:label">
                        <xsl:call-template name="multilinguallabel">
                            <xsl:with-param name="id" select="$objectid"/>
                            <xsl:with-param name="type" select="' inloc:label '"/>
                        </xsl:call-template>
                    </xsl:for-each>

                </xsl:if>

                <xsl:if test="inloc:number!=''">
                    <xsl:value-of select="$idblank"/>
                    <xsl:text> inloc:number "</xsl:text>
                    <xsl:value-of select="inloc:number"/>
                    <xsl:text>"^^xsd:decimal .
</xsl:text>
                </xsl:if>
            </xsl:when>

            <!-- LOC ASSOCIATION LEVEL -->
            <xsl:when test="@type ='http://purl.org/net/inloc/level'">
                <xsl:value-of select="$subjectid"/>
                <xsl:text> inloc:level </xsl:text>
                <xsl:variable name="idblank">
                    <xsl:text>_:blanklevel</xsl:text>
                    <xsl:value-of select="position()"/>
                </xsl:variable>
                <xsl:value-of select="$idblank"/>
                <xsl:text> .
</xsl:text>
                <xsl:value-of select="$idblank"/>
                <xsl:text> inloc:hasScheme </xsl:text>
                <xsl:variable name="schemeid">
                    <xsl:choose>
                        <xsl:when test="contains(inloc:scheme/@id,'http')">
                            <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                            <xsl:value-of select="inloc:scheme/@id"/>
                            <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                        </xsl:when>
                        <xsl:when test="inloc:scheme/@id!=''">
                            <xsl:text>_:</xsl:text>
                            <xsl:value-of select="replace(inloc:scheme/@id,'#','\\#')"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:text>_:blanklevelscheme</xsl:text>
                            <xsl:value-of select="position()"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:value-of select="$schemeid"/>
                <xsl:text> .
</xsl:text>
                <xsl:for-each select="inloc:scheme/inloc:label">
                    <xsl:call-template name="multilinguallabel">
                        <xsl:with-param name="id" select="$schemeid"/>
                        <xsl:with-param name="type" select="' inloc:label '"/>
                    </xsl:call-template>
                </xsl:for-each>

                <xsl:if test="inloc:object/@id!=''">
                    <xsl:value-of select="$idblank"/>
                    <xsl:text> inloc:hasObject </xsl:text>
                    <xsl:variable name="objectid">
                        <xsl:choose>
                            <xsl:when test="contains(inloc:object/@id,$LOCstructureid)">
                                <xsl:variable name="idlocal"
                                    select="tokenize(inloc:object/@id,'/')[last()]"/>
                                <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                                <xsl:value-of select="$idlocal"/>
                                <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                            </xsl:when>
                            <xsl:when test="contains(inloc:object/@id,'http')">
                                <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                                <xsl:value-of select="inloc:object/@id"/>
                                <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                            </xsl:when>
                            <xsl:when test="inloc:object/@id!=''">
                                <xsl:text>_:</xsl:text>
                                <xsl:value-of select="replace(inloc:object/@id,'#','\\#')"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:text>_:blanklevelobject</xsl:text>
                                <xsl:value-of select="position()"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:variable>
                    <xsl:value-of select="$objectid"/>
                    <xsl:text> .
</xsl:text>

                    <xsl:for-each select="inloc:object/inloc:label">
                        <xsl:call-template name="multilinguallabel">
                            <xsl:with-param name="id" select="$objectid"/>
                            <xsl:with-param name="type" select="' inloc:label '"/>
                        </xsl:call-template>
                    </xsl:for-each>

                </xsl:if>
                <xsl:if test="inloc:number!=''">
                    <xsl:value-of select="$idblank"/>
                    <xsl:text> inloc:number "</xsl:text>
                    <xsl:value-of select="inloc:number"/>
                    <xsl:text>"^^xsd:decimal .
</xsl:text>
                </xsl:if>
            </xsl:when>


            <!-- LOC ASSOCIATION OTHER TYPES -->
            <xsl:otherwise>
                <xsl:value-of select="$subjectid"/>
                <xsl:text> inloc:</xsl:text>
                <xsl:value-of
                    select="substring-after(inloc:scheme/@id,'http://purl.org/net/inloc/')"/>
                <xsl:text> "</xsl:text>
                <xsl:value-of select="inloc:object/inloc:label"/>
                <xsl:text>" .
</xsl:text>
            </xsl:otherwise>
        </xsl:choose>

    </xsl:template>


    <!-- ********* TEMPLATE Multilingual string ************** -->
    <xsl:template name="multilinguallabel">
        <xsl:param name="id"/>
        <xsl:param name="type"/>
        <xsl:value-of select="$id"/>
        <xsl:value-of select="$type"/>
        <xsl:text>"</xsl:text>
        <xsl:value-of select="."/>
        <xsl:text>"</xsl:text>
        <xsl:if test="@xml:lang!=''">
            <xsl:text>@</xsl:text>
            <xsl:value-of select="@xml:lang"/>
        </xsl:if>
        <xsl:text> .
</xsl:text>
    </xsl:template>


</xsl:stylesheet>
