<xsl:stylesheet version="1.0" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:dataview="http://www.w3.org/2003/g/data-view#" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:uri="http://www.w3.org/2000/07/uri43/uri.xsl?template="  xmlns:grddl="http://www.w3.org/2003/11/rdf-in-xhtml-processor" exclude-result-prefixes="uri dataview html grddl">
  <xsl:import href="http://www.w3.org/2000/07/uri43/uri.xsl"/>
  <xsl:import href="http://www.w3.org/2003/11/rdf-in-xhtml-processor.xsl"/>
  <xsl:output method="xml" encoding="utf-8" indent="yes" media-type="application/rdf+xml"/>


  <!-- $Id: grddl-xml-processor.xsl,v 1.21 2005/10/24 07:44:49 dom Exp $
GRDDL interpretor for generic XML files
see http://www.w3.org/TR/grddl/

-->

  <xsl:param name="xmlfile"/>
  <xsl:variable name="baseURI">
    <xsl:choose>
      <xsl:when test="/*[@xml:base]">
        <xsl:value-of select="/*[@xml:base]"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$xmlfile"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:template match="/*">
    <xsl:variable name="nsURI">
      <xsl:choose>
        <xsl:when test="not(namespace-uri()='http://www.w3.org/2001/XMLSchema')">
          <xsl:value-of select="namespace-uri()"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>http://www.w3.org/2001/XMLSchema.xsd</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"  xml:base='{$xmlfile}'>
      <!-- Using the XSLT servlet to include the content of the processing of the XML document through the linked XSLTs -->
      <xsl:if test="@dataview:transformation">
        <xsl:call-template name="callInterpreter">
          <xsl:with-param name="uris" select="@dataview:transformation"/>
        </xsl:call-template>
      </xsl:if>
      <!-- if this is an XHTML document, we call the xhtml interprerter for XHTML -->
      <!-- @@@ Should this be in fact recorded in the XHTML namespace for sake of simplicity ? -->

      <xsl:if test="local-name()='html' and namespace-uri()='http://www.w3.org/1999/xhtml'">
        <xsl:call-template name="grddl:XHTMLInterpreter"/>
      </xsl:if>
      <xsl:message>
        Dereferencing NS document <xsl:value-of select="$nsURI"/>
      </xsl:message>

      <xsl:if test="document($nsURI)/rdf:RDF">
        <xsl:call-template name="nsInRdfInterpreter">
          <xsl:with-param name="Rdf" select="document($nsURI)/rdf:RDF"/>
          <xsl:with-param name="nsURI" select="$nsURI"/>
        </xsl:call-template> 
      </xsl:if>
        <!-- Using the XSLT servlet to include the content of the processing of the XML document through the XSLTs linked from the namespace document, itself interpreted as RDF through GRDDL -->
      <xsl:if test="document($nsURI)/*/@dataview:transformation">

        <xsl:variable name="nsRdfInterpretation" select="document(concat('http://www.w3.org/2000/06/webdata/xslt?xslfile=http%3A%2F%2Fwww.w3.org%2F2004%2F01%2Frdxh%2Fgrddl-xml-processor&amp;xmlfile=',$nsURI))/rdf:RDF"/>
        <xsl:call-template name="nsInRdfInterpreter">
          <xsl:with-param name="Rdf" select="$nsRdfInterpretation"/>
          <xsl:with-param name="nsURI" select="$nsURI"/>
        </xsl:call-template>
      </xsl:if>
      <!-- case where the namespace document is in fact an XHTML document using GDDRL for XHTML -->
      <xsl:if test="document($nsURI)/html:html/html:head[contains(@profile,'http://www.w3.org/2003/g/data-view')]">
        <xsl:variable name="nsRdfInterpretation" select="document(concat('http://www.w3.org/2000/06/webdata/xslt?xslfile=http%3A%2F%2Fwww.w3.org%2F2003%2F11%2Frdf-in-xhtml-processor&amp;xmlfile=',$nsURI))/rdf:RDF"/>
        <xsl:call-template name="nsInRdfInterpreter">
          <xsl:with-param name="Rdf" select="$nsRdfInterpretation"/>
          <xsl:with-param name="nsURI" select="$nsURI"/>
        </xsl:call-template>
      </xsl:if>
    </rdf:RDF>
  </xsl:template>

  <xsl:template name="nsInRdfInterpreter" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <xsl:param name="Rdf"/>
    <xsl:param name="nsURI"/>
    <xsl:message>analysing <xsl:value-of select="$nsURI"/></xsl:message>

    <!-- Using the XSLT servlet to include the content of the processing of the XML document through the XSLTs linked from the namespace document -->
    <xsl:for-each select="$Rdf/*[@rdf:about='' or @rdf:about=$nsURI][dataview:namespaceTransformation/@rdf:resource]">
      <xsl:call-template name="callInterpreter">
        <xsl:with-param name="uris">
          <xsl:call-template name="uri:expand">
            <xsl:with-param name="base"
                          select="$nsURI"/>
            <xsl:with-param name="there" select="$Rdf/*[@rdf:about='' or @rdf:about=$nsURI]/dataview:namespaceTransformation/@rdf:resource"/>
          </xsl:call-template>
        </xsl:with-param>
        <xsl:with-param name="docURI" select="$xmlfile"/>
      </xsl:call-template>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="callInterpreter" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <xsl:param name="uris"/>
    <xsl:param name="docURI" select="$xmlfile"/>
    <xsl:if test="normalize-space($uris)">
      <!-- uris is a list of white-space separated URIs -->
      <xsl:variable name="xslfile">
        <!-- Absolute URI of the XSLT -->
        <xsl:call-template name="uri:expand">
          <xsl:with-param name="base"
                          select="$baseURI"/>
          <xsl:with-param name="there" select="substring-before(concat(normalize-space($uris),' '),' ')"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:copy-of select="document(concat('http://www.w3.org/2000/06/webdata/xslt?xslfile=',$xslfile,'&amp;xmlfile=',$docURI))/rdf:RDF/*"/>
      <xsl:call-template name="callInterpreter">
        <xsl:with-param name="uris" select="substring-after(normalize-space($uris),' ')"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

