<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:arr="http://edginet.local/arr"
    xmlns:exsl="http://exslt.org/common"
    extension-element-prefixes="exsl"
    exclude-result-prefixes="arr"
>

<!--

  This transformation produces a list of all sermons in the sermons.xml
  database in the order determined by the $sort parameter and formats
  it as rows in a HTML table.  It is used by index.php.

-->

<!--
  The sorting order: default is "date" but "ref" can be passed
  in from the calling script
-->
<xsl:param name="sort">date</xsl:param>

<xsl:output
  method="xml"
  omit-xml-declaration="yes"
  encoding="utf-8"
  indent = "yes"
/>

<!-- Main template -->
<xsl:template match="/sermons">

  <xsl:variable name="table">

    <!-- Do loop according to sort method.  "xsl:sort" must
         be the direct child of "xsl:for-each", not a descendent -->
    <xsl:choose>

      <!-- Sort by Bible reference -->
      <xsl:when test="$sort='ref'">
        <xsl:for-each select="sermon">
          <xsl:sort select="ref[@class='main']/start"/>
          <xsl:apply-templates select="."/>
        </xsl:for-each>
      </xsl:when>

      <!-- Sort by date -->
      <xsl:when test="$sort='date'">
        <xsl:for-each select="sermon">
          <xsl:sort select="date/y" order="descending"/>
          <xsl:sort select="date/m" order="descending"/>
          <xsl:sort select="date/d" order="descending"/>
          <xsl:apply-templates select="."/>
        </xsl:for-each>
      </xsl:when>

      <!-- Or just process in file order -->
      <xsl:otherwise>
        <xsl:for-each select="sermon">
          <xsl:apply-templates select="."/>
        </xsl:for-each>
      </xsl:otherwise>

    </xsl:choose>

  </xsl:variable>

  <xsl:for-each select="exsl:node-set($table)/tr">
    <xsl:apply-templates select="."/>
  </xsl:for-each>

</xsl:template>

<xsl:template match="sermon">
  <tr>
    <date>
      <xsl:apply-templates select="date"/>
    </date>
    <title>
      <xsl:apply-templates select="title"/>
    </title>
    <text>
      <xsl:apply-templates select="ref[@class='main']"/>
      <xsl:apply-templates select="ref[@class='extra']">
        <xsl:sort select="@num"/>
      </xsl:apply-templates>
    </text>
    <place>
      <xsl:apply-templates select="place"/>
    </place>
  </tr>
</xsl:template>

<xsl:template match="title">
  <a href="{../name}.html">
    <xsl:value-of select="." />
  </a>
</xsl:template>

<xsl:template match="place">
  <xsl:value-of select="." />
</xsl:template>

<xsl:template match="ref[@class='main']">
  <xsl:if test="not(position()=1)"><xsl:text>, </xsl:text></xsl:if>
  <xsl:apply-templates select="text"/>
</xsl:template>

<xsl:template match="ref[@class='extra']">
  <xsl:text>, </xsl:text>
  <xsl:apply-templates select="text"/>
</xsl:template>

<xsl:template match="text">
    <a>
      <xsl:attribute name="href">http://bible.gospelcom.net/bible?passage=<xsl:value-of select="." />&amp;language=english&amp;version=NIV</xsl:attribute>
      <xsl:value-of select="." />
    </a>
</xsl:template>

<xsl:template match="tr">
  <tr>
    <td><xsl:value-of select="date"/></td>
    <td><xsl:copy-of select="title/a"/></td>
    <td><xsl:copy-of select="text/a|text/text()"/></td>
    <td>
      <xsl:choose>
        <xsl:when test="preceding-sibling::tr[1]/place=./place">
          <xsl:text>&#xa0;&#xa0;&quot;&#xa0;&#xa0;&quot;</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="./place"/>
        </xsl:otherwise>
      </xsl:choose>
    </td>
  </tr>
</xsl:template>

<arr:months>
  <month n="01">January</month>
  <month n="02">February</month>
  <month n="03">March</month>
  <month n="04">April</month>
  <month n="05">May</month>
  <month n="06">June</month>
  <month n="07">July</month>
  <month n="08">August</month>
  <month n="09">September</month>
  <month n="10">October</month>
  <month n="11">November</month>
  <month n="12">December</month>
</arr:months>

<xsl:key name="month" match="month" use="@n"/>

<!--
  the document('') business is a cunning way to access the months
  array as a node-set (portably) rather than as a result tree
  fragment in a variable.  NB extension functions are *not* portable.
-->
<xsl:template match="date">
  <xsl:number value="d"/>
  <xsl:text> </xsl:text>
  <!-- <xsl:value-of select="document('')//arr:months/month[number(current()/m)]" /> -->
  <xsl:variable name="m" select="m"/>
  <xsl:for-each select="document('')">
    <xsl:value-of select="key('month',$m)" />
  </xsl:for-each>
  <xsl:text> </xsl:text>
  <xsl:number value="y"/>
</xsl:template>

</xsl:stylesheet>