<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		xmlns:atom="http://purl.org/atom/ns#draft-ietf-atompub-format-07"
		xmlns:atomrdf="http://djpowell.net/schemas/atomrdf/0.1-EXPERIMENTAL/"
		xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
		xmlns:xhtml="http://www.w3.org/1999/xhtml"
		>

  <!-- Experimental Atom to RDF mapping - DO NOT DEPLOY -->
  <!-- $Id: atom2rdf-07.xsl,v 1.4 2005/05/08 11:24:20 Dave Exp $ -->

  <xsl:output method="xml" encoding="utf-8" indent="yes" media-type="application/rdf+xml" />

  <xsl:variable name="NSATOM" select="'http://purl.org/atom/ns#draft-ietf-atompub-format-07'" />

  <xsl:variable name="STRICT_REF_PARSING" select="true()" />

<!-- TODO atom:author on a feed applies to both the author and the
     containing entries -->

  <!-- 

notes:

TODO: update rdfs

TODO: is xml:base relevant to binary content?

The implementation is deliberately verbose and procedural to make it
easier to update and translate to other languages.  it is based on
draft-07.

The document's base url can be passed in as a parameter to the XSLT
processor, as "contentbase".

The document's base language can be passed in as a parameter to the XSLT
processor, as "contentlang".

Structured Extension constructs are preserved for round-tripping using
an atomrdf:Extension resource.  This construct preserves the subtree
together with it's xml:base and xml:lang context.

(Implementations that know how to handle a particular Structured
Extension construct should use a more useful serialization.)

xml:base is used to resolve URIs

Author defaulting is handled.

