<!-- 
RSS 1.0 to RSS 1.1 XSLT Stylesheet
Author: Sean B. Palmer, inamidst.com
-->

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
   xmlns="http://purl.org/net/rss1.1#" 
   xmlns:rss="http://purl.org/rss/1.0/" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
   exclude-result-prefixes="rss" version="1.0">
<xsl:output indent="yes" omit-xml-declaration="yes" method="xml"/>
<xsl:namespace-alias result-prefix="" stylesheet-prefix=""/>
<xsl:variable name="rssns" select="'http://purl.org/rss/1.0/'"/>

<xsl:template match="/rdf:RDF/rss:channel">
   <Channel rdf:about="{@rdf:about}">
      <xsl:apply-templates/>
      <items rdf:parseType="Collection">
         <xsl:call-template name="items"/>
      </items>
   </Channel>
<!-- Force trailing newline -->
<xsl:text>
</xsl:text>
</xsl:template>

<xsl:template match="rss:title|rss:link|rss:url|rss:description">
   <xsl:element name="{local-name(.)}">
      <xsl:copy-of select="@xml:lang"/>
      <xsl:value-of select="."/>
   </xsl:element>
</xsl:template>

<xsl:template match="rss:channel/rss:image">
   <xsl:variable name="imageURI" select="@rdf:resource|@resource"/>
   <xsl:variable name="image" select="//rss:image[@rdf:about=$imageURI]"/>
   <xsl:if test="$image">
      <image rdf:about="{$image/@rdf:about}" rdf:parseType="Resource">
         <xsl:copy-of select="$image/@xml:lang"/>
         <xsl:apply-templates select="$image/*"/>
      </image>
   </xsl:if>
</xsl:template>

<xsl:template match="rss:channel/*[namespace-uri()!=$rssns]">
   <xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="rss:image/*[namespace-uri()!=$rssns]">
   <xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="rss:item/*[namespace-uri()!=$rssns]">
   <xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="rss:image|rss:item"/>

<xsl:template name="items">
   <xsl:for-each select="//rdf:Seq/rdf:li">
      <xsl:variable name="itemURI" select="@rdf:resource|@resource"/>
      <xsl:variable name="item" select="//rss:item[@rdf:about=$itemURI]"/>
      <xsl:if test="$item">
         <item rdf:about="{$item/@rdf:about}">
            <xsl:copy-of select="$item/@xml:lang"/>
            <xsl:apply-templates select="$item/*"/>
         </item>
      </xsl:if>
   </xsl:for-each>
</xsl:template>

<!-- Good ol' don't-pass-text-through -->
<xsl:template match="text()|@*"/>

</xsl:transform>
