<?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="http://xml.insee.intra/schema/annuaire/"
    xmlns:a="http://xml.insee.intra/schema/annuaire/" xmlns:q="http://xml.insee.intra/schema/qbe/"
    xmlns:l="http://xml.insee.intra/schema/ldap/" xmlns:exsl="http://exslt.org/common"
    extension-element-prefixes="exsl" exclude-result-prefixes="a l" version="1.0">
    <!-- 
    
    This transformation implements the scope clause.
    

    -->
    <xsl:import href="what.xsl"/>
    <xsl:strip-space elements="*"/>
    <xsl:output method="xml" indent="yes"/>
    <xsl:key name="resources" match="*[@rdf:ID]" use="concat('#', @rdf:ID)"/>
    <xsl:key name="backResources" match="*[@rdf:resource]"
        use="substring-after( @rdf:resource, '#' )"/>
    <xsl:template match="/*">
        <xsl:apply-templates select="." mode="scope"/>
    </xsl:template>
    <xsl:template match="/*" mode="scope">
        <xsl:copy>
            <xsl:variable name="what">
                <xsl:apply-templates select="." mode="defwhat"/>
            </xsl:variable>
            <xsl:apply-templates select="exsl:node-set($what)/*/*" mode="scope"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*|@*" mode="scope">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" mode="scope"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="q:scope" mode="scope"/>
    <xsl:template match="/rdf:RDF/*[not(self::q:*)]" mode="scope">
        <xsl:variable name="clause">
            <xsl:apply-templates select="." mode="scope.clause"/>
        </xsl:variable>
        <xsl:choose>
            <xsl:when test="$clause = 'scope' ">
                <rdf:Description>
                    <xsl:apply-templates select="@*|node()" mode="scope"/>
                </rdf:Description>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates select="@*|node()" mode="scope"/>
                    <xsl:if test="$clause='where' and /rdf:RDF/q:select/q:scope">
                        <xsl:element
                            name="{name(key('resources', /rdf:RDF/q:select/q:scope/@rdf:resource))}">
                            <xsl:copy-of select="/rdf:RDF/q:select/q:scope/@rdf:resource"/>
                        </xsl:element>
                    </xsl:if>
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template match="/rdf:RDF/*" mode="scope.clause">
        <xsl:apply-templates select="key('backResources', @rdf:ID)" mode="scope.clause"/>
    </xsl:template>
    <xsl:template match="q:select/*" mode="scope.clause">
        <xsl:value-of select="local-name()"/>
    </xsl:template>
    <xsl:template match="*" mode="scope.clause">
        <xsl:apply-templates select="parent::*" mode="scope.clause"/>
    </xsl:template>
</xsl:stylesheet>