Entry instances and feed instances are disociated the feed and entry
ids - this allows temporally varying models to be made if required.


  -->

  <xsl:include href="baselangutils.xsl" />

  <!-- baselangutils.xsl includes urires.xsl
       <xsl:include href="urires.xsl" />
  -->

  <xsl:param name="contentbase" />
  <xsl:param name="contentlang" />

  <!-- Entry point -->
  <xsl:template match="/atom:feed" >

    <rdf:RDF>

      <!-- process atom:feed -->

      <!-- handle default atom:feed/atom:author -->
      <xsl:for-each select="atom:author" >
	<xsl:call-template name="DefaultAuthor" />
      </xsl:for-each>

      <atomrdf:Feed>
      
	<!-- set feed id -->
	<xsl:choose>
	  <!-- use atom:id -->
	  <xsl:when test="atom:id">
	    <xsl:attribute name="rdf:about">
	      <xsl:value-of select="atom:id" />
	    </xsl:attribute>
	  </xsl:when>
	  <!-- create id -->
	  <xsl:otherwise>
	    <xsl:attribute name="rdf:nodeID">
	      <xsl:text>mainFeed</xsl:text>
	    </xsl:attribute>
	  </xsl:otherwise>
	</xsl:choose>

	<!-- dissociate feed from feed instance, so that different
	     instances don't get smushed together -->
	<atomrdf:feedInstance>
	  <atomrdf:FeedInstance>

	    <!-- handle the rest of feed in the same way as feed/entry/source -->
	    <xsl:call-template name="Head" />

	  </atomrdf:FeedInstance>
	</atomrdf:feedInstance>

      </atomrdf:Feed>


      <!-- process entries -->
      <xsl:for-each select="atom:entry">
	<xsl:call-template name="Entry" />
      </xsl:for-each>

    </rdf:RDF>    
  </xsl:template>


  <!-- handle feed/author -->
  <xsl:template name="DefaultAuthor">

    <atomrdf:Person rdf:nodeID="defaultAuthor">

      <xsl:for-each select="atom:uri" >
	<atomrdf:uri>
	  <xsl:call-template name="resolveRef">
	    <xsl:with-param name="base">	
	      <xsl:call-template name="getXmlBase">
		<xsl:with-param name="element" select="." />
		<xsl:with-param name="default" select="$contentbase" />
	      </xsl:call-template>
	    </xsl:with-param>
	    <xsl:with-param name="strict" select="$STRICT_REF_PARSING" />
	    <xsl:with-param name="ref" select="." />
	  </xsl:call-template>
	</atomrdf:uri>
      </xsl:for-each>

      <atomrdf:name>
	<xsl:variable name="lang">
	  <xsl:call-template name="getXmlLang">
	    <xsl:with-param name="element" select="atom:name" />
	    <xsl:with-param name="default" select="$contentlang" />
	  </xsl:call-template>
	</xsl:variable>
	<xsl:if test="$lang != ''">
	  <xsl:attribute name="xml:lang">
	    <xsl:value-of select="$lang" />
	  </xsl:attribute>
	</xsl:if>
	<xsl:value-of select="atom:name" />
      </atomrdf:name>

      <xsl:for-each select="atom:email" >
	<atomrdf:email>
	  <xsl:value-of select="." />
	</atomrdf:email>
      </xsl:for-each>
      
      <!-- handle Person Construct extensions -->
      <xsl:for-each select="*[namespace-uri() != $NSATOM]">
	<xsl:call-template name="Extension" />
      </xsl:for-each>

    </atomrdf:Person>
  </xsl:template>

  <!-- handle feed/entry/author -->
  <xsl:template name="EntryAuthor">
    <atomrdf:Person>

      <xsl:attribute name="rdf:nodeID">
	<xsl:text>entryAuthor</xsl:text><xsl:number count="/atom:feed/atom:entry" />
      </xsl:attribute>	      

      <xsl:for-each select="atom:uri" >
	<atomrdf:uri>
	  <xsl:call-template name="resolveRef">
	    <xsl:with-param name="base">
	      <xsl:call-template name="getXmlBase">
		<xsl:with-param name="element" select="." />
		<xsl:with-param name="default" select="$contentbase" />
	      </xsl:call-template>
	    </xsl:with-param>
	    <xsl:with-param name="strict" select="$STRICT_REF_PARSING" />
	    <xsl:with-param name="ref" select="." />
	  </xsl:call-template>
	</atomrdf:uri>
      </xsl:for-each>

      <atomrdf:name>
	<xsl:variable name="lang">
	  <xsl:call-template name="getXmlLang">
	    <xsl:with-param name="element" select="atom:name" />
	    <xsl:with-param name="default" select="$contentlang" />
	  </xsl:call-template>
	</xsl:variable>
	<xsl:if test="$lang != ''">
	  <xsl:attribute name="xml:lang">
	    <xsl:value-of select="$lang" />
	  </xsl:attribute>
	</xsl:if>
	<xsl:value-of select="atom:name" />
      </atomrdf:name>

      <xsl:for-each select="atom:email" >
	<atomrdf:email>
	  <xsl:value-of select="." />
	</atomrdf:email>
      </xsl:for-each>
      
      <!-- handle Person Construct extensions -->
      <xsl:for-each select="*[namespace-uri() != $NSATOM]">
	<xsl:call-template name="Extension" />
      </xsl:for-each>

    </atomrdf:Person>
  </xsl:template>

  <!-- handle feed/entry -->
  <xsl:template name="Entry" >

    <!-- handle feed/entry/author -->
    <xsl:for-each select="atom:author">
      <xsl:call-template name="EntryAuthor" />
    </xsl:for-each>

    <!-- handle feed/entry/source -->
    <xsl:for-each select="atom:source">
      <xsl:call-template name="EntryHead" />
    </xsl:for-each>

    <atomrdf:Entry>

      <!-- set entry id -->
      <xsl:if test="atom:id">
	<xsl:attribute name="rdf:about">
	  <xsl:value-of select="atom:id" />
	</xsl:attribute>
      </xsl:if>

      <!-- dissociate entry from entry instance, so that different
	   instances don't get smushed together -->
      <atomrdf:entryInstance>
	<atomrdf:EntryInstance>

	  <!-- atomrdf:sourceFeed is used to link entries to main
	       feed, or embedded feed/entry/source -->
	  <xsl:choose>

	    <!-- if entry/source -->
	    <xsl:when test="atom:source">
	      <atomrdf:sourceFeed>
		<xsl:choose>
		  <xsl:when test="atom:source/atom:id">
		    <xsl:attribute name="rdf:resource">
		      <xsl:value-of select="atom:source/atom:id" />
		    </xsl:attribute>
		  </xsl:when>
		  <xsl:otherwise>
		    <xsl:attribute name="rdf:nodeID">
		      <xsl:text>feed</xsl:text><xsl:number count="/atom:feed/atom:entry" />
		    </xsl:attribute>
		  </xsl:otherwise>
		</xsl:choose>
	      </atomrdf:sourceFeed>
	    </xsl:when>

	    <!-- if no entry/source -->
	    <xsl:otherwise>
	      <atomrdf:sourceFeed>
		<xsl:choose>
		  <xsl:when test="/atom:feed/atom:id">
		    <xsl:attribute name="rdf:resource">
		      <xsl:value-of select="/atom:feed/atom:id" />
		    </xsl:attribute>
		  </xsl:when>
		  <xsl:otherwise>
		    <xsl:attribute name="rdf:nodeID">
		      <xsl:text>mainFeed</xsl:text>
		    </xsl:attribute>
		  </xsl:otherwise>
		</xsl:choose>
	      </atomrdf:sourceFeed>
	    </xsl:otherwise>
	  </xsl:choose>

	  <!-- link to feed -->
	  <atomrdf:containingFeed>
	    <xsl:choose>
	      <xsl:when test="/atom:feed/atom:id">
		<xsl:attribute name="rdf:resource">
		  <xsl:value-of select="/atom:feed/atom:id" />
		</xsl:attribute>
	      </xsl:when>
	      <xsl:otherwise>
		<xsl:attribute name="rdf:nodeID">
		  <xsl:text>mainFeed</xsl:text>
		</xsl:attribute>
	      </xsl:otherwise>
	    </xsl:choose>
	  </atomrdf:containingFeed>

	  <atomrdf:title>

	    <xsl:for-each select="atom:title">
	      <xsl:call-template name="Text" />
	    </xsl:for-each>

	  </atomrdf:title>

	  <xsl:for-each select="atom:link">
	    <xsl:call-template name="Link" />
	  </xsl:for-each>

	  <xsl:for-each select="atom:category" >
	    <xsl:call-template name="Category" />
	  </xsl:for-each>

	  <xsl:choose>

	    <!-- if there is an author then it overides the feed/entry/source/author -->
	    <xsl:when test="atom:author">
	      <xsl:for-each select="atom:author" >
		<atomrdf:author>
		  <xsl:attribute name="rdf:nodeID">
		    <xsl:text>entryAuthor</xsl:text><xsl:number count="/atom:feed/atom:entry"/>
		  </xsl:attribute>	    
		</atomrdf:author>
	      </xsl:for-each>
	    </xsl:when>
	    <xsl:otherwise>
	      
	      <!-- use the feed/entry/source/author if present -->
	      <xsl:choose>
		<xsl:when test="atom:source/atom:author">
		  <atomrdf:author>
		    <xsl:attribute name="rdf:nodeID">
		      <xsl:text>entryHeadAuthor</xsl:text><xsl:number count="/atom:feed/atom:entry" />
		    </xsl:attribute>
		  </atomrdf:author>
		</xsl:when>
		<xsl:otherwise>

		  <!-- use the default feed/author -->
		  <atomrdf:author rdf:nodeID="defaultAuthor" />
		</xsl:otherwise>
	      </xsl:choose>

	    </xsl:otherwise>
	  </xsl:choose>

	  <xsl:for-each select="atom:contributor" >

	    <atomrdf:contributor>

	      <atomrdf:Person>

		<xsl:for-each select="atom:uri" >
		  <atomrdf:uri>
		    <xsl:call-template name="resolveRef">
		      <xsl:with-param name="base">
			<xsl:call-template name="getXmlBase">
			  <xsl:with-param name="element" select="." />
			  <xsl:with-param name="default" select="$contentbase" />
			</xsl:call-template>
		      </xsl:with-param>
		      <xsl:with-param name="strict" select="$STRICT_REF_PARSING" />
		      <xsl:with-param name="ref" select="." />
		    </xsl:call-template>
		  </atomrdf:uri>
		</xsl:for-each>

		<atomrdf:name>
		  <xsl:variable name="lang">
		    <xsl:call-template name="getXmlLang">
		      <xsl:with-param name="element" select="atom:name" />
		      <xsl:with-param name="default" select="$contentlang" />
		    </xsl:call-template>
		  </xsl:variable>
		  <xsl:if test="$lang != ''">
		    <xsl:attribute name="xml:lang">
		      <xsl:value-of select="$lang" />
		    </xsl:attribute>
		  </xsl:if>
		  <xsl:value-of select="atom:name" />
		</atomrdf:name>

		<xsl:for-each select="atom:email" >
		  <atomrdf:email>
		    <xsl:value-of select="." />
		  </atomrdf:email>
		</xsl:for-each>

		<!-- handle Person Construct extensions -->
		<xsl:for-each select="*[namespace-uri() != $NSATOM]">
		  <xsl:call-template name="Extension" />
		</xsl:for-each>

	      </atomrdf:Person>

	    </atomrdf:contributor>
	  </xsl:for-each>

	  <xsl:for-each select="atom:updated" >
	    <atomrdf:updated>
	      <xsl:value-of select="." />
	    </atomrdf:updated>
	  </xsl:for-each>

	  <xsl:for-each select="atom:published" >
	    <atomrdf:published>
	      <xsl:value-of select="." />
	    </atomrdf:published>
	  </xsl:for-each>

	  <xsl:for-each select="atom:summary" >
	    <atomrdf:summary>
	      <xsl:call-template name="Text" />
	    </atomrdf:summary>
	  </xsl:for-each>


	  <xsl:for-each select="atom:content" >
	    <xsl:call-template name="Content" />
	  </xsl:for-each>

	  <xsl:for-each select="atom:copyright" >
	    <atomrdf:copyright>
	      <xsl:call-template name="Text" />
	    </atomrdf:copyright>
	  </xsl:for-each>

	  <!-- handle Entry extensions -->
	  <xsl:for-each select="*[namespace-uri() != $NSATOM]">
	    <xsl:call-template name="Extension" />
	  </xsl:for-each>

	</atomrdf:EntryInstance>
      </atomrdf:entryInstance>

    </atomrdf:Entry>

  </xsl:template>


  <!-- handle feed/entry/source -->
  <xsl:template name="EntryHead" >

    <!-- declare default author's for this head -->
    <xsl:for-each select="atom:author" >
      <xsl:call-template name="EntryHeadAuthor" />
    </xsl:for-each>

    <atomrdf:Feed>

      <!-- set feed id -->
      <xsl:choose>
	<!-- use atom:id -->
	<xsl:when test="atom:id">
	  <xsl:attribute name="rdf:about">
	    <xsl:value-of select="atom:id" />
	  </xsl:attribute>
	</xsl:when>
	<!-- create id -->
	<xsl:otherwise>
	  <xsl:attribute name="rdf:nodeID">
	    <xsl:text>feed</xsl:text><xsl:number count="/atom:feed/atom:entry" />
	  </xsl:attribute>
	</xsl:otherwise>
      </xsl:choose>
      
      <!-- handle the rest of feed/entry/source in the same way as feed -->
      <xsl:call-template name="Head" />
    </atomrdf:Feed>
  </xsl:template>

  <!-- handle feed and feed/entry/source -->
  <xsl:template name="Head">

    <atomrdf:title>

      <xsl:for-each select="atom:title">

	<xsl:call-template name="Text" />
      </xsl:for-each>

    </atomrdf:title>

    <xsl:for-each select="atom:link">
      <xsl:call-template name="Link" />
    </xsl:for-each>

    <xsl:for-each select="atom:image">
      <atomrdf:image>
	<atomrdf:Image>
	  <xsl:attribute name="rdf:about">
	    <xsl:call-template name="resolveRef">
	      <xsl:with-param name="base">
		<xsl:call-template name="getXmlBase">
		  <xsl:with-param name="element" select="." />
		  <xsl:with-param name="default" select="$contentbase" />
		</xsl:call-template>
	      </xsl:with-param>
	      <xsl:with-param name="strict" select="$STRICT_REF_PARSING" />
	      <xsl:with-param name="ref" select="node()" />
	    </xsl:call-template>
	  </xsl:attribute>
	</atomrdf:Image>
      </atomrdf:image>
    </xsl:for-each>

    <xsl:for-each select="atom:icon">
      <atomrdf:icon>
	<atomrdf:Icon>
	  <xsl:attribute name="rdf:about">
	    <xsl:call-template name="resolveRef">
	      <xsl:with-param name="base">
		<xsl:call-template name="getXmlBase">
		  <xsl:with-param name="element" select="." />
		  <xsl:with-param name="default" select="$contentbase" />
		</xsl:call-template>
	      </xsl:with-param>
	      <xsl:with-param name="strict" select="$STRICT_REF_PARSING" />
	      <xsl:with-param name="ref" select="node()" />
	    </xsl:call-template>
	  </xsl:attribute>
	</atomrdf:Icon>
      </atomrdf:icon>
    </xsl:for-each>

    <xsl:for-each select="atom:category" >
      <xsl:call-template name="Category" />
    </xsl:for-each>

    <!-- don't put atom:feed/atom:author here - it is just a shortcut
	 for atom:entry/atom:author -->

    <xsl:for-each select="atom:contributor" >

      <atomrdf:contributor>
	<atomrdf:Person>

	  <xsl:for-each select="atom:uri" >
	    <atomrdf:uri>
	      <xsl:call-template name="resolveRef">
		<xsl:with-param name="base">
		  <xsl:call-template name="getXmlBase">
		    <xsl:with-param name="element" select="." />
		    <xsl:with-param name="default" select="$contentbase" />
		  </xsl:call-template>
		</xsl:with-param>
		<xsl:with-param name="strict" select="$STRICT_REF_PARSING" />
		<xsl:with-param name="ref" select="." />
	      </xsl:call-template>
	    </atomrdf:uri>
	  </xsl:for-each>

	  <atomrdf:name>
	    <xsl:variable name="lang">
	      <xsl:call-template name="getXmlLang">
		<xsl:with-param name="element" select="atom:name" />
		<xsl:with-param name="default" select="$contentlang" />
	      </xsl:call-template>
	    </xsl:variable>
	    <xsl:if test="$lang != ''">
	      <xsl:attribute name="xml:lang">
		<xsl:value-of select="$lang" />
	      </xsl:attribute>
	    </xsl:if>
	    <xsl:value-of select="atom:name" />
	  </atomrdf:name>

	  <xsl:for-each select="atom:email" >
	    <atomrdf:email>
	      <xsl:value-of select="." />
	    </atomrdf:email>
	  </xsl:for-each>

	  <!-- handle Person Construct extensions -->
	  <xsl:for-each select="*[namespace-uri() != $NSATOM]">
	    <xsl:call-template name="Extension" />
	  </xsl:for-each>

	</atomrdf:Person>
      </atomrdf:contributor>
    </xsl:for-each>

    <xsl:for-each select="atom:subtitle" >
      <atomrdf:subtitle>
	<xsl:call-template name="Text" />
      </atomrdf:subtitle>
    </xsl:for-each>

    <xsl:for-each select="atom:generator" >
      <atomrdf:generator>
	<atomrdf:Generator>
	  
	  <atomrdf:generatorName>
	    <xsl:value-of select="." />
	  </atomrdf:generatorName>
	  
	  <xsl:if test="@uri">
	    <atomrdf:generatorUri>
	      <xsl:attribute name="rdf:resource">
		<xsl:call-template name="resolveRef">
		  <xsl:with-param name="base">
		    <xsl:call-template name="getXmlBase">
		      <xsl:with-param name="element" select="." />
		      <xsl:with-param name="default" select="$contentbase" />
		    </xsl:call-template>
		  </xsl:with-param>
		  <xsl:with-param name="strict" select="$STRICT_REF_PARSING" />
		  <xsl:with-param name="ref" select="@uri" />
		</xsl:call-template>
	      </xsl:attribute>
	    </atomrdf:generatorUri>
	  </xsl:if>
	  
	  <xsl:if test="@version">
	    <atomrdf:generatorVersion>
	      <xsl:value-of select="@version" />
	    </atomrdf:generatorVersion>
	  </xsl:if>

	</atomrdf:Generator>
      </atomrdf:generator>
    </xsl:for-each>

    <xsl:for-each select="atom:copyright" >
      <atomrdf:copyright>
	<xsl:call-template name="Text" />
      </atomrdf:copyright>
    </xsl:for-each>

    <atomrdf:updated>
      <xsl:value-of select="atom:updated" />
    </atomrdf:updated>

    <!-- handle head extensions -->
    <xsl:for-each select="*[namespace-uri() != $NSATOM]">
      <xsl:call-template name="Extension" />
    </xsl:for-each>

  </xsl:template>

  <!-- handle feed/entry/source/author -->
  <xsl:template name="EntryHeadAuthor">

    <atomrdf:Person>

      <xsl:attribute name="rdf:nodeID">
	<xsl:text>entryHeadAuthor</xsl:text><xsl:number count="/atom:feed/atom:entry"/>
      </xsl:attribute>

      <xsl:for-each select="atom:uri" >
	<atomrdf:uri>
	  <xsl:call-template name="resolveRef">
	    <xsl:with-param name="base">
	      <xsl:call-template name="getXmlBase">
		<xsl:with-param name="element" select="." />
		<xsl:with-param name="default" select="$contentbase" />
	      </xsl:call-template>
	    </xsl:with-param>
	    <xsl:with-param name="strict" select="$STRICT_REF_PARSING" />
	    <xsl:with-param name="ref" select="." />
	  </xsl:call-template>
	</atomrdf:uri>
      </xsl:for-each>

      <atomrdf:name>
	<xsl:variable name="lang">
	  <xsl:call-template name="getXmlLang">
	    <xsl:with-param name="element" select="atom:name" />
	    <xsl:with-param name="default" select="$contentlang" />
	  </xsl:call-template>
	</xsl:variable>
	<xsl:if test="$lang != ''">
	  <xsl:attribute name="xml:lang">
	    <xsl:value-of select="$lang" />
	  </xsl:attribute>
	</xsl:if>
	<xsl:value-of select="atom:name" />
      </atomrdf:name>

      <xsl:for-each select="atom:email" >
	<atomrdf:email>
	  <xsl:value-of select="." />
	</atomrdf:email>
      </xsl:for-each>

      <!-- handle Person Construct extensions -->
      <xsl:for-each select="*[namespace-uri() != $NSATOM]">
	<xsl:call-template name="Extension" />
      </xsl:for-each>
      
    </atomrdf:Person>
  </xsl:template>

  <!-- handle Content Constructs -->
  <xsl:template name="Content">

    <atomrdf:content>

      <xsl:variable name="base">
	<xsl:call-template name="getXmlBase">
	  <xsl:with-param name="element" select="." />
	  <xsl:with-param name="default" select="$contentbase" />
	</xsl:call-template>
      </xsl:variable>

      <atomrdf:Text>
	<xsl:choose>
	  <!-- handle external content -->
	  <xsl:when test="@src">
	    <atomrdf:textSource>
	      <xsl:attribute name="rdf:resource">
		<xsl:call-template name="resolveRef">
		  <xsl:with-param name="base" select="$base" />
		  <xsl:with-param name="strict" select="$STRICT_REF_PARSING" />
		  <xsl:with-param name="ref" select="@src" />
		</xsl:call-template>
	      </xsl:attribute>
	    </atomrdf:textSource>
	    <xsl:if test="@type">
	      <atomrdf:mimeType>
		<xsl:value-of select="@type" />
	      </atomrdf:mimeType>
	    </xsl:if>
	    <atomrdf:textType>external</atomrdf:textType>
	  </xsl:when>

	  <!-- handle text content -->
	  <xsl:when test="not(@type) or @type='text'">
	    <!-- preserve xml:lang context -->
	    <xsl:variable name="lang">
	      <xsl:call-template name="getXmlLang">
		<xsl:with-param name="element" select="." />
		<xsl:with-param name="default" select="$contentlang" />
	      </xsl:call-template>
	    </xsl:variable>
	    <xsl:if test="$lang != ''">
	      <atomrdf:contentLang>
		<xsl:value-of select="$lang" />
	      </atomrdf:contentLang>
	    </xsl:if>

	    <rdf:value>
	      <!-- don't include containing element -->
	      <xsl:copy-of select="node()" />
	    </rdf:value>
	    <atomrdf:textType>text</atomrdf:textType>
	  </xsl:when>

	  <!-- handle html content -->
	  <xsl:when test="@type='html'">
	    <!-- preserve xml:lang context -->
	    <xsl:variable name="lang">
	      <xsl:call-template name="getXmlLang">
		<xsl:with-param name="element" select="." />
		<xsl:with-param name="default" select="$contentlang" />
	      </xsl:call-template>
	    </xsl:variable>
	    <xsl:if test="$lang != ''">
	      <atomrdf:contentLang>
		<xsl:value-of select="$lang" />
	      </atomrdf:contentLang>
	    </xsl:if>

	    <!-- preserve xml:base context -->
	    <xsl:if test="not($base='')">
	      <atomrdf:contentBase>
		<xsl:value-of select="$base" />
	      </atomrdf:contentBase>
	    </xsl:if>

	    <rdf:value>
	      <!-- don't include containing element -->
	      <xsl:copy-of select="node()" />
	    </rdf:value>
	    <atomrdf:textType>html</atomrdf:textType>
	  </xsl:when>

	  <!-- handle xhtml content -->
	  <xsl:when test="@type='xhtml'">
	    <!-- preserve xml:lang context -->
	    <!-- get lang from the xhtml:div -->
	    <xsl:variable name="lang">
	      <xsl:call-template name="getXmlLang">
		<xsl:with-param name="element" select="xhtml:div" />
		<xsl:with-param name="default" select="$contentlang" />
	      </xsl:call-template>
	    </xsl:variable>
	    <xsl:if test="$lang != ''">
	      <atomrdf:contentLang>
		<xsl:value-of select="$lang" />
	      </atomrdf:contentLang>
	    </xsl:if>

	    <!-- preserve xml:base context -->
	    <xsl:if test="not($base='')">
	      <atomrdf:contentBase>
		<xsl:value-of select="$base" />
	      </atomrdf:contentBase>
	    </xsl:if>

	    <rdf:value rdf:parseType="Literal">
	      <!-- don't include containing element -->	      
	      <xsl:copy-of select="xhtml:div/node()" />
	    </rdf:value>
	    <atomrdf:textType>xhtml</atomrdf:textType>
	  </xsl:when>

	  <!-- handle XML content -->
	  <xsl:when test="(substring(@type, string-length(@type) - 3) = '+xml') or (substring(@type, string-length(@type) - 3) = '/xml')">
	    <!-- preserve xml:lang context -->
	    <xsl:variable name="lang">
	      <xsl:call-template name="getXmlLang">
		<xsl:with-param name="element" select="." />
		<xsl:with-param name="default" select="$contentlang" />
	      </xsl:call-template>
	    </xsl:variable>
	    <xsl:if test="$lang != ''">
	      <atomrdf:contentLang>
		<xsl:value-of select="$lang" />
	      </atomrdf:contentLang>
	    </xsl:if>

	    <!-- preserve xml:base context -->
	    <xsl:if test="not($base='')">
	      <atomrdf:contentBase>
		<xsl:value-of select="$base" />
	      </atomrdf:contentBase>
	    </xsl:if>

	    <rdf:value rdf:parseType="Literal">
	      <!-- don't include containing element -->
	      <xsl:copy-of select="node()" />
	    </rdf:value>
	    <atomrdf:textType>XML</atomrdf:textType>
	    <atomrdf:mimeType>
	      <xsl:value-of select="@type" />
	    </atomrdf:mimeType>
	  </xsl:when>

	  <!-- handle text/* content -->
	  <xsl:when test="starts-with(@type, 'text/')">
	    <!-- preserve xml:lang context -->
	    <xsl:variable name="lang">
	      <xsl:call-template name="getXmlLang">
		<xsl:with-param name="element" select="." />
		<xsl:with-param name="default" select="$contentlang" />
	      </xsl:call-template>
	    </xsl:variable>
	    <xsl:if test="$lang != ''">
	      <atomrdf:contentLang>
		<xsl:value-of select="$lang" />
	      </atomrdf:contentLang>
	    </xsl:if>

	    <rdf:value>
	      <!-- don't include containing element -->
	      <xsl:copy-of select="node()" />
	    </rdf:value>
	    <atomrdf:textType>text</atomrdf:textType>
	    <atomrdf:mimeType>
	      <xsl:value-of select="@type" />
	    </atomrdf:mimeType>
	  </xsl:when>

	  <!-- handle inline binary content -->
	  <xsl:otherwise>
	    <!-- preserve xml:base context -->
	    <xsl:if test="not($base='')">
	      <atomrdf:contentBase>
		<xsl:value-of select="$base" />
	      </atomrdf:contentBase>
	    </xsl:if>
	    <rdf:value>
	      <!-- don't include containing element -->
	      <xsl:copy-of select="node()" />
	    </rdf:value>
	    <atomrdf:textType>BASE64</atomrdf:textType>
	    <atomrdf:mimeType>
	      <xsl:value-of select="@type" />
	    </atomrdf:mimeType>
	  </xsl:otherwise>
	</xsl:choose>
      </atomrdf:Text>

    </atomrdf:content>
  </xsl:template>

  <!-- handle text constructs -->
  <xsl:template name="Text">

    <xsl:variable name="base">
      <xsl:call-template name="getXmlBase">
	<xsl:with-param name="element" select="." />
	<xsl:with-param name="default" select="$contentbase" />
      </xsl:call-template>
    </xsl:variable>

    <atomrdf:Text>
      <xsl:choose>
	<!-- handle text content -->
	<xsl:when test="not(@type) or @type='text'">
	  <!-- preserve xml:lang context -->
	  <xsl:variable name="lang">
	    <xsl:call-template name="getXmlLang">
	      <xsl:with-param name="element" select="." />
	      <xsl:with-param name="default" select="$contentlang" />
	    </xsl:call-template>
	  </xsl:variable>
	  <xsl:if test="$lang != ''">
	    <atomrdf:contentLang>
	      <xsl:value-of select="$lang" />
	    </atomrdf:contentLang>
	  </xsl:if>

	  <rdf:value>
	    <xsl:value-of select="." />
	  </rdf:value>
	  <atomrdf:textType>text</atomrdf:textType>
	</xsl:when>

	<!-- handle html content -->
	<xsl:when test="@type='html'">
	  <!-- preserve xml:lang context -->
	  <xsl:variable name="lang">
	    <xsl:call-template name="getXmlLang">
	      <xsl:with-param name="element" select="." />
	      <xsl:with-param name="default" select="$contentlang" />
	    </xsl:call-template>
	  </xsl:variable>
	  <xsl:if test="$lang != ''">
	    <atomrdf:contentLang>
	      <xsl:value-of select="$lang" />
	    </atomrdf:contentLang>
	  </xsl:if>

	  <!-- preserve xml:base context -->
	  <xsl:if test="not($base='')">
	    <atomrdf:contentBase>
	      <xsl:value-of select="$base" />
	    </atomrdf:contentBase>
	  </xsl:if>
	  <rdf:value>
	    <xsl:value-of select="." />
	  </rdf:value>
	  <atomrdf:textType>html</atomrdf:textType>
	</xsl:when>

	<!-- handle xhtml content -->
	<xsl:when test="@type='xhtml'">
	  <!-- preserve xml:lang context -->
	  <!-- get lang from the xhtml:div -->
	  <xsl:variable name="lang">
	    <xsl:call-template name="getXmlLang">
	      <xsl:with-param name="element" select="xhtml:div" />
	      <xsl:with-param name="default" select="$contentlang" />
	    </xsl:call-template>
	  </xsl:variable>
	  <xsl:if test="$lang != ''">
	    <atomrdf:contentLang>
	      <xsl:value-of select="$lang" />
	    </atomrdf:contentLang>
	  </xsl:if>

	  <!-- preserve xml:base context -->
	  <xsl:if test="not($base='')">
	    <atomrdf:contentBase>
	      <xsl:value-of select="$base" />
	    </atomrdf:contentBase>
	  </xsl:if>
	  <rdf:value rdf:parseType="Literal">
	    <!-- don't include containing element -->
	    <xsl:copy-of select="xhtml:div/node()" />
	  </rdf:value>
	  <atomrdf:textType>xhtml</atomrdf:textType>
	</xsl:when>
      </xsl:choose>	
    </atomrdf:Text>
  </xsl:template>

  <!-- Extension Construct -->
  <xsl:template name="Extension">

    <xsl:choose>
      <!-- Structured Extension Construct -->
      <xsl:when test="@* or ./*">
	<atomrdf:extension>
	  <atomrdf:Extension>

	    <xsl:variable name="base">
	      <xsl:call-template name="getXmlBase">
		<xsl:with-param name="element" select="." />
		<xsl:with-param name="default" select="$contentbase" />
	      </xsl:call-template>
	    </xsl:variable>
	    <xsl:if test="not($base='')">
	      <atomrdf:contentBase>
		<xsl:value-of select="$base" />
	      </atomrdf:contentBase>
	    </xsl:if>

	    <xsl:variable name="lang">
	      <xsl:call-template name="getXmlLang">
		<xsl:with-param name="element" select="." />
		<xsl:with-param name="default" select="$contentlang" />
	      </xsl:call-template>
	    </xsl:variable>
	    <xsl:if test="$lang != ''">
	      <atomrdf:contentLang>
		<xsl:value-of select="$lang" />
	      </atomrdf:contentLang>
	    </xsl:if>

	    <rdf:value rdf:parseType="Literal" >
	      <!-- include containing element -->
	      <!-- remove xml:base and xml:lang, they're handled above -->
	      <xsl:copy>
		<xsl:copy-of select="@*[not((name() = 'xml:base') or (name() = 'xml:lang'))]" />
		<!-- NOTE: urgh QNames (tho safe here) why doesn't
		     not(@xml:base) work?? -->
		<xsl:copy-of select="*" /> 
	      </xsl:copy>

	    </rdf:value>
	  </atomrdf:Extension>
	</atomrdf:extension>
      </xsl:when>
      <xsl:otherwise>
	<!-- Simple Extension Construct -->
	<xsl:copy>
	  <xsl:value-of select="." />
	</xsl:copy>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


  <!-- handle Link constructs -->
  <xsl:template name="Link">

    <atomrdf:link>
      <atomrdf:Link>

	<atomrdf:rel>
	  <xsl:attribute name="rdf:resource">
	    <xsl:choose>

	      <!-- default rel is 'alternate' -->
	      <xsl:when test="not(@rel)">
		<xsl:text>http://www.iana.org/assignments/relation/alternate</xsl:text>
	      </xsl:when>

	      <!-- if a URI -->
	      <xsl:when test="contains(., ':')">
		<xsl:value-of select="@rel" />
	      </xsl:when>

	      <!-- otherwise use IANA namespace -->
	      <xsl:otherwise>
		<xsl:text>http://www.iana.org/assignments/relation/</xsl:text><xsl:value-of select="@rel" />
	      </xsl:otherwise>
	    </xsl:choose>
	  </xsl:attribute>
	</atomrdf:rel>
	
	<xsl:variable name="linkType" select="@type" />
	<xsl:if test="$linkType">
	  <atomrdf:linkType>
	    <xsl:value-of select="$linkType" />
	  </atomrdf:linkType>
	</xsl:if>
	
	<atomrdf:linkHref>
	  <xsl:attribute name="rdf:resource">
	    <xsl:call-template name="resolveRef">
	      <xsl:with-param name="base">
		<xsl:call-template name="getXmlBase">
		  <xsl:with-param name="element" select="." />
		  <xsl:with-param name="default" select="$contentbase" />
		</xsl:call-template>
	      </xsl:with-param>
	      <xsl:with-param name="strict" select="$STRICT_REF_PARSING" />
	      <xsl:with-param name="ref" select="@href" />
	    </xsl:call-template>
	  </xsl:attribute>
	</atomrdf:linkHref>
	
	<xsl:variable name="linkHrefLang" select="@hreflang" />
	<xsl:if test="$linkHrefLang">
	  <atomrdf:linkHrefLang>
	    <xsl:value-of select="$linkHrefLang" />
	  </atomrdf:linkHrefLang>
	</xsl:if>

	<xsl:variable name="linkTitle" select="@title" />
	<xsl:if test="$linkTitle">
	  <atomrdf:linkTitle>
	    <xsl:variable name="lang">
	      <xsl:call-template name="getXmlLang">
		<xsl:with-param name="element" select="." />
		<xsl:with-param name="default" select="$contentlang" />
	      </xsl:call-template>
	    </xsl:variable>
	    <xsl:if test="$lang != ''">
	      <xsl:attribute name="xml:lang">
		<xsl:value-of select="$lang" />
	      </xsl:attribute>
	    </xsl:if>
	    <xsl:value-of select="$linkTitle" />
	  </atomrdf:linkTitle>
	</xsl:if>

	<xsl:variable name="linkLength" select="@length" />
	<xsl:if test="$linkLength">
	  <atomrdf:linkLength>
	    <xsl:value-of select="$linkLength" />
	  </atomrdf:linkLength>
	</xsl:if>

      </atomrdf:Link>
    </atomrdf:link>
  </xsl:template>

  <!-- handle Category constructs -->
  <xsl:template name="Category">

    <atomrdf:category>
      <atomrdf:Category>
	
	<atomrdf:categoryTerm>
	  <xsl:value-of select="@term" />
	</atomrdf:categoryTerm>

	<xsl:variable name="categoryScheme" select="@scheme" />
	<xsl:if test="$categoryScheme">	      
	  <atomrdf:categoryScheme>
	    <xsl:value-of select="$categoryScheme" />
	  </atomrdf:categoryScheme>
	</xsl:if>

	<xsl:variable name="categoryLabel" select="@label" />
	<xsl:if test="$categoryLabel">	      
	  <atomrdf:categoryLabel>
	    <xsl:variable name="lang">
	      <xsl:call-template name="getXmlLang">
		<xsl:with-param name="element" select="." />
		<xsl:with-param name="default" select="$contentlang" />
	      </xsl:call-template>
	    </xsl:variable>
	    <xsl:if test="$lang != ''">
	      <xsl:attribute name="xml:lang">
		<xsl:value-of select="$lang" />
	      </xsl:attribute>
	    </xsl:if>
	    <xsl:value-of select="$categoryLabel" />
	  </atomrdf:categoryLabel>
	</xsl:if>

      </atomrdf:Category>
    </atomrdf:category>
  </xsl:template>

  <!-- drop everything else -->
  <xsl:template match="*" />

</xsl:stylesheet>
