<?xml version="1.0" encoding="utf-8"?>
<!-- $Id: leagueSched.xsl,v 1.5 2003/06/03 17:36:33 connolly Exp connolly $ -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:h="http://www.w3.org/1999/xhtml"
  xmlns:r="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:dt   ="http://www.w3.org/2001/XMLSchema#"
  xmlns:l="http://www.w3.org/2002/12/cal/league@@#"
  xmlns:k    ="http://opencyc.sourceforge.net/daml/cyc.daml#"
                version="1.0">

  <xsl:output method="xml" indent="yes"/>

  <xsl:variable name="K_ns" select='"http://opencyc.sourceforge.net/daml/cyc.daml#"'/>
  <xsl:param name="year" select='2003'/>

  <xsl:template match="/">
    <xsl:variable name="gameDuration">
      <xsl:if test='.//h:p[contains(normalize-space(.), "must stop promptly at 1 hour 15 minutes")]'>
        <xsl:value-of select='"PT1H15M"'/>
      </xsl:if>
    </xsl:variable>

    <r:RDF>
      <xsl:for-each select='.//h:div/h:p[contains(., "DIVISION") or contains(., "EAST") or contains(., "WEST")]'>
        <xsl:variable name="division" select='generate-id()'/>

        <r:Description r:about='#{$division}'>
          <r:value><xsl:value-of select="."/></r:value>
          <l:webSite r:resource="http://www.bluevalleyrec.org/"/>

          <!-- each team: number, coach's name -->
          <!-- @@ < 9 is a kludge! -->
          <xsl:for-each select='../following-sibling::h:div[position() &lt; 9]'>
            <xsl:call-template name="teamInfo">
              <xsl:with-param name="division" select='$division'/>
            </xsl:call-template>
          </xsl:for-each>

        </r:Description>


        <xsl:for-each select='../following-sibling::h:table[1]'>
          <xsl:call-template name="schedTable">
            <xsl:with-param name="division" select='$division'/>
            <xsl:with-param name="gameDuration" select='$gameDuration'/>
          </xsl:call-template>
        </xsl:for-each>

      </xsl:for-each>
    </r:RDF>
  </xsl:template>

  <xsl:template name="teamInfo">
    <xsl:param name="division"/>

    <xsl:variable name="num" select='normalize-space(substring-before(., "."))'/>
    <xsl:variable name="coachName" select='normalize-space(substring-before(substring-after(., "."), "("))'/>
    <xsl:variable name="schoolAbbr" select='normalize-space(substring-before(substring-after(., "("), ")"))'/>

    <xsl:if test='$num and $coachName'>
      <k:part>
        <r:Description r:about='#t{concat($num, "_", $division)}'>
          <r:value><xsl:value-of select='$num'/></r:value>
          <l:coach r:parseType="Resource">
            <r:value><xsl:value-of select='$coachName'/></r:value>
          </l:coach>
          <xsl:if test='$schoolAbbr'>
            <l:school>
              <r:Description r:about="#school{$schoolAbbr}">
                <r:value><xsl:value-of select="$schoolAbbr"/></r:value>
              </r:Description>
            </l:school>
          </xsl:if>
        </r:Description>
      </k:part>
    </xsl:if>

  </xsl:template>

  <xsl:template name="schedTable">
    <xsl:param name="division"/>
    <xsl:param name="gameDuration"/>

    <xsl:for-each select='h:tr'>
      <xsl:variable name="dow" select='normalize-space(h:td[1])'/>
      <xsl:variable name="mmmdd" select='normalize-space(h:td[2])'/>
      <xsl:variable name="timeOfDay" select='normalize-space(h:td[3])'/>
      <xsl:variable name="field" select='normalize-space(h:td[4])'/>
      <xsl:variable name="teams" select='h:td[5]'/>

      <xsl:variable name="homeTeam" select='normalize-space(substring-before($teams, "-"))'/>
      <xsl:variable name="awayTeam" select='normalize-space(substring-after($teams, "-"))'/>
      <xsl:if test="$homeTeam and $awayTeam">
        
        <r:Description>
          <xsl:call-template name="timeAndDay">
            <xsl:with-param name="timeOfDay" select='$timeOfDay'/>
            <xsl:with-param name="mmmdd" select='$mmmdd'/>
            <xsl:with-param name="dow" select='$dow'/>
          </xsl:call-template>
          <xsl:if test='$gameDuration'>
            <l:duration><xsl:value-of select='$gameDuration'/></l:duration>
          </xsl:if>
          <k:eventOccursAt>
            <r:Description r:about='#field{$field}'>
              <r:value><xsl:value-of select='$field'/></r:value>
            </r:Description>
          </k:eventOccursAt>
          <l:homeTeam r:resource='#{concat("t", $homeTeam, "_", $division)}'/>
          <l:awayTeam r:resource='#{concat("t", $awayTeam, "_", $division)}'/>
        </r:Description>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="timeAndDay">
    <xsl:param name="mmmdd"/>
    <xsl:param name="dow"/>
    <xsl:param name="timeOfDay"/>

    <!-- THURSDAY -> Thursday -->
    <xsl:variable name="dowc" select='concat(substring($dow, 1, 1), translate(substring($dow, 2), "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"))'/>

    <!-- split JULY 3 into 3 and JULY -->
    <xsl:variable name="day" select='number(substring-after($mmmdd, " "))'/>
    <xsl:variable name="mmm" select='substring(substring-before($mmmdd, " "), 1, 3)'/>
    <xsl:variable name="month">
      <xsl:choose>
        <xsl:when test='$mmm = "JAN"'><xsl:value-of select='1'/></xsl:when>
        <xsl:when test='$mmm = "FEB"'><xsl:value-of select='2'/></xsl:when>
        <xsl:when test='$mmm = "MAR"'><xsl:value-of select='3'/></xsl:when>
        <xsl:when test='$mmm = "APR"'><xsl:value-of select='4'/></xsl:when>
        <xsl:when test='$mmm = "MAY"'><xsl:value-of select='5'/></xsl:when>
        <xsl:when test='$mmm = "JUN"'><xsl:value-of select='6'/></xsl:when>
        <xsl:when test='$mmm = "JUL"'><xsl:value-of select='7'/></xsl:when>
        <xsl:when test='$mmm = "AUG"'><xsl:value-of select='8'/></xsl:when>
        <xsl:when test='$mmm = "SEP"'><xsl:value-of select='9'/></xsl:when>
        <xsl:when test='$mmm = "OCT"'><xsl:value-of select='10'/></xsl:when>
        <xsl:when test='$mmm = "NOV"'><xsl:value-of select='11'/></xsl:when>
        <xsl:when test='$mmm = "DEC"'><xsl:value-of select='12'/></xsl:when>
      </xsl:choose>
    </xsl:variable>

    <!-- grok 12:30 PM, NOON, 10:30 AM -->
    <xsl:variable name="hh" select='substring-before($timeOfDay, ":")'/>
    <xsl:variable name="mm" select='substring(substring-after($timeOfDay, ":"), 1, 2)'/>
    <xsl:variable name="ap" select='normalize-space(substring(substring-after($timeOfDay, ":"), 3))'/>
    <xsl:variable name="hh_mm">
      <xsl:choose>
        <xsl:when test='$timeOfDay = "NOON" or $ap = "N"'>
          <xsl:value-of select='"12:00:00"'/>
        </xsl:when>
        <xsl:when test='$ap = "PM"'>
          <xsl:value-of select='concat(
                              format-number(number($hh) + 12, "00"),
                              ":",
                              format-number(number($mm), "00"), ":00"
                              )'/>
        </xsl:when>
        <xsl:when test='$ap = "AM"'>
          <xsl:value-of select='concat(
                              format-number(number($hh), "00"),
                              ":",
                              format-number(number($mm), "00"), ":00"
                              )'/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:message>
            can't grok time of day: `<xsl:value-of select='$timeOfDay'/>'
          ap: `<xsl:value-of select='$ap'/>'
          hh: `<xsl:value-of select='$hh'/>'
          mm: `<xsl:value-of select='$mm'/>'
          </xsl:message>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>


    <k:startingPoint r:parseType="Resource">
      <r:value>
        <xsl:value-of select='$hh_mm'/>
      </r:value>
      <k:startingDate r:parseType="Resource">
        <dt:date>
          <xsl:value-of select='concat(
                                format-number($year, "0000"), "-",
                                format-number($month, "00"), "-",
                                format-number($day, "00")
                                )'/>
        </dt:date>
        <r:type>
          <r:Description r:about='{concat($K_ns, $dowc)}'>
            <r:value><xsl:value-of select='$dowc'/></r:value>
          </r:Description>
        </r:type>
        <r:value><xsl:value-of select='$mmmdd'/></r:value>
      </k:startingDate>
    </k:startingPoint>

  </xsl:template>
</xsl:stylesheet>
