<?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-05"
		xmlns:atomrdf="http://djpowell.net/schemas/atomrdf/0.1-EXPERIMENTAL/"
		xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
		>

  <!-- Experimental Atom to RDF mapping - DO NOT DEPLOY -->
  <!-- $Id: atom2rdf-05.xsl,v 1.1 2005/03/16 21:46:42 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-05'" />

  <!-- 

Notes:

The implementation is deliberately verbose and mechanical to make it
easier to understand and update.  It is based on draft-05 and
PaceExtensionConstruct.

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

Simple Extension constructs don't currently preserve xml:lang, but
this can be changed.

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

Implementations that know how to handle a particular Structured
Extension construct can 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.

Handles xml:lang, preserve it for Structured Extension Constructs

Allows "contentlang" parameter for xml:lang default (if allowed by
atom)

  -->

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

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

  <!-- Entry point -->
  <xsl:template match="/atom:feed" >
    <!-- resolve xml:base -->
    <xsl:variable name="xbase">
      <xsl:choose>
	<xsl:when test="$contentbase">
	  <xsl:choose>
	    <xsl:when test="@xml:base">
	      <xsl:call-template name="resolveRef">
		<xsl:with-param name="base" select="$contentbase" />
		<xsl:with-param name="ref" select="@xml:base" />
		<xsl:with-param name="strict" select="true" />
	      </xsl:call-template>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$contentbase" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of select="@xml:base" />
	</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:variable name="lang">
      <xsl:choose>
	<xsl:when test="@xml:lang">
	  <xsl:value-of select="@xml:lang" />
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of select="$contentlang" />
	</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <rdf:RDF>
      <xsl:attribute name="xml:base">
	<xsl:value-of select="$xbase" />
      </xsl:attribute>

      <!-- process feed head -->
      <xsl:for-each select="atom:head">
	<xsl:call-template name="FeedHead">
	  <xsl:with-param name="base" select="$xbase" />
	  <xsl:with-param name="lang" >
	    <xsl:choose>
	      <xsl:when test="@xml:lang">
		<xsl:value-of select="@xml:lang" />
	      </xsl:when>
	      <xsl:otherwise>
		<xsl:value-of select="$lang" />
	      </xsl:otherwise>
	    </xsl:choose>
	  </xsl:with-param>
	</xsl:call-template>
      </xsl:for-each>

      <!-- process entries -->
      <xsl:for-each select="atom:entry">
	<xsl:call-template name="Entry">
	  <xsl:with-param name="base" select="$xbase" />
	  <xsl:with-param name="lang" >
	    <xsl:choose>
	      <xsl:when test="@xml:lang">
		<xsl:value-of select="@xml:lang" />
	      </xsl:when>
	      <xsl:otherwise>
		<xsl:value-of select="$lang" />
	      </xsl:otherwise>
	    </xsl:choose>
	  </xsl:with-param>
	</xsl:call-template>
      </xsl:for-each>

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

  <!-- handle feed/head/author -->
  <xsl:template name="DefaultAuthor">
    <xsl:param name="base" />
    <xsl:param name="lang" />

    <!-- resolve xml:base -->
    <xsl:variable name="xbase">
      <xsl:choose>
	<xsl:when test="$base">
	  <xsl:choose>
	    <xsl:when test="@xml:base">
	      <xsl:call-template name="resolveRef">
		<xsl:with-param name="base" select="$base" />
		<xsl:with-param name="ref" select="@xml:base" />
		<xsl:with-param name="strict" select="true" />
	      </xsl:call-template>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$base" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of select="@xml:base" />
	</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <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="resolveRef">
		<xsl:with-param name="base" select="$xbase" />
		<xsl:with-param name="strict" select="true" />
		<xsl:with-param name="ref" select="@xml:base" />
	      </xsl:call-template>
	    </xsl:with-param>
	    <xsl:with-param name="strict" select="true" />
	    <xsl:with-param name="ref" select="." />
	  </xsl:call-template>
	</atomrdf:uri>
      </xsl:for-each>

      <atomrdf:name>
	<xsl:attribute name="xml:lang">
	  <xsl:choose>
	    <xsl:when test="@xml:lang">
	      <xsl:value-of select="@xml:lang" />
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$lang" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:attribute>
	<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:with-param name="base" select="$base" />
	  <xsl:with-param name="lang">
	    <xsl:choose>
	      <xsl:when test="@xml:lang">
		<xsl:value-of select="@xml:lang" />
	      </xsl:when>
	      <xsl:otherwise>
		<xsl:value-of select="$lang" />
	      </xsl:otherwise>
	    </xsl:choose>
	  </xsl:with-param>
	</xsl:call-template>
      </xsl:for-each>

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

  <!-- handle feed/entry/author -->
  <xsl:template name="EntryAuthor">
    <xsl:param name="base" />
    <xsl:param name="lang" />

    <!-- resolve xml:base -->
    <xsl:variable name="xbase">
      <xsl:choose>
	<xsl:when test="$base">
	  <xsl:choose>
	    <xsl:when test="@xml:base">
	      <xsl:call-template name="resolveRef">
		<xsl:with-param name="base" select="$base" />
		<xsl:with-param name="ref" select="@xml:base" />
		<xsl:with-param name="strict" select="true" />
	      </xsl:call-template>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$base" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of select="@xml:base" />
	</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <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="resolveRef">
		<xsl:with-param name="base" select="$xbase" />
		<xsl:with-param name="strict" select="true" />
		<xsl:with-param name="ref" select="@xml:base" />
	      </xsl:call-template>
	    </xsl:with-param>
	    <xsl:with-param name="strict" select="true" />
	    <xsl:with-param name="ref" select="." />
	  </xsl:call-template>
	</atomrdf:uri>
      </xsl:for-each>

      <atomrdf:name>
	<xsl:attribute name="xml:lang">
	  <xsl:choose>
	    <xsl:when test="@xml:lang">
	      <xsl:value-of select="@xml:lang" />
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$lang" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:attribute>
	<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:with-param name="base" select="$base" />
	  <xsl:with-param name="lang">
	    <xsl:choose>
	      <xsl:when test="@xml:lang">
		<xsl:value-of select="@xml:lang" />
	      </xsl:when>
	      <xsl:otherwise>
		<xsl:value-of select="$lang" />
	      </xsl:otherwise>
	    </xsl:choose>
	  </xsl:with-param>
	</xsl:call-template>
      </xsl:for-each>

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

  <!-- handle feed/head -->
  <xsl:template name="FeedHead" >
    <xsl:param name="base" />
    <xsl:param name="lang" />

    <!-- resolve xml:base -->
    <xsl:variable name="xbase">
      <xsl:choose>
	<xsl:when test="$base">
	  <xsl:choose>
	    <xsl:when test="@xml:base">
	      <xsl:call-template name="resolveRef">
		<xsl:with-param name="base" select="$base" />
		<xsl:with-param name="ref" select="@xml:base" />
		<xsl:with-param name="strict" select="true" />
	      </xsl:call-template>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$base" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of select="@xml:base" />
	</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <!-- handle default feed/head/author -->
    <xsl:for-each select="atom:author" >
      <xsl:call-template name="DefaultAuthor">
	<xsl:with-param name="base" select="$xbase" />
	<xsl:with-param name="lang">
	  <xsl:choose>
	    <xsl:when test="@xml:lang">
	      <xsl:value-of select="@xml:lang" />
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$lang" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:with-param>
      </xsl:call-template>
    </xsl:for-each>

    <atomrdf:Feed>
      
      <xsl:choose>
	<xsl:when test="atom:id">
	  <xsl:attribute name="rdf:about">
	    <xsl:value-of select="atom:id" />
	  </xsl:attribute>
	</xsl:when>
	<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>

	  <!-- not used for entry heads -->
	  <xsl:variable name="feedVersion" select="../@version" />
	  <xsl:if test="$feedVersion">
	    <atomrdf:feedVersion>
	      <xsl:value-of select="$feedVersion" />
	    </atomrdf:feedVersion>
	  </xsl:if>

	  <!-- handle the rest of feed/head in the same way as feed/entry/head -->
	  <xsl:call-template name="Head">
	    <xsl:with-param name="base" select="$xbase" />
	    <xsl:with-param name="lang">
	      <xsl:choose>
		<xsl:when test="@xml:lang">
		  <xsl:value-of select="@xml:lang" />
		</xsl:when>
		<xsl:otherwise>
		  <xsl:value-of select="$lang" />
		</xsl:otherwise>
	      </xsl:choose>
	    </xsl:with-param>
	  </xsl:call-template>

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

    </atomrdf:Feed>
  </xsl:template>

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

    <!-- resolve xml:base -->
    <xsl:variable name="xbase">
      <xsl:choose>
	<xsl:when test="$base">
	  <xsl:choose>
	    <xsl:when test="@xml:base">
	      <xsl:call-template name="resolveRef">
		<xsl:with-param name="base" select="$base" />
		<xsl:with-param name="ref" select="@xml:base" />
		<xsl:with-param name="strict" select="true" />
	      </xsl:call-template>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$base" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of select="@xml:base" />
	</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <!-- handle feed/entry/author -->
    <xsl:for-each select="atom:author">
      <xsl:call-template name="EntryAuthor">
	<xsl:with-param name="base" select="$xbase" />
	<xsl:with-param name="lang">
	  <xsl:choose>
	    <xsl:when test="@xml:lang">
	      <xsl:value-of select="@xml:lang" />
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$lang" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:with-param>
      </xsl:call-template>
    </xsl:for-each>

    <!-- handle feed/entry/head -->
    <xsl:for-each select="atom:head">
      <xsl:call-template name="EntryHead">
	<xsl:with-param name="base" select="$xbase" />
	<xsl:with-param name="lang">
	  <xsl:choose>
	    <xsl:when test="@xml:lang">
	      <xsl:value-of select="@xml:lang" />
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$lang" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:with-param>
      </xsl:call-template>
    </xsl:for-each>

    <atomrdf:Entry>

      <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:originFeed is used to link entries to main
	       feed/head, or embedded feed/entry/head -->
	  <xsl:choose>

	    <!-- if entry/head -->
	    <xsl:when test="atom:head">
	      <atomrdf:originFeed>
		<xsl:choose>
		  <xsl:when test="atom:head/atom:id">
		    <xsl:attribute name="rdf:resource">
		      <xsl:value-of select="atom:head/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:originFeed>
	    </xsl:when>

	    <!-- if no entry/head -->
	    <xsl:otherwise>
	      <atomrdf:originFeed>
		<xsl:choose>
		  <xsl:when test="/atom:feed/atom:head/atom:id">
		    <xsl:attribute name="rdf:resource">
		      <xsl:value-of select="/atom:feed/atom:head/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:originFeed>
	    </xsl:otherwise>
	  </xsl:choose>

	  <!-- link to feed/head -->
	  <atomrdf:containingFeed>
	    <xsl:choose>
	      <xsl:when test="/atom:feed/atom:head/atom:id">
		<xsl:attribute name="rdf:resource">
		  <xsl:value-of select="/atom:feed/atom:head/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:with-param name="base" select="$xbase" />
		<xsl:with-param name="lang">
		  <xsl:choose>
		    <xsl:when test="@xml:lang">
		      <xsl:value-of select="@xml:lang" />
		    </xsl:when>
		    <xsl:otherwise>
		      <xsl:value-of select="$lang" />
		    </xsl:otherwise>
		  </xsl:choose>
		</xsl:with-param>
	      </xsl:call-template>
	    </xsl:for-each>

	  </atomrdf:title>

	  <xsl:for-each select="atom:link">
	    <xsl:call-template name="Link">
	      <xsl:with-param name="base" select="$xbase" />
	      <xsl:with-param name="lang">
		<xsl:choose>
		  <xsl:when test="@xml:lang">
		    <xsl:value-of select="@xml:lang" />
		  </xsl:when>
		  <xsl:otherwise>
		    <xsl:value-of select="$lang" />
		  </xsl:otherwise>
		</xsl:choose>
	      </xsl:with-param>
	    </xsl:call-template>
	  </xsl:for-each>

	  <xsl:for-each select="atom:category" >
	    <xsl:call-template name="Category">
	      <!-- note categories don't use xml:base -->
	      <xsl:with-param name="lang">
		<xsl:choose>
		  <xsl:when test="@xml:lang">
		    <xsl:value-of select="@xml:lang" />
		  </xsl:when>
		  <xsl:otherwise>
		    <xsl:value-of select="$lang" />
		  </xsl:otherwise>
		</xsl:choose>
	      </xsl:with-param>
	    </xsl:call-template>
	  </xsl:for-each>

	  <xsl:for-each select="atom:edit" >
	    <atomrdf:edit>
	      <xsl:attribute name="rdf:resource">
		<xsl:call-template name="resolveRef">
		  <xsl:with-param name="base">
		    <xsl:call-template name="resolveRef">
		      <xsl:with-param name="base" select="$xbase" />
		      <xsl:with-param name="strict" select="true" />
		      <xsl:with-param name="ref" select="@xml:base" />
		    </xsl:call-template>
		  </xsl:with-param>
		  <xsl:with-param name="strict" select="true" />
		  <xsl:with-param name="ref" select="@href" />
		</xsl:call-template>
	      </xsl:attribute>
	    </atomrdf:edit>
	  </xsl:for-each>

	  <xsl:choose>

	    <!-- if there is an author then it overides the feed/entry/head/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/head/author if present -->
	      <xsl:choose>
		<xsl:when test="atom:head/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/head/author -->
		  <atomrdf:author rdf:nodeID="defaultAuthor" />
		</xsl:otherwise>
	      </xsl:choose>

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

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

	    <xsl:variable name="lang2">
	      <xsl:choose>
		<xsl:when test="@xml:lang">
		  <xsl:value-of select="@xml:lang" />
		</xsl:when>
		<xsl:otherwise>
		  <xsl:value-of select="$lang" />
		</xsl:otherwise>
	      </xsl:choose>
	    </xsl:variable>
	    
	    <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="resolveRef">
			  <xsl:with-param name="base" select="$xbase" />
			  <xsl:with-param name="strict" select="true" />
			  <xsl:with-param name="ref" select="@xml:base" />
			</xsl:call-template>
		      </xsl:with-param>
		      <xsl:with-param name="strict" select="true" />
		      <xsl:with-param name="ref" select="." />
		    </xsl:call-template>
		  </atomrdf:uri>
		</xsl:for-each>

		<atomrdf:name>
		  <xsl:attribute name="xml:lang">
		    <xsl:choose>
		      <xsl:when test="@xml:lang">
			<xsl:value-of select="@xml:lang" />
		      </xsl:when>
		      <xsl:otherwise>
			<xsl:value-of select="$lang2" />
		      </xsl:otherwise>
		    </xsl:choose>
		  </xsl:attribute>
		  <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:with-param name="base" select="$base" />
		    <xsl:with-param name="lang">
		      <xsl:choose>
			<xsl:when test="@xml:lang">
			  <xsl:value-of select="@xml:lang" />
			</xsl:when>
			<xsl:otherwise>
			  <xsl:value-of select="$lang2" />
			</xsl:otherwise>
		      </xsl:choose>
		    </xsl:with-param>
		  </xsl:call-template>
		</xsl:for-each>

	      </atomrdf:Person>

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

	  <xsl:for-each select="atom:host" >
	    <atomrdf:host>
	      <xsl:value-of select="." />
	    </atomrdf:host>
	  </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">
		<xsl:with-param name="base" select="$xbase" />
		<xsl:with-param name="lang">
		  <xsl:choose>
		    <xsl:when test="@xml:lang">
		      <xsl:value-of select="@xml:lang" />
		    </xsl:when>
		    <xsl:otherwise>
		      <xsl:value-of select="$lang" />
		    </xsl:otherwise>
		  </xsl:choose>
		</xsl:with-param>
	      </xsl:call-template>
	    </atomrdf:summary>
	  </xsl:for-each>


	  <xsl:for-each select="atom:content" >
	    <xsl:call-template name="Content">
	      <xsl:with-param name="base" select="$xbase" />
	      <xsl:with-param name="lang">
		<xsl:choose>
		  <xsl:when test="@xml:lang">
		    <xsl:value-of select="@xml:lang" />
		  </xsl:when>
		  <xsl:otherwise>
		    <xsl:value-of select="$lang" />
		  </xsl:otherwise>
		</xsl:choose>
	      </xsl:with-param>
	    </xsl:call-template>
	  </xsl:for-each>

	  <xsl:for-each select="atom:copyright" >
	    <atomrdf:copyright>
	      <xsl:call-template name="Text">
		<xsl:with-param name="base" select="$xbase" />
		<xsl:with-param name="lang">
		  <xsl:choose>
		    <xsl:when test="@xml:lang">
		      <xsl:value-of select="@xml:lang" />
		    </xsl:when>
		    <xsl:otherwise>
		      <xsl:value-of select="$lang" />
		    </xsl:otherwise>
		  </xsl:choose>
		</xsl:with-param>
	      </xsl:call-template>
	    </atomrdf:copyright>
	  </xsl:for-each>

	  <!-- handle Entry extensions -->
	  <xsl:for-each select="*[namespace-uri() != $NSATOM]">
	    <xsl:call-template name="Extension">
	      <xsl:with-param name="base" select="$base" />
	      <xsl:with-param name="lang">
		<xsl:choose>
		  <xsl:when test="@xml:lang">
		    <xsl:value-of select="@xml:lang" />
		  </xsl:when>
		  <xsl:otherwise>
		    <xsl:value-of select="$lang" />
		  </xsl:otherwise>
		</xsl:choose>
	      </xsl:with-param>
	    </xsl:call-template>
	  </xsl:for-each>

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

    </atomrdf:Entry>

  </xsl:template>


  <!-- handle feed/entry/head -->
  <xsl:template name="EntryHead" >
    <xsl:param name="base" />
    <xsl:param name="lang" />

    <!-- resolve xml:base -->
    <xsl:variable name="xbase">
      <xsl:choose>
	<xsl:when test="$base">
	  <xsl:choose>
	    <xsl:when test="@xml:base">
	      <xsl:call-template name="resolveRef">
		<xsl:with-param name="base" select="$base" />
		<xsl:with-param name="ref" select="@xml:base" />
		<xsl:with-param name="strict" select="true" />
	      </xsl:call-template>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$base" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of select="@xml:base" />
	</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <!-- declare default author's for this head -->
    <xsl:for-each select="atom:author" >
      <xsl:call-template name="EntryHeadAuthor" >
	<xsl:with-param name="base" select="$xbase" />
	<xsl:with-param name="lang">
	  <xsl:choose>
	    <xsl:when test="@xml:lang">
	      <xsl:value-of select="@xml:lang" />
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$lang" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:with-param>
      </xsl:call-template>
    </xsl:for-each>

    <atomrdf:Feed>

      <xsl:choose>
	<!-- use feed id -->
	<xsl:when test="atom:id">
	  <xsl:attribute name="rdf:about">
	    <xsl:value-of select="atom:id" />
	  </xsl:attribute>
	</xsl:when>

	<!-- create feed 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/head in the same way as feed/head -->
      <xsl:call-template name="Head">
	<xsl:with-param name="base" select="$xbase" />
	<xsl:with-param name="lang">
	  <xsl:choose>
	    <xsl:when test="@xml:lang">
	      <xsl:value-of select="@xml:lang" />
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$lang" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:with-param>
      </xsl:call-template>
    </atomrdf:Feed>
  </xsl:template>

  <!-- handle feed/head and feed/entry/head -->
  <xsl:template name="Head">
    <xsl:param name="base" />
    <xsl:param name="lang" />

    <!-- resolve xml:base -->
    <xsl:variable name="xbase">
      <xsl:choose>
	<xsl:when test="$base">
	  <xsl:choose>
	    <xsl:when test="@xml:base">
	      <xsl:call-template name="resolveRef">
		<xsl:with-param name="base" select="$base" />
		<xsl:with-param name="ref" select="@xml:base" />
		<xsl:with-param name="strict" select="true" />
	      </xsl:call-template>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$base" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of select="@xml:base" />
	</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <atomrdf:title>

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

	<xsl:call-template name="Text">
	  <xsl:with-param name="base" select="$xbase" />
	  <xsl:with-param name="lang">
	    <xsl:choose>
	      <xsl:when test="@xml:lang">
		<xsl:value-of select="@xml:lang" />
	      </xsl:when>
	      <xsl:otherwise>
		<xsl:value-of select="$lang" />
	      </xsl:otherwise>
	    </xsl:choose>
	  </xsl:with-param>
	</xsl:call-template>
      </xsl:for-each>

    </atomrdf:title>

    <xsl:for-each select="atom:link">
      <xsl:call-template name="Link">
	<xsl:with-param name="base" select="$xbase" />
	<xsl:with-param name="lang">
	  <xsl:choose>
	    <xsl:when test="@xml:lang">
	      <xsl:value-of select="@xml:lang" />
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$lang" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:with-param>
      </xsl:call-template>
    </xsl:for-each>

    <xsl:for-each select="atom:category" >
      <xsl:call-template name="Category">
	<!-- note categories don't use xml:base -->
	<xsl:with-param name="lang">
	  <xsl:choose>
	    <xsl:when test="@xml:lang">
	      <xsl:value-of select="@xml:lang" />
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$lang" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:with-param>
      </xsl:call-template>
    </xsl:for-each>

    <xsl:for-each select="atom:introspection" >
      <atomrdf:introspection>
	<xsl:attribute name="rdf:resource">
	  <xsl:call-template name="resolveRef">
	    <xsl:with-param name="base">
	      <xsl:call-template name="resolveRef">
		<xsl:with-param name="base" select="$xbase" />
		<xsl:with-param name="strict" select="true" />
		<xsl:with-param name="ref" select="@xml:base" />
	      </xsl:call-template>
	    </xsl:with-param>
	    <xsl:with-param name="strict" select="true" />
	    <xsl:with-param name="ref" select="@href" />
	  </xsl:call-template>
	</xsl:attribute>
      </atomrdf:introspection>
    </xsl:for-each>

    <xsl:for-each select="atom:post" >
      <atomrdf:post>
	<xsl:attribute name="rdf:resource">
	  <xsl:call-template name="resolveRef">
	    <xsl:with-param name="base">
	      <xsl:call-template name="resolveRef">
		<xsl:with-param name="base" select="$xbase" />
		<xsl:with-param name="strict" select="true" />
		<xsl:with-param name="ref" select="@xml:base" />
	      </xsl:call-template>
	    </xsl:with-param>
	    <xsl:with-param name="strict" select="true" />
	    <xsl:with-param name="ref" select="@href" />
	  </xsl:call-template>
	</xsl:attribute>
      </atomrdf:post>
    </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" >
      <xsl:variable name="lang2">
	<xsl:choose>
	  <xsl:when test="@xml:lang">
	    <xsl:value-of select="@xml:lang" />
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:value-of select="$lang" />
	  </xsl:otherwise>
	</xsl:choose>
      </xsl:variable>

      <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="resolveRef">
		    <xsl:with-param name="base" select="$xbase" />
		    <xsl:with-param name="strict" select="true" />
		    <xsl:with-param name="ref" select="@xml:base" />
		  </xsl:call-template>
		</xsl:with-param>
		<xsl:with-param name="strict" select="true" />
		<xsl:with-param name="ref" select="." />
	      </xsl:call-template>
	    </atomrdf:uri>
	  </xsl:for-each>

	  <atomrdf:name>
	    <xsl:attribute name="xml:lang">
	      <xsl:choose>
		<xsl:when test="@xml:lang">
		  <xsl:value-of select="@xml:lang" />
		</xsl:when>
		<xsl:otherwise>
		  <xsl:value-of select="$lang2" />
		</xsl:otherwise>
	      </xsl:choose>
	    </xsl:attribute>
	    <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:with-param name="base" select="$base" />
	      <xsl:with-param name="lang">
		<xsl:choose>
		  <xsl:when test="@xml:lang">
		    <xsl:value-of select="@xml:lang" />
		  </xsl:when>
		  <xsl:otherwise>
		    <xsl:value-of select="$lang2" />
		  </xsl:otherwise>
		</xsl:choose>
	      </xsl:with-param>
	    </xsl:call-template>
	  </xsl:for-each>

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

    <xsl:for-each select="atom:tagline" >
      <atomrdf:tagline>

	<xsl:call-template name="Text">
	  <xsl:with-param name="base" select="$xbase" />
	  <xsl:with-param name="lang">
	    <xsl:choose>
	      <xsl:when test="@xml:lang">
		<xsl:value-of select="@xml:lang" />
	      </xsl:when>
	      <xsl:otherwise>
		<xsl:value-of select="$lang" />
	      </xsl:otherwise>
	    </xsl:choose>
	  </xsl:with-param>
	</xsl:call-template>
      </atomrdf:tagline>
    </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="resolveRef">
		      <xsl:with-param name="base" select="$xbase" />
		      <xsl:with-param name="strict" select="true" />
		      <xsl:with-param name="ref" select="@xml:base" />
		    </xsl:call-template>
		  </xsl:with-param>
		  <xsl:with-param name="strict" select="true" />
		  <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">
	  <xsl:with-param name="base" select="$xbase" />
	  <xsl:with-param name="lang">
	    <xsl:choose>
	      <xsl:when test="@xml:lang">
		<xsl:value-of select="@xml:lang" />
	      </xsl:when>
	      <xsl:otherwise>
		<xsl:value-of select="$lang" />
	      </xsl:otherwise>
	    </xsl:choose>
	  </xsl:with-param>
	</xsl:call-template>

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

    <xsl:for-each select="atom:info" >
      <atomrdf:info>

	<xsl:call-template name="Text">
	  <xsl:with-param name="base" select="$xbase" />
	  <xsl:with-param name="lang">
	    <xsl:choose>
	      <xsl:when test="@xml:lang">
		<xsl:value-of select="@xml:lang" />
	      </xsl:when>
	      <xsl:otherwise>
		<xsl:value-of select="$lang" />
	      </xsl:otherwise>
	    </xsl:choose>
	  </xsl:with-param>
	</xsl:call-template>

      </atomrdf:info>
    </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:with-param name="base" select="$base" />
	<xsl:with-param name="lang">
	  <xsl:choose>
	    <xsl:when test="@xml:lang">
	      <xsl:value-of select="@xml:lang" />
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$lang" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:with-param>
      </xsl:call-template>
    </xsl:for-each>

  </xsl:template>

  <!-- handle feed/entry/head/author -->
  <xsl:template name="EntryHeadAuthor">
    <xsl:param name="base" />
    <xsl:param name="lang" />

    <!-- resolve xml:base -->
    <xsl:variable name="xbase">
      <xsl:choose>
	<xsl:when test="$base">
	  <xsl:choose>
	    <xsl:when test="@xml:base">
	      <xsl:call-template name="resolveRef">
		<xsl:with-param name="base" select="$base" />
		<xsl:with-param name="ref" select="@xml:base" />
		<xsl:with-param name="strict" select="true" />
	      </xsl:call-template>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$base" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of select="@xml:base" />
	</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <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="resolveRef">
		<xsl:with-param name="base" select="$xbase" />
		<xsl:with-param name="strict" select="true" />
		<xsl:with-param name="ref" select="@xml:base" />
	      </xsl:call-template>
	    </xsl:with-param>
	    <xsl:with-param name="strict" select="true" />
	    <xsl:with-param name="ref" select="." />
	  </xsl:call-template>
	</atomrdf:uri>
      </xsl:for-each>

      <atomrdf:name>
	<xsl:attribute name="xml:lang">
	  <xsl:choose>
	    <xsl:when test="@xml:lang">
	      <xsl:value-of select="@xml:lang" />
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$lang" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:attribute>
	<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:with-param name="base" select="$base" />
	  <xsl:with-param name="lang">
	    <xsl:choose>
	      <xsl:when test="@xml:lang">
		<xsl:value-of select="@xml:lang" />
	      </xsl:when>
	      <xsl:otherwise>
		<xsl:value-of select="$lang" />
	      </xsl:otherwise>
	    </xsl:choose>
	  </xsl:with-param>
	</xsl:call-template>
      </xsl:for-each>
      
    </atomrdf:Person>
  </xsl:template>

  <!-- handle Content Constructs -->
  <xsl:template name="Content">
    <xsl:param name="base" />
    <xsl:param name="lang" />

    <!-- resolve xml:base -->
    <xsl:variable name="xbase">
      <xsl:choose>
	<xsl:when test="$base">
	  <xsl:choose>
	    <xsl:when test="@xml:base">
	      <xsl:call-template name="resolveRef">
		<xsl:with-param name="base" select="$base" />
		<xsl:with-param name="ref" select="@xml:base" />
		<xsl:with-param name="strict" select="true" />
	      </xsl:call-template>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$base" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of select="@xml:base" />
	</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <atomrdf:content>

      <atomrdf:Text>
	<!-- preserve xml:base context -->
	<atomrdf:contentBase>
	  <xsl:value-of select="$xbase" />
	</atomrdf:contentBase>
	<atomrdf:contentLang>
	  <xsl:value-of select="$lang" />
	</atomrdf:contentLang>

	<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">
		    <xsl:call-template name="resolveRef">
		      <xsl:with-param name="base" select="$xbase" />
		      <xsl:with-param name="strict" select="true" />
		      <xsl:with-param name="ref" select="@xml:base" />
		    </xsl:call-template>
		  </xsl:with-param>
		  <xsl:with-param name="strict" select="true" />
		  <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'">
	    <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'">
	    <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'">
	    <rdf:value rdf:parseType="Literal">
	      <!-- don't include containing element -->
	      <xsl:copy-of select="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')">
	    <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/')">
	    <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>
	    <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:param name="base" />
    <xsl:param name="lang" />

    <!-- resolve xml:base -->
    <xsl:variable name="xbase">
      <xsl:choose>
	<xsl:when test="$base">
	  <xsl:choose>
	    <xsl:when test="@xml:base">
	      <xsl:call-template name="resolveRef">
		<xsl:with-param name="base" select="$base" />
		<xsl:with-param name="ref" select="@xml:base" />
		<xsl:with-param name="strict" select="true" />
	      </xsl:call-template>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$base" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of select="@xml:base" />
	</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <atomrdf:Text>
      <!-- preserve xml:base context -->
      <atomrdf:contentBase>
	<xsl:value-of select="$xbase" />
      </atomrdf:contentBase>
      <atomrdf:contentLang>
	<xsl:value-of select="$lang" />
      </atomrdf:contentLang>

      <xsl:choose>
	<!-- handle TEXT content -->
	<xsl:when test="not(@type) or @type='TEXT'">
	  <rdf:value>
	    <xsl:value-of select="." />
	  </rdf:value>
	  <atomrdf:textType>TEXT</atomrdf:textType>
	</xsl:when>

	<!-- handle HTML content -->
	<xsl:when test="@type='HTML'">
	  <rdf:value>
	    <xsl:value-of select="." />
	  </rdf:value>
	  <atomrdf:textType>HTML</atomrdf:textType>
	</xsl:when>

	<!-- handle XHTML content -->
	<xsl:when test="@type='XHTML'">
	  <rdf:value rdf:parseType="Literal">
	    <!-- don't include containing element -->
	    <xsl:copy-of select="node()" />
	  </rdf:value>
	  <atomrdf:textType>XHTML</atomrdf:textType>
	</xsl:when>
      </xsl:choose>	
    </atomrdf:Text>
  </xsl:template>

  <!-- Extension Construct -->
  <xsl:template name="Extension">
    <xsl:param name="base" />
    <xsl:param name="lang" />

    <!-- don't resolve xml:base as it is attached as contentbase -->
    <!--
	<xsl:variable name="xbase">
	<xsl:choose>
	<xsl:when test="$base">
	<xsl:choose>
	<xsl:when test="@xml:base">
	<xsl:call-template name="resolveRef">
	<xsl:with-param name="base" select="$base" />
	<xsl:with-param name="ref" select="@xml:base" />
	<xsl:with-param name="strict" select="true" />
	</xsl:call-template>
	</xsl:when>
	<xsl:otherwise>
	<xsl:value-of select="$base" />
	</xsl:otherwise>
	</xsl:choose>
	</xsl:when>
	<xsl:otherwise>
	<xsl:value-of select="@xml:base" />
	</xsl:otherwise>
	</xsl:choose>
	</xsl:variable>
    -->

    <xsl:choose>
      <!-- Structured Extension Construct -->
      <xsl:when test="@* or ./*">
	<atomrdf:extension>
	  <atomrdf:Extension>
	    <atomrdf:contentBase>
	      <!-- use passed in xml:base -->
	      <xsl:value-of select="$base" />
	    </atomrdf:contentBase>
	    <atomrdf:contentLang>
	      <xsl:value-of select="$lang" />
	    </atomrdf:contentLang>

	    <rdf:value rdf:parseType="Literal" >
	      <!-- include containing element -->
	      <xsl:copy-of select="." />
	    </rdf:value>
	  </atomrdf:Extension>
	</atomrdf:extension>
      </xsl:when>
      <xsl:otherwise>
	<!-- Simple Extension Construct -->
	<xsl:copy>
	  <!-- no contentLang -->
	  <xsl:value-of select="." />
	</xsl:copy>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- handle Link constructs -->
  <xsl:template name="Link">
    <xsl:param name="base" />
    <xsl:param name="lang" />

    <!-- resolve xml:base -->
    <xsl:variable name="xbase">
      <xsl:choose>
	<xsl:when test="$base">
	  <xsl:choose>
	    <xsl:when test="@xml:base">
	      <xsl:call-template name="resolveRef">
		<xsl:with-param name="base" select="$base" />
		<xsl:with-param name="ref" select="@xml:base" />
		<xsl:with-param name="strict" select="true" />
	      </xsl:call-template>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$base" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of select="@xml:base" />
	</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <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="resolveRef">
		  <xsl:with-param name="base" select="$xbase" />
		  <xsl:with-param name="strict" select="true" />
		  <xsl:with-param name="ref" select="@xml:base" />
		</xsl:call-template>
	      </xsl:with-param>
	      <xsl:with-param name="strict" select="true" />
	      <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:attribute name="xml:lang">
	      <xsl:choose>
		<xsl:when test="@xml:lang">
		  <xsl:value-of select="@xml:lang" />
		</xsl:when>
		<xsl:otherwise>
		  <xsl:value-of select="$lang" />
		</xsl:otherwise>
	      </xsl:choose>
	    </xsl:attribute>
	    <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">
    <xsl:param name="lang" />

    <!-- categories don't need xml:base -->
    <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:attribute name="xml:lang">
	      <xsl:choose>
		<xsl:when test="@xml:lang">
		  <xsl:value-of select="@xml:lang" />
		</xsl:when>
		<xsl:otherwise>
		  <xsl:value-of select="$lang" />
		</xsl:otherwise>
	      </xsl:choose>
	    </xsl:attribute>
	    <xsl:value-of select="$categoryLabel" />
	  </atomrdf:categoryLabel>
	</xsl:if>

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

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

</xsl:stylesheet>
