<!-- stylesheet that turns (modified) TriX into RDF/XML 
  In particular predicates must have:
    - an attribute namespace with an  absolute URI as value
    - a URI that matches the NCName production of XML Namespaces
-->

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns="http://www.w3.org/1999/xhtml" 
  xmlns:html="http://www.w3.org/1999/xhtml" 
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" 
  xmlns:xa="http://www.w3.org/2003/g/xml-attributes#"
  exclude-result-prefixes="html">

<xsl:import href="http://www.w3.org/2003/g/xml-attributes" />
  
<!-- Output method XML -->
<xsl:output method="xml" 
  indent="yes"
  omit-xml-declaration="no" 
  encoding="utf-8"  />


<!-- first, second and third templates labelled for reference
     from the documentation. -->
     
<!-- First template -->
<xsl:template match="graph">
 <rdf:RDF>
   <xsl:call-template name="xa:base" />
   <xsl:apply-templates />
 </rdf:RDF>
</xsl:template>


<!-- Second template -->
<xsl:template match="triple">
 <rdf:Description>
   <xsl:call-template name="xa:base">
     <xsl:with-param name="node" select="*[1]" />
     <xsl:with-param name="node-used-by-ancestor" select=".."/>
   </xsl:call-template>
   <xsl:apply-templates select="*[1]" mode="subject"/>
   <xsl:apply-templates select="." mode="property"/>
 </rdf:Description>
</xsl:template>


<!-- Third template -->
<xsl:template match="triple" mode="property">
 <xsl:element name="xaxa:{*[2]/text()}" 
    namespace="{*[2]/@namespace}">
   <xsl:call-template name="xa:base">
     <xsl:with-param name="node" select="*[3]"/>
     <xsl:with-param name="node-used-by-ancestor" select="*[1]"/>
   </xsl:call-template>
   <xsl:apply-templates select="*[3]" mode="object"/>
 </xsl:element>
</xsl:template>


<xsl:template match="uri" mode="subject">
  <xsl:attribute name="rdf:about">
    <xsl:value-of select="text()"/>
  </xsl:attribute>
</xsl:template>


<xsl:template match="id" mode="subject">
  <xsl:attribute name="rdf:nodeID">
    <xsl:value-of select="text()"/>
  </xsl:attribute>
</xsl:template>

<xsl:template match="id" mode="object">
  <xsl:attribute name="rdf:nodeID">
    <xsl:value-of select="text()"/>
  </xsl:attribute>
</xsl:template>

<xsl:template match="uri" mode="object">
  <xsl:attribute name="rdf:resource">
    <xsl:value-of select="text()"/>
  </xsl:attribute>
</xsl:template>

<xsl:template match="plainLiteral" mode="object">
  <xsl:call-template name="xa:lang" />
  <xsl:value-of select="text()"/>
</xsl:template>


<xsl:template match="typedLiteral" mode="object">
  <xsl:attribute name="rdf:datatype">
    <xsl:value-of select="@datatype"/>
  </xsl:attribute>
  <xsl:value-of select="text()"/>
</xsl:template>

  
<!-- don't pass text through. -->
<xsl:template match="text()">
</xsl:template>

<!-- don't pass attributes through. -->
<xsl:template match="@*">
</xsl:template>


</xsl:stylesheet>

