<xsl:stylesheet 
    xmlns:xsl  ="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:h    ="http://www.w3.org/1999/xhtml"
    xmlns:cc   ="http://web.resource.org/cc/"
    xmlns:dc   ="http://purl.org/dc/elements/1.1/"
    xmlns:rdf  ="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:dt   ="http://www.w3.org/2001/XMLSchema#"
    xmlns:rddl="http://www.rddl.org/"
    xmlns:rddlp='http://www.rddl.org/purposes#'
    xmlns:rddln='http://www.rddl.org/natures#'
    >

<!--
  $Id: grokRDDL.xsl,v 1.2 2004/01/16 05:29:06 connolly Exp $
  interpret RDDL as RDF.
  cf http://www.tbray.org/tag/rddl4.html
  -->

<xsl:output method="xml" indent="yes"/>

<xsl:template match="h:html">

  <xsl:variable name='subjectNamespace'>
    <xsl:choose>
      <xsl:when test='.//h:a[@rddl:purpose="http://www.rddl.org/purposes#target"]'>
	<xsl:value-of select='.//h:a[@rddl:purpose="http://www.rddl.org/purposes#target"]/@href'/>
      </xsl:when>

      <!-- @@KLUDGE! but we need some way to find out what
	   namespace rddl4.html is talking about -->
      <xsl:when test='.//h:code[starts-with(., "http://")]'>
	<xsl:value-of
	 select='.//h:code[starts-with(., "http://")]/text()'/>
      </xsl:when>

      <xsl:otherwise>
	<xsl:message>no target namespace found!</xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <rdf:RDF>
    <xsl:for-each select=".//h:a[@rddl:nature or @rddl:purpose]">
      <xsl:call-template name='grokLink'>
	<xsl:with-param name='subject' select='$subjectNamespace'/>
      </xsl:call-template>
    </xsl:for-each>
  </rdf:RDF>
</xsl:template>


<xsl:template name='grokLink'>
  <xsl:param name='subject'/>

  <xsl:if test='@rddl:purpose'>

    <!-- cf httpRange-14 ;-) -->
    <xsl:if test='not(contains(@rddl:purpose, "#"))'>
      <xsl:message>warning: don't grok nature without #: <xsl:value-of select='@rddl:purpose'/>
      </xsl:message>
    </xsl:if>
    
    <xsl:variable name='ns' select='concat(substring-before(@rddl:purpose, "#"), "#")'/>
    <xsl:variable name='ln' select='substring-after(@rddl:purpose, "#")'/>
    
    <rdf:Description rdf:about='{$subject}'>
      <xsl:element namespace='{$ns}' name='{$ln}'>
	<xsl:attribute name='rdf:resource'>
	  <xsl:value-of select='@href'/>
	</xsl:attribute>
      </xsl:element>
    </rdf:Description>
  </xsl:if>

  <xsl:if test='@rddl:nature'>
    <rdf:Description rdf:about='{@href}'>
      <rddl:nature rdf:resource='{@rddl:nature}'/>
    </rdf:Description>
  </xsl:if>

</xsl:template>


<!-- don't pass text thru -->
<xsl:template match="text()|@*">
</xsl:template>


</xsl:stylesheet>
