<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

  <!-- PenguinBlog XSLT stylesheet for RSS feed
       Copyright 2003, 2004 by Silmaril Consultants
       Available for public use under the terms of the GNU GPL -->

  <xsl:output method="xml" version="1.0" encoding="iso-8859-1"
    indent="yes" omit-xml-declaration="no" media-type="text/xml"/>
  <!--
    doctype-public="-//Netscape//DTD Rich Site Summary 0.91//EN//XML"
    doctype-system="http://my.netscape.com/publish/formats/rss-0.91.dtd"/>
   -->
  <xsl:strip-space elements="*"/>

  <!-- Date at last call (file storedate) is in the format
       <date>
       <now weekday="Friday" wday="5" day="23" month="January" 
            year="2004" hour="14" minute="22" 
            second="55">2004-01-23T14:22:55+0000</now>
       ...
       </date>
       in file /var/www/html/cc/datetime.xml 

       External users please note this is not supplied with the 
       PenguinBlog. You have to set this up on your server with
       a script to run hourly from cron.
       -->

  <xsl:variable name="datefile" select="document('../cc/datetime.xml')"/>

  <xsl:variable name="isodate">
    <xsl:value-of select="substring($datefile/date/now,1,10)"/>
  </xsl:variable>

  <xsl:variable name="entrydates" select="//entries"/>

  <xsl:template match="/">
    <!--
    <xsl:processing-instruction name="xml-stylesheet">
      <xsl:text>href="http://silmaril.ie/rss.css" type="text/css" ?</xsl:text>
    </xsl:processing-instruction>
    -->
    <rss version="0.91">
      <channel xml:base="http://www.silmaril.ie/cgi-bin/blog">
        <title>
          <xsl:value-of select="document('../id.xml')/identity/name"/>
          <xsl:text>'s WebLog</xsl:text>
        </title>
        <link>http://www.silmaril.ie/cgi-bin/blog</link>
        <copyright>Silmaril Consultants</copyright>
        <image>
          <title>
            <xsl:value-of select="document('../id.xml')/identity/name"/>
            <xsl:text>'s WebLog</xsl:text>
          </title>
          <width>144</width>
          <height>100</height>
          <url>http://www.silmaril.ie/images/winter_theme_cutdown_icon.jpg</url>
          <link>http://www.silmaril.ie/cgi-bin/blog</link>
        </image>
        <description>Topics in structured information processing</description>
        <lastBuildDate>Tue, 26 Jul 2005 16:51:43 GMT</lastBuildDate>
        <language>EN</language>
        <xsl:for-each select="blog/entries/entry[position()&lt;=10]">
          <xsl:sort select="@YYYY-MM-DD" order="descending"/>
          <xsl:apply-templates select="." mode="selected"/>
        </xsl:for-each>
      </channel>
    </rss>
  </xsl:template>

  <xsl:template match="entry" mode="selected">
    <item>
      <title>
        <xsl:apply-templates select="topic/para" mode="selected"/>
      </title>
      <link>
        <xsl:text>http://www.silmaril.ie/cgi-bin/blog#</xsl:text>
        <xsl:value-of select="@id"/>
      </link>
      <description>
        <xsl:apply-templates select="summary" mode="selected"/>
        <xsl:text> (</xsl:text>
        <xsl:value-of select="substring-before(para[1],'.')"/>
        <xsl:text>...)</xsl:text>
      </description>
      <pubDate>
        <xsl:value-of select="substring(@day,1,3)"/>
        <xsl:text>, </xsl:text>
        <xsl:value-of select="substring(@YYYY-MM-DD,9,2)"/>
        <xsl:text> </xsl:text>
        <xsl:variable name="mon">
          <xsl:value-of select="number(substring(@YYYY-MM-DD,6,2))"/>
        </xsl:variable>
        <xsl:choose>
          <xsl:when test="$mon=1">
            <xsl:text>Jan</xsl:text>
          </xsl:when>
          <xsl:when test="$mon=2">
            <xsl:text>Feb</xsl:text>
          </xsl:when>
          <xsl:when test="$mon=3">
            <xsl:text>Mar</xsl:text>
          </xsl:when>
          <xsl:when test="$mon=4">
            <xsl:text>Apr</xsl:text>
          </xsl:when>
          <xsl:when test="$mon=5">
            <xsl:text>May</xsl:text>
          </xsl:when>
          <xsl:when test="$mon=6">
            <xsl:text>Jun</xsl:text>
          </xsl:when>
          <xsl:when test="$mon=7">
            <xsl:text>Jul</xsl:text>
          </xsl:when>
          <xsl:when test="$mon=8">
            <xsl:text>Aug</xsl:text>
          </xsl:when>
          <xsl:when test="$mon=9">
            <xsl:text>Sep</xsl:text>
          </xsl:when>
          <xsl:when test="$mon=10">
            <xsl:text>Oct</xsl:text>
          </xsl:when>
          <xsl:when test="$mon=11">
            <xsl:text>Nov</xsl:text>
          </xsl:when>
          <xsl:when test="$mon=12">
            <xsl:text>Dec</xsl:text>
          </xsl:when>
        </xsl:choose>
        <xsl:text> </xsl:text>
        <xsl:value-of select="substring(@YYYY-MM-DD,1,4)"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="@HH-MM-SS"/>
        <xsl:text> GMT</xsl:text>
      </pubDate>
    </item>
  </xsl:template>

  <xsl:template match="para | summary" mode="selected">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="para/list">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="para/list/item">
    <xsl:choose>
      <xsl:when test="position() != last() and
                      position() &gt; 1">
        <xsl:text>; </xsl:text>
      </xsl:when>
      <xsl:when test="position()=last()">
        <xsl:text>; or </xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text></xsl:text>
      </xsl:otherwise>
    </xsl:choose>
      <xsl:number format="a"/>
    <xsl:text>)&#x00A0;</xsl:text>
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="para/sequence/item">
    <xsl:choose>
      <xsl:when test="position() != last() and
                      position() &gt; 1">
        <xsl:text>; </xsl:text>
      </xsl:when>
      <xsl:when test="position()=last()">
        <xsl:text>; and </xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text></xsl:text>
      </xsl:otherwise>
    </xsl:choose>
      <xsl:number format="a"/>
    <xsl:text>)&#x00A0;</xsl:text>
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="text/list/item/text | text/sequence/item/text">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="q">
    <xsl:text>`</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>'</xsl:text>
  </xsl:template>

  <xsl:template match="link">
    <xsl:apply-templates/>
    <xsl:choose>
      <xsl:when test="@uri != ''">
        <xsl:text> [</xsl:text>
        <xsl:value-of select="@uri"/>
        <xsl:text>]</xsl:text>
      </xsl:when>
      <xsl:when test="@uri='' and @to != ''">
        <xsl:variable name="thisid" select="@to"/>
        <xsl:if test=".=''">
          <xsl:text>`</xsl:text>
        </xsl:if>
        <xsl:choose>
          <xsl:when test=".=''">
            <xsl:apply-templates select="//entry[@id=$thisid]/topic/para"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates/>
          </xsl:otherwise>
        </xsl:choose>
        <xsl:if test=".=''">
          <xsl:text>'</xsl:text>
        </xsl:if>
        <xsl:text> [http://www.silmaril.ie/cgi-bin/blog#</xsl:text>
        <xsl:value-of select="@to"/>
        <xsl:text>]</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text> [unknown link]</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="document">
    <xsl:text>`</xsl:text>
    <xsl:choose>
      <xsl:when test="@uri=''">
        <xsl:apply-templates/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="@title">
            <xsl:value-of select="@title"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="document(@uri)//title[1]"/>
          </xsl:otherwise>
        </xsl:choose>
        <xsl:text> [</xsl:text>
        <xsl:value-of select="@uri"/>
        <xsl:text>]</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:text>'</xsl:text>
  </xsl:template>

  <xsl:template match="para/code">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="emph">
    <xsl:text>_</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>_</xsl:text>
  </xsl:template>
  
  <xsl:template match="*"/>

</xsl:stylesheet>
