<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:foaf="http://xmlns.com/foaf/0.1/"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  xmlns:rss="http://purl.org/rss/1.0/"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:html="http://www.w3.org/1999/xhtml"
  version="1.0">

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

  <xsl:param name="baseurl">http://www.example.org/</xsl:param>

  <xsl:template match="html:html">
    <rdf:RDF>
      <xsl:apply-templates/>
    </rdf:RDF>
  </xsl:template>
  
  <xsl:template match="html:head">
    <rdf:Description>
      <xsl:attribute name="rdf:about">
	<xsl:value-of select="$baseurl"/>
      </xsl:attribute>
    <xsl:apply-templates/>
    </rdf:Description>

  </xsl:template>


 <xsl:template match="html:link">
	<xsl:if test="@type='application/rss+xml'">
	 <rdfs:seeAlso>
	<xsl:attribute name="rdf:resource">
	<xsl:value-of select="@href"/>
	</xsl:attribute>
	</rdfs:seeAlso>
	</xsl:if>
</xsl:template>

  <xsl:template match="html:title">
      <xsl:if test=". != ''">
	<dc:title><xsl:value-of select="."/></dc:title>
      </xsl:if>
  </xsl:template>

  <xsl:template match="html:body">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="html:p|html:div|html:span|html:table|html:td|html:tr|html:ul|html:li">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="html:a">
    <!-- first, describe outgoing links -->
    <foaf:Document>
      <xsl:attribute name="rdf:about">
	<xsl:value-of select="$baseurl"/>
      </xsl:attribute>
      <xsl:choose>
	<xsl:when test="starts-with(@href, $baseurl)">
	  <!-- absolute href that should be relative -->
	  <rss:link>
	    <xsl:attribute name="rdf:resource">
	      <xsl:value-of select="@href"/>
	    </xsl:attribute>
	  </rss:link>
	</xsl:when>
	<xsl:when test="starts-with(@href, 'http://')">
	  <!-- absolute href -->
	  <rss:link>
	    <!-- check for empty link text -->
	    <xsl:choose>
	      <xsl:when test="normalize-space(.) = ''">
		<xsl:attribute name="rdf:resource">
		  <xsl:value-of select="@href"/>
		</xsl:attribute>
	      </xsl:when>
	      <xsl:otherwise>
		<foaf:Document>
		  <xsl:attribute name="rdf:about">
		    <xsl:value-of select="@href"/>
		  </xsl:attribute>
		  <rdfs:label><xsl:value-of select="normalize-space(.)"/></rdfs:label>
		</foaf:Document>
	      </xsl:otherwise>
	    </xsl:choose>	    
	  </rss:link>
	</xsl:when>
	<xsl:when test="starts-with(@href, 'mailto:')">
	  <!-- email address -->
	  <foaf:topic>
	    <foaf:Agent>
	      <foaf:mbox>
		<xsl:attribute name="rdf:resource">
		  <xsl:value-of select="@href"/>
		</xsl:attribute>
	      </foaf:mbox>
	      <rdfs:label>
		<xsl:value-of select="."/>
	      </rdfs:label>
	    </foaf:Agent>
	  </foaf:topic>
	</xsl:when>
	<xsl:otherwise>
	  <!-- relative links -->
	  <rss:link>
	    <xsl:attribute name="rdf:resource">
	      <!-- might start with a / -->
	      <xsl:choose>
		<xsl:when test="starts-with(@href, '/')">
		  <xsl:value-of select="concat($baseurl, substring(@href, 2))"/>
		</xsl:when>
		<xsl:otherwise>	
     		  <xsl:value-of select="concat($baseurl, @href)"/>
		</xsl:otherwise>
	      </xsl:choose>
	    </xsl:attribute>
	  </rss:link>
	</xsl:otherwise>
      </xsl:choose>
    </foaf:Document>
  </xsl:template>
  
  <xsl:template match="html:img">
    <rdf:Description>
      <xsl:choose>
	<xsl:when test="starts-with(@src, 'http://')">
	  <xsl:attribute name="rdf:about">
	    <xsl:value-of select="@src"/>
	  </xsl:attribute>
	</xsl:when>
	<xsl:when test="starts-with(@src, 'mailto:')">
	  <xsl:attribute name="rdf:about">
	    <xsl:value-of select="@src"/>
	  </xsl:attribute>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:attribute name="rdf:about">
	    <xsl:value-of select="concat($baseurl, @src)"/>
	  </xsl:attribute>
	</xsl:otherwise>
      </xsl:choose>
      <xsl:if test="normalize-space(@alt) != ''">
	<rdfs:label><xsl:value-of select="normalize-space(@alt)"/></rdfs:label>
      </xsl:if>
      <rdf:type rdf:resource="http://xmlns.com/foaf/0.1/Image"/>
    </rdf:Description>
  </xsl:template>

  <!-- match all other tags -->
  <xsl:template match="*">
  </xsl:template>
	
  <!-- match default text -->
  <xsl:template match="text()|@*">
  </xsl:template>
</xsl:stylesheet>
