<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:date="http://www.w3.org/2001/08/date-util.xslt"
    xmlns:str="http://www.w3.org/2001/10/str-util.xsl"
    exclude-result-prefixes="date str"
    version="1.0">
  <xsl:import href="http://www.w3.org/2001/08/date-util.xslt"/>
  <xsl:import href="http://www.w3.org/2001/10/str-util.xsl"/>

  <xsl:variable name="head" select="/spec/header"/>
  <xsl:variable name="pubdate" select="$head/pubdate"/>
  <xsl:variable name="status" select="$head/status"/>
  <xsl:variable name="literal_date">
    <xsl:call-template name="getDate"/>
  </xsl:variable>

  <xsl:template name="getStatusSubtitle">
    <xsl:value-of select="normalize-space(concat(normalize-space($head/w3c-doctype),' ',normalize-space($literal_date)))"/>
  </xsl:template>

  <xsl:template name="getStatus">
    <xsl:variable name="tmp">
      <xsl:call-template name="getStatusSubtitle"/>
    </xsl:variable>
    <xsl:value-of select="normalize-space(substring-before(substring-after($tmp,'W3C'),$literal_date))"/>
  </xsl:template>

  <xsl:template name="getShortStatus">
    <xsl:variable name="tmp">
      <xsl:call-template name="getStatus"/>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="$tmp='Recommendation'">
        <xsl:value-of select="'REC'"/>
      </xsl:when>
      <xsl:when test="$tmp='Proposed Recommendation'">
        <xsl:value-of select="'PR'"/>
      </xsl:when>
      <xsl:when test="$tmp='Candidate Recommendation'">
        <xsl:value-of select="'CR'"/>
      </xsl:when>
      <xsl:when test="$tmp='Working Draft'">
        <xsl:value-of select="'WD'"/>
      </xsl:when>
      <xsl:when test="$tmp='Note'">
        <xsl:value-of select="'NOTE'"/>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="isLastCall">
    <xsl:variable name="tmp">
      <xsl:call-template name="getShortStatus"/>
    </xsl:variable>
    <xsl:if test="$tmp='WD'">
      <xsl:if test="contains(translate(normalize-space($status),$str:uc,$str:lc),'last call')">
        <xsl:text>yes</xsl:text>
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <xsl:template name="getDate">
    <xsl:value-of select="concat(normalize-space($pubdate/day),' ',normalize-space($pubdate/month),' ',normalize-space($pubdate/year))"/>
  </xsl:template>

  <xsl:template name="getNumericDate">
    <xsl:call-template name="date:makeDateNumeric">
      <xsl:with-param name="friendlyDate" select="$literal_date"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="getTitle">
    <xsl:value-of select="$head/title"/>
  </xsl:template>

  <xsl:template name="getVersionURI">
    <xsl:param name="which"/>
    <xsl:choose>
      <xsl:when test="$which='this'">
        <xsl:value-of select="$head/publoc/loc[1]/@href"/>
      </xsl:when>
      <xsl:when test="$which='latest'">
        <xsl:value-of select="$head/latestloc/loc[1]/@href"/>
      </xsl:when>
      <xsl:when test="$which='previous'">
        <xsl:value-of select="$head/prevlocs/loc[1]/@href"/>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template name="getEditorsSectionName">

  </xsl:template>

  <xsl:template name="getEditorsList">
    <xsl:apply-templates select="$head/authlist"/>
  </xsl:template>

  <xsl:template match="authlist">
    <xsl:for-each select="author">
      <xsl:variable name="tmp">
        <xsl:if test="normalize-space(email)">
          <xsl:value-of select="concat('mailto:',email)"/>
        </xsl:if>
      </xsl:variable>
      <xsl:call-template name="formatEditorName">
        <xsl:with-param name="EditorName" select="normalize-space(name)"/>
        <xsl:with-param name="EditorMailbox" select="normalize-space($tmp)"/>
      </xsl:call-template>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="formatEditorName">
    <xsl:param name="EditorName"/>
    <xsl:value-of select="$EditorName"/>
    <xsl:if test="position()!=last()">
      <xsl:value-of select="string(', ')"/>
    </xsl:if>
  </xsl:template>

  <xsl:template name="getErrata">
    <xsl:variable name="tmp">
      <xsl:call-template name="getShortStatus"/>
    </xsl:variable>
    <xsl:if test="$tmp='REC'">
      <xsl:for-each select="$status//loc/@href[normalize-space(translate(substring(.,string-length(.)-6+1),$str:uc,$str:lc))='errata']">
        <xsl:if test="position()=last()">
          <xsl:value-of select="."/>
        </xsl:if>
      </xsl:for-each>
    </xsl:if>
  </xsl:template>

  <xsl:template name="getTranslations">
    <xsl:variable name="tmp">
      <xsl:call-template name="getShortStatus"/>
    </xsl:variable>
    <xsl:if test="$tmp='REC'">
      <xsl:for-each select="$status//loc/@href[contains(.,'trans')]">
        <xsl:value-of select="."/>
      </xsl:for-each>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>