<xsl:transform 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:atom="http://purl.org/atom/ns#" 
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
  version="1.0" >

<xsl:variable name="xml-ns" select="'http://www.w3.org/XML/1998/namespace'"/>
<xsl:variable name="atom-ns" select="'http://purl.org/atom/ns#'"/>

<xsl:template match="@id|@href|@mode">
   <xsl:if test="../ancestor::*[@mode]">
     <xsl:copy>
       <xsl:apply-templates select="@*"/>
       <xsl:apply-templates/>
     </xsl:copy>
   </xsl:if>
</xsl:template>

<xsl:template match="node()|@*">
   <xsl:choose>
      <xsl:when test="../atom:atom">
        <!-- The atom documentElement, atom:atom -->
        <rdf:RDF>
          <xsl:apply-templates select="@*"/>
          <xsl:apply-templates/>
        </rdf:RDF>
      </xsl:when>

      <xsl:when test="../../atom:atom">
        <!-- Node element children of atom:atom -->
        <xsl:copy>
          <xsl:if test="@id">
            <xsl:attribute name="rdf:about">
              <xsl:value-of select="@id"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:apply-templates select="@*"/>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:when>

      <xsl:when test="ancestor::*[@mode]">
        <!-- Literal content (just copy it) -->
        <xsl:copy>
          <xsl:apply-templates select="@*"/>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:when>

      <xsl:when test="../../../* and @id">
        <!-- Node element -->
        <xsl:copy>
          <rdf:Description>
            <xsl:attribute name="rdf:about">
              <xsl:value-of select="@id"/>
            </xsl:attribute>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates/>
          </rdf:Description>
        </xsl:copy>
      </xsl:when>

      <xsl:when test="../../../* and not(@id) and not(@mode)">
        <!-- Property element -->
        <xsl:copy>
          <xsl:if test="./*">
             <xsl:attribute name="rdf:parseType">Resource</xsl:attribute>
          </xsl:if>
          <xsl:if test="not(./*) and @href">
            <xsl:attribute name="rdf:resource">
              <xsl:value-of select="@href"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:apply-templates select="@*"/>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:when>

      <xsl:when test="../../../* and not(./@*[namespace-uri()!=$xml-ns and 
           local-name()!='mode']) and not(@id|@href) and (@mode or not(./*))">
        <xsl:copy>
          <xsl:if test="./* or @mode='xml'">
            <xsl:attribute name="rdf:parseType">Literal</xsl:attribute>
          </xsl:if>
          <xsl:if test="not(@mode='xml')">
            <xsl:attribute name="rdf:datatype">
              <xsl:value-of select="concat($atom-ns,@mode)"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:apply-templates select="@*"/>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:when>

      <xsl:when test="../../../* and (@mode or not(./*)) 
                      and not(@id) and not(@href)">
        <xsl:copy>
          <xsl:attribute name="rdf:parseType">Resource</xsl:attribute>
          <xsl:apply-templates select="@*[namespace-uri()=
                          'http://www.w3.org/XML/1998/namespace']"/>
          <xsl:for-each select="@*">
            <xsl:if test="local-name()!='mode' and 
                namespace-uri()!='http://www.w3.org/XML/1998/namespace'">
              <xsl:element name="{name(.)}" namespace="{namespace-uri(..)}">
                <xsl:value-of select="."/>
              </xsl:element>
            </xsl:if>
          </xsl:for-each>
          <xsl:element name="rdf:value">
            <xsl:attribute name="rdf:parseType">Literal</xsl:attribute>
            <xsl:if test="not(@mode='xml')">
              <xsl:attribute name="rdf:datatype">
                <xsl:value-of select="concat($atom-ns,@mode)"/>
              </xsl:attribute>
            </xsl:if>
            <xsl:apply-templates/>
          </xsl:element>
        </xsl:copy>
      </xsl:when>
   </xsl:choose>
</xsl:template>

</xsl:transform>