#36 [html]

BART Schedule as a Smallx Pipeline

<p:pipe xmlns:p="urn:publicid:IDN+smallx.com:pipeline:1.0" name="bart-schedule"
    xmlns:c="urn:publicid:IDN+smallx.com:component-language:1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:h="http://www.w3.org/1999/xhtml"
>

<!-- Scope the service to the bart-schedule elements -->
<p:subtree select="bart-schedule">

   <!-- Translate the request to a 'routes' element and add the c:url-get input 
        for the url component -->
   <p:template>
   <xsl:for-each select="bart-schedule">
   <routes>
   <xsl:copy-of select="node()"/>
   <c:url-get href="http://www.bart.gov/index.asp?origin={from}&amp;destination={to}&amp;time_mode=departs&amp;depart_month={departing/month}&amp;depart_date={departing/day}&amp;depart_time={substring-before(departing/time,' ')}%20{substring-after(departing/time,' ')}&amp;cookiesTested=1"
              parse-as-html="true"/>
   </routes>
   </xsl:for-each>
   </p:template>

   <!-- Get the schedule -->
   <p:url/>

   <!-- delete unnecessary elements -->
   <p:subtree select='h:script|h:meta|h:head'>
      <p:delete/>
   </p:subtree>

   <!-- Find the contents table and drop the rest -->
   <p:subtree select="h:table">
      <p:template>
      <xsl:copy-of select="h:table/h:tbody/h:tr/h:td[h:a/@name='content']/h:div[@id='bodytext']/h:table[contains(h:tr[1]/h:td[1],'Your Schedule')]"/>
      </p:template>
   </p:subtree>

   <!-- Find the schedule tables and drop the rest -->
   <p:subtree select="h:table">
      <p:xslt>
      <xsl:transform version="1.0">

         <xsl:template match="/">
         <search-results>
         <xsl:apply-templates select="h:table/h:tr/h:td/h:table/h:tr/h:td/h:table"/>
         </search-results>
         </xsl:template>

         <xsl:template match="h:table|h:tr|h:td|h:a">
         <xsl:copy> 
         <xsl:apply-templates select="@href|node()"/>
         </xsl:copy>
         </xsl:template>

         <xsl:template match="h:br"><xsl:text> </xsl:text></xsl:template>
  
         <xsl:template match="@*"><xsl:copy/></xsl:template>

      </xsl:transform>
      </p:xslt>

      <!-- Translate the schedule tables into the 'route-option' element -->
      <p:xslt>
      <xsl:transform version="1.0">

         <xsl:template match="h:table[not(preceding-sibling::*)]"/>

         <xsl:template match="h:table" xml:space='preserve'>
         <route-option>
         <xsl:apply-templates select="h:tr"/>
         </route-option>
         </xsl:template>

         <xsl:template match="h:tr[not(preceding-sibling::*)]"/>
  
         <xsl:template match="h:tr" xml:space='preserve'>
         <train>
         <xsl:apply-templates select="h:td"/>
         </train>
         </xsl:template>

         <xsl:template match="h:td[1]">
         <depart><xsl:value-of select="normalize-space(.)"/></depart>
         <xsl:text>
         </xsl:text>
         </xsl:template>

         <xsl:template match="h:td[3]">
         <board><xsl:value-of select="normalize-space(.)"/></board>
         <xsl:text>
         </xsl:text>
         </xsl:template>

         <xsl:template match="h:td[4]">
         <arrive><xsl:value-of select="normalize-space(.)"/></arrive>
         <xsl:text>
         </xsl:text>
         </xsl:template>

         <xsl:template match="h:td"/>

      </xsl:transform>
      </p:xslt>
   </p:subtree>

   <!-- drop extras that remain -->
   <p:subtree select='h:br'>
      <p:delete/>
   </p:subtree>

   <!-- Unwrap the route-options from the XHTML -->
   <p:unwrap select='h:html'/>
   <p:unwrap select='h:body'/>

</p:subtree>

</p:pipe>