<xsl:transform xmlns="http://purl.org/atom/ns#" 
  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:* and not(../..)">
        <!-- The atom documentElement, atom:* -->
        <rdf:RDF>
          <xsl:apply-templates select="@*"/>
          <xsl:apply-templates/>
        </rdf:RDF>
      </xsl:when>

      <xsl:when test="../../atom:* and (@id|./*) and not(../../..)">
        <!-- Node element children of atom:* -->
        <rdf:Description>
          <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/>
        </rdf:Description>
      </xsl:when>

      <xsl:when test="ancestor::*[@mode]">
        <!-- Literal XML content (just copy it) -->
        <xsl:copy>
          <xsl:apply-templates select="@*"/>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:when>

      <xsl:when test="../../../* and @id">
        <!-- Property element with an untyped node child -->
        <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|@mode|@type) and (@href or ./*)">
        <!-- Simple property element -->
        <xsl:copy>
          <xsl:if test="./* and not(@href)">
             <xsl:attribute name="rdf:parseType">Resource</xsl:attribute>
          </xsl:if>
          <xsl:if test="@href and not(./*)">
            <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(@id|@href) 
                      and not(@*[namespace-uri()!=$xml-ns and 
                          not(namespace-uri()='' and local-name()='mode')])">
        <!-- Property element no non-special attributes -->
        <xsl:copy>
          <xsl:if test="./* or (@mode='xml')">
            <xsl:attribute name="rdf:parseType">Literal</xsl:attribute>
          </xsl:if>
          <xsl:if test="@mode and 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 not(@id|@href)
                      and @*[namespace-uri()!=$xml-ns and 
                         not(namespace-uri()='' and local-name()='mode')]">
        <!-- Property element with non-special attributes -->
        <xsl:copy>
          <xsl:attribute name="rdf:parseType">Resource</xsl:attribute>
          <xsl:apply-templates select="@*[namespace-uri()=$xml-ns 
                                       and local-name()!='lang']"/>
          <xsl:for-each select="@*">
            <xsl:if test="local-name()!='mode' and namespace-uri()!=$xml-ns">
              <!-- This auto-converts type, quite neatly -->
              <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>