<xsl:transform
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0"
  xmlns:h="http://www.w3.org/1999/xhtml"
  xmlns:r="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:s="http://www.w3.org/2000/01/rdf-schema#"
  xmlns:k="http://opencyc.sourceforge.net/daml/cyc.daml#"
  xmlns:t="http://www.w3.org/2000/10/swap/pim/travelTerms#"
  xmlns:dt="http://www.w3.org/2001/XMLSchema#"
  xmlns:apt="http://www.daml.org/2001/10/html/airport-ont#"
  >

<!-- 

$Id: grokItinTable.xsl,v 1.1 2004/07/05 12:41:36 connolly Exp $
cribbed from ../../2003/09dc-bos/grokItinTable.xsl
Id: grokItinTable.xsl,v 1.2 2003/08/26 00:14:58 connolly Exp 

USAGE:

1. get an HTML intinerary from virtuallythere.com; the printable version
2. tidy -asxml trip.html >ttrip.html
3. xsltproc  grokItinTable.xsl ttrip.html  >,trip.rdf
-->


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

<xsl:param name="Year" select='2004'/>

<xsl:template match="/">
  <r:RDF>
    <r:Description r:about="">
      <k:containsInformationAbout-Focally r:parseType="Resource">
        <xsl:apply-templates/>
      </k:containsInformationAbout-Focally>
    </r:Description>
  </r:RDF>
</xsl:template>


<xsl:template match='h:table[h:tr/h:td/h:b = "From:"]'>
  <xsl:message>found flight table
  </xsl:message>

  <xsl:variable name='flightInfo' select='normalize-space(translate(h:tr[1]/h:td[1], "&#160;", " "))'/>

  <xsl:variable name='yyyymmdd'>
    <xsl:call-template name='grokDate'>
      <xsl:with-param name="mmmdd"
		      select='normalize-space(substring-after(substring-before($flightInfo, ":"), ","))'/>
    </xsl:call-template>
  </xsl:variable>


  <xsl:variable name='carrierName' select='normalize-space(substring-before(substring-after($flightInfo, ":"), ","))'/>
  <xsl:variable name='flightCode' select='normalize-space(substring-after(substring-after($flightInfo, ":"), ","))'/>
  <xsl:variable name='flightNum' select='substring-after($flightCode, " ")'/>

  <xsl:variable name='depPlaceName' select='normalize-space(h:tr[3]/h:td[2])'/>
  <xsl:variable name='depAptName' select='substring(substring-after($depPlaceName, "("), 1, 3)'/>

  <xsl:variable name='arPlaceName' select='normalize-space(h:tr[5]/h:td[2])'/>
  <xsl:variable name='arAptName' select='substring(substring-after($arPlaceName, "("), 1, 3)'/>

  <xsl:variable name='depTime'>
    <xsl:call-template name='grokTime'>
      <xsl:with-param name="when"
		      select='normalize-space(h:tr[3]/h:td[4])'/>
    </xsl:call-template>
  </xsl:variable>

  <!-- arrival time might be the 4th or 5th row -->
  <xsl:variable name='arTime'>
    <xsl:call-template name='grokTime'>
      <xsl:with-param name="when"
		      select='normalize-space(
		        translate(concat(h:tr[4]/h:td[4], h:tr[5]/h:td[4]),
                                  "&#160;", " "))'/>
    </xsl:call-template>
  </xsl:variable>


  <xsl:message>found flight info: <xsl:value-of select='$flightInfo'/>
  when: <xsl:value-of select='$yyyymmdd'/>
  carrier: <xsl:value-of select='$carrierName'/>
  code: <xsl:value-of select='$flightCode'/>
  num: <xsl:value-of select='$flightNum'/>
  dep: <xsl:value-of select='$depAptName'/>
  depTime: <xsl:value-of select='$depTime'/>
  ar: <xsl:value-of select='$arAptName'/>
  arTime: <xsl:value-of select='$arTime'/>
  </xsl:message>


  <!-- skipping: flight class -->
  
  <xsl:variable name='seatName' select='normalize-space(h:tr[7]/h:td[4])'/>
  
  <!-- @@passenger name? -->
  

  <k:subEvents r:parseType="Resource">
    <k:startingDate>
      <r:Description r:about="#date_{$yyyymmdd}">
	<dt:date><xsl:value-of select="$yyyymmdd"/></dt:date>
      </r:Description>
    </k:startingDate>
    <!-- @@cheat -->
    <k:endingDate>
      <r:Description r:about="#date_{$yyyymmdd}">
	<dt:date><xsl:value-of select="$yyyymmdd"/></dt:date>
      </r:Description>
    </k:endingDate>
    
    <t:flightNumber><xsl:value-of select="$flightNum"/></t:flightNumber>
    <t:departureTime><xsl:value-of select="$depTime"/></t:departureTime>
    <t:arrivalTime><xsl:value-of select="$arTime"/></t:arrivalTime>
    
    <k:fromLocation>
      <k:Airport-Physical>
	<apt:iataCode><xsl:value-of select="$depAptName"/></apt:iataCode>
      </k:Airport-Physical>
    </k:fromLocation>
    
    <k:toLocation>
      <k:Airport-Physical>
	<apt:iataCode><xsl:value-of select="$arAptName"/></apt:iataCode>
      </k:Airport-Physical>
    </k:toLocation>
    
    <t:carrier r:parseType="Resource">
      <k:nameOfAgent><xsl:value-of select="$carrierName"/></k:nameOfAgent>
    </t:carrier>
    
    <!-- @@others -->
    
  </k:subEvents>

</xsl:template>

<xsl:template name="grokDate">
  <xsl:param name="mmmdd"/> <!-- e.g. May 15 -->
  <xsl:message>
    grokking date: <xsl:value-of select='$mmmdd'/>
  </xsl:message>
  <xsl:variable name='dayOfMonth'
		select='number(substring-after($mmmdd, " "))'/>
  <xsl:variable name='month'>
    <xsl:call-template name="monthIndex">
      <xsl:with-param name="name"
		      select='substring-before($mmmdd, " ")'/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:value-of select='concat(format-number($Year, "0000"), "-", format-number(number($month), "00"), "-", format-number($dayOfMonth, "00"))'/>
</xsl:template>


<xsl:template name="monthIndex">
  <!-- cribbed from schedScrape.xsl 
       schedScrape.xsl,v 1.12 2004/04/02 20:50:41
   -->
  <xsl:param name="name"/>

  <xsl:variable name="mmm">
    <xsl:value-of select='substring(translate($name, "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1, 3)'/>
  </xsl:variable>
  <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:template>


<xsl:template name="grokTime">
  <xsl:param name="when"/> <!-- format: HH:MMPP -->

  <xsl:variable name='hr' select='substring-before($when, ":")'/>
  <xsl:variable name='mn' select='substring(substring-after($when, ":"), 1, 2)'/>
  <xsl:variable name='pmoff'>
    <xsl:choose>
      <xsl:when test='substring(substring-after($when, ":"), 3, 2) = "pm"'>12</xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:value-of select='concat(format-number(number($hr) + number($pmoff), "00"), ":", format-number($mn, "00"))'/>
</xsl:template>




<!-- don't pass text thru -->
<xsl:template match="text()|@*">
</xsl:template>

</xsl:transform>

