<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="utf-8"/>

<xsl:param name="timestamp"/>

<xsl:template name="output_address">
   <xsl:text>http://</xsl:text>
   <xsl:text>www.barrycornelius.com/running/races/next.htm#</xsl:text>
   <xsl:value-of select="myid"/>
</xsl:template>

<xsl:template name="output_anchor_without_class">
   <xsl:call-template name="output_address"/>
</xsl:template>

<xsl:template name="output_anchor_with_class">
   <a class="url">
   <xsl:attribute name="href">
      <xsl:call-template name="output_address"/>
   </xsl:attribute>
   </a>
</xsl:template>

<xsl:template name="output_distance">
   <xsl:choose>
      <xsl:when test="distance_number='0.0000'">
         <xsl:text>unknown</xsl:text>
      </xsl:when>
      <xsl:when test="distance_number='21.0975'">
         <xsl:text>half marathon</xsl:text>
      </xsl:when>
      <xsl:when test="starts-with(distance_number,'42.195')">
         <xsl:text>marathon</xsl:text>
      </xsl:when>
      <xsl:otherwise>
         <xsl:value-of select="format-number(distance_number,'#.#####')"/>
         <xsl:value-of select="distance_units"/>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

<xsl:template name="output_datetime">
   <xsl:attribute name="title">
      <xsl:value-of select="translate(mydate,'-','')"/>
      <xsl:if test="string-length(starttime)=4">
         <xsl:text>T</xsl:text>
         <xsl:value-of select="starttime"/>
         <xsl:text>00</xsl:text>
      </xsl:if>
   </xsl:attribute>
   <xsl:value-of select="mydate"/>
   <xsl:if test="string-length(starttime)&gt;1">
      <xsl:text> at </xsl:text>
      <xsl:value-of select="starttime"/>
   </xsl:if>
</xsl:template>

<xsl:template match="/">
   <feed xmlns="http://www.w3.org/2005/Atom">
      <title>A race from Barry Cornelius's future races page</title>
      <content type="xhtml">
        <xsl:apply-templates/>
      </content>
   </feed>
</xsl:template>

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

<xsl:template match="race">
    <div xmlns="http://www.w3.org/1999/xhtml" class="vevent">
      <abbr class="dtstamp">
         <xsl:attribute name="title">
            <xsl:value-of select="$timestamp"/>
         </xsl:attribute>
      </abbr>
      <abbr class="uid">
         <xsl:attribute name="title">
            <xsl:value-of select="concat(myid, '@www.barrycornelius.com')"/>
         </xsl:attribute>
      </abbr>
      <abbr class="dtstart">
         <xsl:call-template name="output_datetime"/>
      </abbr>
      <abbr class="dtend">
         <xsl:call-template name="output_datetime"/>
      </abbr>
      <span class="location">
         <xsl:value-of select="location"/>
         <!--
         <xsl:variable name="pc" select="substring-after(smap, 'pc=')"/>
         <xsl:choose>
	    <xsl:when test="$pc=''">
               <xsl:value-of select="location"/>
	    </xsl:when>
	    <xsl:otherwise>
               <xsl:value-of select="$pc"/>
	       <xsl:text> (</xsl:text>
               <xsl:value-of select="location"/>
	       <xsl:text>)</xsl:text>
	    </xsl:otherwise>
         </xsl:choose>
	 -->
      </span>
      <xsl:call-template name="output_anchor_with_class"/>
      <span class="summary">
         <xsl:variable name="title" select="substring-before(comments, ';')"/>
	 <xsl:choose>
	    <xsl:when test="$title=''">
               <xsl:value-of select="translate(comments,'&quot;','')"/>
	    </xsl:when>
	    <xsl:otherwise>
               <xsl:value-of select="translate($title,'&quot;','')"/>
	    </xsl:otherwise>
	 </xsl:choose>
         <xsl:if test="string-length(starttime)&gt;1">
            <xsl:text> at </xsl:text>
            <xsl:value-of select="starttime"/>
         </xsl:if>
         <xsl:text>, cd=</xsl:text>
         <xsl:value-of select="translate(cd,'-_','? ')"/>
         <xsl:text>, eods=</xsl:text>
         <xsl:value-of select="translate(eods,'-','?')"/>
      </span>
      <div class="description">
         <xsl:text>details=</xsl:text>
         <xsl:call-template name="output_anchor_without_class"/>
         <xsl:text>, length=</xsl:text>
         <xsl:call-template name="output_distance"/>
         <xsl:text>, cd=</xsl:text>
         <xsl:value-of select="translate(cd,'-_','? ')"/>
         <xsl:text>, eods=</xsl:text>
         <xsl:value-of select="translate(eods,'-','?')"/>
      </div>
    </div>
</xsl:template>

</xsl:stylesheet>
