<?xml version="1.0"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
  xmlns:atom="http://www.w3.org/2005/Atom"
  xmlns="http://www.w3.org/2005/Atom"
  xmlns:vi="http://www.openlinksw.com/weblog/"
  xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/"
  xmlns:itunes="http://www.itunes.com/DTDs/Podcast-1.0.dtd"
  version="1.0">


<xsl:output indent="yes" />


<!-- general element conversions -->

<xsl:template match="rss/channel">
  <xsl:comment>ATOM based XML document generated By OpenLink Virtuoso</xsl:comment>
  <feed>
      <id><xsl:value-of select="link"/></id>
      <xsl:apply-templates/>
  </feed>
</xsl:template>

<xsl:template match="title">
    <title><xsl:apply-templates /></title>
</xsl:template>

<xsl:template match="link">
    <link href="{.}" type="text/html" rel="alternate"/>
    <xsl:if test="parent::channel">
	<link href="{replace(.,'output=html','output=atom')}" type="application/atom+xml" rel="self"/>
    </xsl:if>
</xsl:template>

<xsl:template match="channel/itunes:*" />
<xsl:template match="item/itunes:*" />

<xsl:template match="channel/description[.!='']">
    <subtitle><xsl:apply-templates /></subtitle>
</xsl:template>

<xsl:template match="channel/copyright">
    <xsl:if test=". != ''">
	<rights><xsl:apply-templates /></rights>
    </xsl:if>
</xsl:template>

<xsl:template match="channel/managingEditor[.!='']">
	<xsl:call-template name="author"/>
</xsl:template>

<xsl:template match="channel[not lastBuildDate]/pubDate">
    <updated><xsl:call-template name="date"/></updated>
</xsl:template>

<xsl:template match="channel/lastBuildDate">
    <updated><xsl:call-template name="date"/></updated>
</xsl:template>

<xsl:template match="channel/category[not @text]">
    <category term="{.}" />
</xsl:template>

<xsl:template match="channel/generator">
    <generator>
	<xsl:apply-templates />
    </generator>
</xsl:template>

<xsl:template match="channel/image">
    <logo><xsl:apply-templates select="url"/></logo>
</xsl:template>

<xsl:template match="item/author[.!='' and namespace-uri () = '']">
    <xsl:call-template name="author"/>
</xsl:template>

<xsl:template match="item/description">
    <content type="html">
	<xsl:apply-templates />
    </content>
</xsl:template>

<xsl:template match="item/guid">
    <id><xsl:apply-templates /></id>
</xsl:template>

<xsl:template match="item/pubDate">
    <published><xsl:call-template name="date"/></published>
</xsl:template>

<xsl:template match="item/vi:modified">
    <updated><xsl:apply-templates /></updated>
</xsl:template>

<xsl:template match="item">
    <entry>
	<xsl:if test="not (guid) and link">
	    <id><xsl:value-of select="link"/></id>
	</xsl:if>
	<xsl:if test="not (description)">
	    <content type="html">
		<xsl:value-of select="title"/>
	    </content>
	</xsl:if>
	<xsl:apply-templates />
    </entry>
</xsl:template>

<xsl:template match="channel/language" />
<xsl:template match="channel/webMaster" />
<xsl:template match="channel/cloud" />
<xsl:template match="wfw:*" />
<xsl:template match="dc:*" />
<xsl:template match="openSearch:*" />
<xsl:template match="slash:*" />
<xsl:template match="item/comments" />
<xsl:template match="item/enclosure" />
<xsl:template match="itunes:*" />

<xsl:template match="@*" />

<xsl:template match="text()">
  <xsl:value-of select="normalize-space(.)" />
</xsl:template>

