<?xml version='1.0'?>
<!DOCTYPE doc SYSTEM "../../../../../../lib/xml/xsl.dtd" >
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rddl="http://www.rddl.org/" xmlns:rddlx="http://www.rddl.org/natures#" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:rddlp="http://www.rddl.org/purposes#" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:xsdl="http://www.w3.org/XML/XMLSchema#">
 
 <xsl:output method="xml" indent="yes"/>

 <xsl:param name="doc">http://www.w3.org/2001/XMLSchema</xsl:param>
 
 <xsl:template match="/">
  <rdf:RDF>
   <owl:Ontology rdf:about="">
    <owl:imports rdf:resource="rddl.owl"/>
    <owl:imports rdf:resource="xsdStatic.owl"/>
   </owl:Ontology>
   <owl:Ontology rdf:about="rddl.owl"/>
   <owl:Ontology rdf:about="xsdStatic.owl"/>
   <rdf:Description rdf:about="{$doc}">
    <rdf:type rdf:resource="http://www.w3.org/XML/namespaces#Namespace"/>
    <xsl:apply-templates select="//*[@class='resource']/rddl:resource"/>
   </rdf:Description>
   <xsl:apply-templates select="//*[@class='anchor']/rddl:resource"/>
  </rdf:RDF>
 </xsl:template>
 
 <!-- For now, at least, the [1] means we only extract the first thing about any given anchor -->
 
 <xsl:template match="*[@class='anchor']/rddl:resource[1]">
  <xsl:choose>
   <xsl:when test="contains(@xlink:role,'#dt-built-in')">
    <rdfs:Datatype rdf:ID="{../@id}" xml:base="{$doc}">
   <xsl:call-template name="rr"/>
  </rdfs:Datatype>
   </xsl:when>
   <xsl:when test="contains(@xlink:role,'#rf-facets')">
    <xsdl:Facet rdf:ID="{../@id}" xml:base="{$doc}">
     <xsl:call-template name="rr"/>
    </xsdl:Facet>
   </xsl:when>
   <xsl:when test="contains(@xlink:role,'#f')">
    <xsdl:FacetValue rdf:ID="{../@id}" xml:base="{$doc}">
     <xsl:call-template name="rr"/>
    </xsdl:FacetValue>
   </xsl:when>
   <xsl:when test="contains(@xlink:role,'#element')">
    <xsdl:Element rdf:ID="{../@id}" xml:base="{$doc}">
     <xsl:call-template name="rr"/>
    </xsdl:Element>
   </xsl:when>
   <xsl:otherwise>
    <rdf:Description rdf:ID="{../@id}" xml:base="{$doc}">
   <xsl:call-template name="rr"/>
  </rdf:Description>
   </xsl:otherwise>
  </xsl:choose>  
 </xsl:template>
 
 <xsl:template match="*[@class='anchor']/rddl:resource[position()>1]"/>
 
 <xsl:template name="rr" match="*[@class='resource']/rddl:resource">
  <xsl:variable name="purpose" select="@xlink:arcrole"/>
  <xsl:variable name="nature" select="@xlink:role"/>
  <xsl:variable name="target" select="@xlink:href"/>
  <xsl:variable name="pns" select="substring-before($purpose,'#')"/>
  <xsl:variable name="pfx">
   <xsl:if test="$pns='http://www.rddl.org/purposes'"><xsl:text>rddlp:</xsl:text></xsl:if>
  </xsl:variable>
  <xsl:variable name="pnm" select="concat($pfx,substring-after($purpose,'#'))"/>
  <xsl:element name="{$pnm}" namespace="{$pns}#">
   <rddlx:Object>
    <rddlx:natureKey>
     <rddlx:NatureKey rdf:about="{$nature}"/>
    </rddlx:natureKey>
    <rddlx:target rdf:resource="{$target}"/>
   </rddlx:Object>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>
