<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
	       xmlns:h="http://www.w3.org/2002/06/xhtml2"
	       xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	       xmlns:dc="http://purl.org/dc/elements/1.1/"
	       xmlns:foaf="http://xmlns.com/foaf/0.1/"
>

  <xsl:output indent="yes"/>


  <xsl:template name="resolve-QName">
    <xsl:param name="qname" select="''"/>

    <xsl:choose>
      <xsl:when test="$qname=''">h:reference</xsl:when>
      <xsl:when test="not(contains($qname,':'))">h:<xsl:value-of select="$qname"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="$qname"/></xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  

<!-- ##### META ########################## -->


  <xsl:template match="/">
    <rdf:RDF>
      <xsl:apply-templates select="h:html/h:head"/>
    </rdf:RDF>
  </xsl:template>
  
  <xsl:template match="h:meta">
    <xsl:variable name="prop">
      <xsl:call-template name="resolve-QName">
	<xsl:with-param name="qname" select="@property"/>
      </xsl:call-template>
    </xsl:variable>

    <rdf:Description>
      <xsl:attribute name="rdf:about">
	<xsl:choose>
	  <xsl:when test="@about">
	    <xsl:value-of select="@about"/>
	  </xsl:when>
	  <xsl:when test="parent::h:link/@resource">
	    <xsl:value-of select="parent::h:link/@resource"/>
	  </xsl:when>
	  <xsl:when test="parent::h:link">
	    <xsl:value-of select="generate-id(parent::h:link)"/>
	  </xsl:when>
	  <xsl:otherwise/>
	</xsl:choose>
      </xsl:attribute>

      <xsl:element name="{$prop}">

	<xsl:if test="@datatype">
	  <xsl:attribute name="rdf:datatype">
	    <xsl:value-of select="@datatype"/>
	  </xsl:attribute>
	</xsl:if>
	
	<xsl:choose>
	  <xsl:when test="@content">
	    <xsl:value-of select="@content"/>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:attribute name="rdf:parseType">Literal</xsl:attribute>
	    <xsl:copy-of select="*|text()"/>
	  </xsl:otherwise>
	</xsl:choose>
      </xsl:element>
    </rdf:Description>

  </xsl:template>


<!-- ############ LINK #################### -->


  <xsl:template match="h:link">
    <xsl:variable name="rel">
      <xsl:call-template name="resolve-QName">
	<xsl:with-param name="qname" select="@rel|@rev|@property"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:if test="@rel|@property">
      <rdf:Decription>
	<!-- subject -->
	<xsl:choose>
	  <xsl:when test="@about">
	      <xsl:attribute name="rdf:about">
		<xsl:value-of select="@about"/>
	    </xsl:attribute>
	  </xsl:when>
	  <xsl:when test="parent::h:link/@resource">
	    <xsl:attribute name="rdf:about">
	      <xsl:value-of select="parent::h:link/@resource"/>
	    </xsl:attribute>
	  </xsl:when>
	  <xsl:when test="parent::h:link">
	    <xsl:attribute name="rdf:nodeID">
	      <xsl:value-of select="generate-id(parent::h:link)"/>
	    </xsl:attribute>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:attribute name="rdf:about"/>
	    </xsl:otherwise>
	</xsl:choose>
	
	
	<xsl:element name="{$rel}">
	  <!-- generate the object, either rdf:resource, or rdf:nodeID -->
	  <xsl:choose>
	    <xsl:when test="@resource">
	      <xsl:attribute name="rdf:resource">
		<xsl:value-of select="@resource"/>
	      </xsl:attribute>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:attribute name="rdf:nodeID">
		<xsl:value-of select="generate-id(.)"/>
	      </xsl:attribute>
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:element>
      </rdf:Decription>
    </xsl:if>
    <xsl:if test="@rev">
      <rdf:Description>
	<xsl:choose>
	  <!-- subject -->
	  <xsl:when test="@resource">
	    <xsl:attribute name="rdf:about">
	      <xsl:value-of select="@resource"/>
	    </xsl:attribute>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:attribute name="rdf:nodeID">
	      <xsl:value-of select="generate-id(.)"/>
	    </xsl:attribute>
	  </xsl:otherwise>
	</xsl:choose>
	
	<xsl:element name="{$rel}">
	    <!-- object -->
	    <xsl:choose>
	      <xsl:when test="@about">
		<xsl:attribute name="rdf:resource">
		  <xsl:value-of select="@about"/>
		</xsl:attribute>
	      </xsl:when>
	      <xsl:when test="parent::h:link/@resource">
		<xsl:attribute name="rdf:resource">
		  <xsl:value-of select="parent::h:link/@resource"/>
		</xsl:attribute>
	      </xsl:when>
	      <xsl:when test="parent::h:link">
		<xsl:attribute name="rdf:nodeID">
		  <xsl:value-of select="generate-id(parent::h:link)"/>
		</xsl:attribute>
	      </xsl:when>
	      <xsl:otherwise>
		<xsl:attribute name="rdf:resource"/>
	      </xsl:otherwise>
	    </xsl:choose>
	</xsl:element>
      </rdf:Description>
    </xsl:if>

    <!-- link could contain link or meta element -->
    <xsl:apply-templates/>

  </xsl:template>


  
  <!-- by default, do nothing. @@TODO: handle links in <body> -->
  <xsl:template match="h:title"/>


</xsl:transform>