<xsl:template name="author">
    <xsl:variable name="author">
	<author>
	    <xsl:choose>
		<xsl:when test="contains (., '&lt;')">
		    <name><xsl:value-of select="normalize-space (substring-before (.,'&lt;'))"/></name>
		    <email><xsl:value-of select="translate (substring-after (.,'&lt;'), '&gt;', '')"/></email>
		</xsl:when>
		<xsl:when test="contains (., '(')">
		    <name><xsl:value-of select="translate (substring-after (.,'('), ')', '')"/></name>
		    <email><xsl:value-of select="normalize-space (substring-before (.,'('))"/></email>
		</xsl:when>
		<xsl:otherwise>
		    <name><xsl:value-of select="."/></name>
		    <email><xsl:value-of select="."/></email>
		</xsl:otherwise>
	    </xsl:choose>
	</author>
    </xsl:variable>
    <xsl:choose>
	<xsl:when test="$author/author/name[.!=''] and $author/author/email[.!='']">
	    <xsl:copy-of select="$author/author"/>
	</xsl:when>
	<xsl:when test="$author/author/email[.!='']">
	    <author>
		<name>~unknown~</name>
		<xsl:copy-of select="$author/author/email"/>
	    </author>
	</xsl:when>
	<xsl:when test="$author/author/name[.!='']">
	    <author>
		<xsl:copy-of select="$author/author/name"/>
	    </author>
	</xsl:when>
    </xsl:choose>
</xsl:template>

<xsl:template name="date">
  <xsl:variable name="m" select="substring(., 9, 3)" />
  <xsl:value-of select="substring(., 13, 4)"
  />-<xsl:choose>
    <xsl:when test="$m='Jan'">01</xsl:when>
    <xsl:when test="$m='Feb'">02</xsl:when>
    <xsl:when test="$m='Mar'">03</xsl:when>
    <xsl:when test="$m='Apr'">04</xsl:when>
    <xsl:when test="$m='May'">05</xsl:when>
    <xsl:when test="$m='Jun'">06</xsl:when>
    <xsl:when test="$m='Jul'">07</xsl:when>
    <xsl:when test="$m='Aug'">08</xsl:when>
    <xsl:when test="$m='Sep'">09</xsl:when>
    <xsl:when test="$m='Oct'">10</xsl:when>
    <xsl:when test="$m='Nov'">11</xsl:when>
    <xsl:when test="$m='Dec'">12</xsl:when>
    <xsl:otherwise>00</xsl:otherwise>
  </xsl:choose>-<xsl:value-of select="substring(., 6, 2)"
  />T<xsl:value-of select="substring(., 18, 8)" /><xsl:text>Z</xsl:text>
</xsl:template>

<xsl:template name="removeTags">
  <xsl:param name="html" select="." />
  <xsl:choose>
    <xsl:when test="contains($html,'&lt;')">
      <xsl:call-template name="removeEntities">
        <xsl:with-param name="html" select="substring-before($html,'&lt;')" />
      </xsl:call-template>
      <xsl:call-template name="removeTags">
        <xsl:with-param name="html" select="substring-after($html, '&gt;')" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="removeEntities">
        <xsl:with-param name="html" select="$html" />
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="removeEntities">
  <xsl:param name="html" select="." />
  <xsl:choose>
    <xsl:when test="contains($html,'&amp;')">
      <xsl:value-of select="substring-before($html,'&amp;')" />
      <xsl:variable name="c" select="substring-before(substring-after($html,'&amp;'),';')" />
      <xsl:choose>
        <xsl:when test="$c='nbsp'">&#160;</xsl:when>
        <xsl:when test="$c='lt'">&lt;</xsl:when>
        <xsl:when test="$c='gt'">&gt;</xsl:when>
        <xsl:when test="$c='amp'">&amp;</xsl:when>
        <xsl:when test="$c='quot'">&quot;</xsl:when>
        <xsl:when test="$c='apos'">&apos;</xsl:when>
        <xsl:otherwise>?</xsl:otherwise>
      </xsl:choose>
      <xsl:call-template name="removeTags">
        <xsl:with-param name="html" select="substring-after($html, ';')" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$html" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>
