<xsl:transform
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:r="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:s="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:c="http://www.w3.org/2002/12/cal/icaltzd#"
    xmlns:h="http://www.w3.org/1999/xhtml"
    xmlns:apt="http://www.daml.org/2001/10/html/airport-ont#"
    xmlns:dt="http://www.w3.org/2001/XMLSchema#"
    xmlns:trav="http://www.w3.org/2000/10/swap/pim/travelTerms#"
    version="1.0"
    >
<div xmlns="http://www.w3.org/1999/xhtml">
<p>Extract RDF data out of itinerary choices from
the <a href="http://www.aa.com/">American Airlines web site</a>.</p>

<p>Search for an itinerary and then get the print view and save it,
then tidy it, then run this XSLT transformation to get a iCalendar
VCalendar object for each choice, and a VEvent for each flight.</p>

</div>

<xsl:output indent="yes"/>

<xsl:template match="/">
  <r:RDF
      xmlns:owl="http://www.w3.org/2002/07/owl#">
    <!-- this should move to icaltzd schema... -->
    <r:Description
	r:about="http://www.w3.org/2002/12/cal/icaltzd#summary">
      <s:subPropertyOf
	  r:resource="http://www.w3.org/2000/01/rdf-schema#label" />
    </r:Description>

    <r:Description r:about="http://www.daml.org/2001/10/html/airport-ont#iataCode">
      <r:type r:resource="http://www.w3.org/2002/07/owl#InverseFunctionalProperty" />
    </r:Description>
    <xsl:apply-templates />
  </r:RDF>
</xsl:template>

<!-- each option... -->
<xsl:template match="h:table[contains(h:tr/h:td/h:table/h:tr,
  'Average Fare')]">
  <c:Vcalendar r:ID="{generate-id()}">
    <xsl:variable name="itinInfo" select="h:tr/h:td/h:table/
					  h:tr/h:td" />
    <xsl:variable name="price"
		  select='substring-before(normalize-space(
			  substring-after($itinInfo/text()[last()], " ")), " USD")'/>

    <trav:priceUSD r:datatype="http://www.w3.org/2001/XMLSchema#double">
      <xsl:value-of select="$price" />
    </trav:priceUSD>

    <c:summary>
      <xsl:value-of select="normalize-space($itinInfo)" />
    </c:summary>

    <xsl:for-each select="h:tr[2]/h:td/h:table/h:tr[h:td/@class='resultCell']">
      <c:component>
	<xsl:variable name="carrier" select="normalize-space(h:td[1])" />
	<xsl:variable name="fltNum" select="normalize-space(h:td[2])" />
	<xsl:variable name="lvPlace" select="normalize-space(h:td[3])" />
	<xsl:variable name="lvTime" select="normalize-space(h:td[4])" />
	<xsl:variable name="arPlace" select="normalize-space(h:td[5])" />
	<xsl:variable name="arTime" select="normalize-space(h:td[6])" />
	<xsl:variable name="eqp" select="normalize-space(h:td[7])" />
	<xsl:variable name="cabin" select="normalize-space(h:td[8])" />
	<xsl:variable name="miles" select="normalize-space(h:td[9])" />
	<!-- skip meals -->

	<xsl:variable name="lvapt" select='substring-before($lvPlace, " ")' />
	<xsl:variable name="arapt" select='substring-before($arPlace, " ")' />
	<xsl:variable name="dtstart">
	  <xsl:call-template name="grokDate">
	    <xsl:with-param name="when" select="$lvTime" />
	  </xsl:call-template>
	</xsl:variable>

	<xsl:variable name="fltDetails"
		      select='concat($fltNum, $lvapt, "_", $arapt,
			      "_", $dtstart)' />
	<xsl:variable name="fltID"
		      select='translate($fltDetails, "-:", "")' />
	<c:Vevent r:ID='flt{$fltID}'>

	  <c:summary>
	    <xsl:value-of select="$carrier" />
	    <xsl:text> #</xsl:text>
	    <xsl:value-of select="$fltNum" />
	    <xsl:text> to </xsl:text>
	    <xsl:value-of select="$arPlace" />
	  </c:summary>


	  <c:dtstart
	      r:datatype="http://www.w3.org/2002/12/cal/icaltzd#dateTime">
	    <xsl:value-of select="$dtstart" />
	  </c:dtstart>

	  <c:dtend
	      r:datatype="http://www.w3.org/2002/12/cal/icaltzd#dateTime">
	    <xsl:call-template name="grokDate">
	      <xsl:with-param name="when" select="$arTime" />
	    </xsl:call-template>
	  </c:dtend>

	  <c:location>
	    <r:Description r:about="#path_{$lvapt}_to_{$arapt}">
	      <r:value>
		<xsl:value-of select="$lvPlace" />
		<xsl:text> to </xsl:text>
		<xsl:value-of select="$arPlace" />
	      </r:value>
	      <trav:startLocation>
		<xsl:call-template name="describeAirport">
		  <xsl:with-param name="apt" select="$lvapt" />
		  <xsl:with-param name="label" select="$lvPlace" />
		</xsl:call-template>
	      </trav:startLocation>
	      <trav:endLocation>
		<xsl:call-template name="describeAirport">
		  <xsl:with-param name="apt" select="$arapt" />
		  <xsl:with-param name="label" select="$arPlace" />
		</xsl:call-template>
	      </trav:endLocation>
	    </r:Description>
	  </c:location>

	  <c:description>
	    <xsl:value-of select="normalize-space(.)" />
	  </c:description>
	</c:Vevent>
      </c:component>
    </xsl:for-each>
  </c:Vcalendar>
</xsl:template>


<xsl:template name="describeAirport">
  <xsl:param name="apt" />
  <xsl:param name="label" />
  <r:Description
      r:about="http://dig.csail.mit.edu/2006/wikdt/airports?iata={$apt}#item">
    <s:label><xsl:value-of select="$label" /></s:label>
    <apt:iataCode><xsl:value-of select="$apt" /></apt:iataCode>
  </r:Description>
</xsl:template>

<xsl:template name="grokDate">
  <!-- adapted from ../../2006/05dc-edi/itin-enrich.xsl -->
  <xsl:param name="when" />

  <xsl:variable name="mdy"
		select="substring-before($when, ' ')" />
  <xsl:variable name="mm"
		select='number(substring(substring-after($when, ":"),
			1, 2))' />
  <xsl:variable name="hh"
		select='number(substring-before(
			substring-after($when, " ")
			, ":"))' />
  <xsl:variable name="hr">
    <xsl:choose>
      <xsl:when test='$hh &lt; 12
		      and substring($when, string-length($when)-1) = "PM"'>
	<xsl:value-of select="$hh + 12" />
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select="$hh" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:value-of
		select='concat(substring($mdy, 7, 4), "-",
                               substring($mdy, 1, 2), "-",
                               substring($mdy, 4, 2), "T",
			       format-number($hr, "00"), ":",
			       format-number($mm, "00"), ":00"
			       )' />
</xsl:template>

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

</xsl:transform>