« XSL for formatting a date | Main | Pop Art Canvases »

June 01, 2009

XSL for transforming RSS 2.0 to RSS 1.0

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" id="rss2to1"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                xmlns:dc="http://purl.org/dc/elements/1.1/"
                xmlns="http://purl.org/rss/1.0/"
                >
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/rss"><xsl:apply-templates/></xsl:template>
<xsl:template match="channel">
<rdf:RDF>
    <channel rdf:about="{link}">
    <title><xsl:value-of select="title"/></title>  
    <description><xsl:value-of select="description"/></description>  
    <link><xsl:value-of select="link"/></link>    
    <items>
        <rdf:Seq>
            <xsl:for-each select="item"><rdf:li  rdf:resource="{link}"/></xsl:for-each>
        </rdf:Seq>
    </items>
   </channel>
    <xsl:for-each select="item">
    <item rdf:about="{link}">
        <title><xsl:value-of select="title"/></title>  
        <description><xsl:value-of select="description"/></description>  
        <link><xsl:value-of select="link"/></link>
        <dc:date><xsl:call-template name="FormatDate"><xsl:with-param name="DateTime"  select="pubDate"/></xsl:call-template></dc:date>
    </item>
    </xsl:for-each>
    </rdf:RDF>
</xsl:template>
  <xsl:template name="FormatDate">
    <xsl:param name="DateTime" />
    <!-- new date format 2006-01-14T08:55:22Z -->
    <xsl:variable name="mo">
      <xsl:value-of select="substring($DateTime,9,3)" />
    </xsl:variable>
    <xsl:variable name="day">
      <xsl:value-of select="substring($DateTime,6,2)" />
    </xsl:variable>
    <xsl:variable name="year">
      <xsl:value-of select="substring($DateTime,13,4)" />
    </xsl:variable>
    <xsl:variable name="time">
      <xsl:value-of select="substring($DateTime,18,8)" />
    </xsl:variable>
    <xsl:variable name="hh">
      <xsl:value-of select="substring($time,1,2)" />
    </xsl:variable>
    <xsl:variable name="mm">
      <xsl:value-of select="substring($time,4,2)" />
    </xsl:variable>
    <xsl:variable name="ss">
      <xsl:value-of select="substring($time,7,2)" />
    </xsl:variable>
    <xsl:value-of select="$year"/>
    <xsl:value-of select="'-'"/>
    <xsl:choose>
      <xsl:when test="$mo = 'Jan'">01</xsl:when>
      <xsl:when test="$mo = 'Feb'">02</xsl:when>
      <xsl:when test="$mo = 'Mar'">03</xsl:when>
      <xsl:when test="$mo = 'Apr'">04</xsl:when>
      <xsl:when test="$mo = 'May'">05</xsl:when>
      <xsl:when test="$mo = 'Jun'">06</xsl:when>
      <xsl:when test="$mo = 'Jul'">07</xsl:when>
      <xsl:when test="$mo = 'Aug'">08</xsl:when>
      <xsl:when test="$mo = 'Sep'">09</xsl:when>
      <xsl:when test="$mo = 'Oct'">10</xsl:when>
      <xsl:when test="$mo = 'Nov'">11</xsl:when>
      <xsl:when test="$mo = 'Dec'">12</xsl:when>
    </xsl:choose>

    <xsl:value-of select="'-'"/>
    <xsl:if test="(string-length($day) < 2)">
      <xsl:value-of select="0"/>
    </xsl:if>
    <xsl:value-of select="$day"/>
    <xsl:value-of select="'T'"/>
    <xsl:value-of select="$hh"/>
    <xsl:value-of select="':'"/>
    <xsl:value-of select="$mm"/>
    <xsl:value-of select="':'"/>
    <xsl:value-of select="$ss"/>
    <xsl:value-of select="'Z'"/>
  </xsl:template>

</xsl:stylesheet>

Tags: RSS , XSLT

Posted by pj at June 1, 2009 09:02 PM

Comments