<?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 fact that literal such as in:

    <q:where>
      <inseePerson>
        <mail>franck.cotton@insee.fr</mail>
      </inseePerson>
    </q:where>
    
    are shortcuts to:
    
    <q:where>
      <inseePerson>
        <mail>
          <q:conditions>
            <q:if>
              <q:all>
                <q:equals>franck.cotton@insee.fr</q:equals>
              </q:all>
            </q:if>
          </q:conditions>
        </mail>
      </inseePerson>
    </q:where>
    
    ie, on flat RDF:
    
    <inseePerson rdf:ID="d0e7">
      <mail>franck.cotton@insee.fr</mail>
    </inseePerson>
    
    is a shortcut to:
    
    <inseePerson rdf:ID="d0e7">
      <mail rdf:resource="#d0e9"/>
    </inseePerson>
    <q:conditions rdf:ID="d0e9">
      <q:if rdf:resource="#d0e11"/>
    </q:conditions>
    <q:all rdf:ID="d0e11">
      <q:equals>franck.cotton@insee.fr</q:equals>
    </q:all>
    -->
    <xsl:import href="flat-rdf.xsl"/>
    <xsl:strip-space elements="*"/>
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/*">
        <xsl:apply-templates select="." mode="literals"/>
    </xsl:template>
    <xsl:template match="/*" mode="literals">
        <xsl:copy>
            <xsl:variable name="flat">
                <xsl:apply-templates select="." mode="root"/>
            </xsl:variable>
            <xsl:apply-templates select="exsl:node-set($flat)/*/*" mode="literals.ref"/>
            <xsl:apply-templates select="exsl:node-set($flat)/*/*" mode="literals.def"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*|@*" mode="literals.ref">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" mode="literals.ref"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/*/*/a:*[text()]|/*/*/l:*[text()]" mode="literals.ref">
        <xsl:copy>
            <xsl:attribute name="rdf:resource">
                <xsl:value-of select="concat('#', generate-id())"/>
            </xsl:attribute>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*|@*" mode="literals.def">
        <xsl:apply-templates select="@*|node()" mode="literals.def"/>
    </xsl:template>
    <xsl:template match="/*/*/a:*[text()]|/*/*/l:*[text()]" mode="literals.def">
        <q:conditions rdf:ID="{generate-id()}">
            <q:if rdf:resource="#all:{generate-id()}"/>
        </q:conditions>
        <q:all rdf:ID="all:{generate-id()}">
            <q:equals>
                <xsl:value-of select="."/>
            </q:equals>
        </q:all>
    </xsl:template>
</xsl:stylesheet>
