<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns="http://www.hackingcongress.org/ns/Politics#"
  version="1.0">

  <xsl:output method="xml" indent="yes"/>

  <!-- This is our main template. It starts things off. -->
  <xsl:template match="/">
    <!-- Create an RDF wrapper. -->
    <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
      xmlns:pol="http://www.hackingcongress.org/ns/Politics">
      <!-- Now, get all the the "tr" elements in the page, and apply the "tr" template to each one. -->
      <xsl:apply-templates select="//tr"/>
    </rdf:RDF>
  </xsl:template>
  
  <!-- This is the template for all "tr" elements. -->
  <xsl:template match="tr">

    <!--Now we create a variable for the single quote, to use for
         later pattern matches (XSLT doesn't have an easy quote escape
         mechanism). -->

    <xsl:variable name="q">'</xsl:variable>

    <!-- We are looking for the first tr element that comes before a
         tr element with a divider graphic -->

    <xsl:if test="following-sibling::tr[1]/td/img[@src='/resources/graphic/horiz_content_break.gif']">
      
      <!-- Okay! Now let's create a URI to use in our rdf:about
           attribute, using the senator's website as our value. -->
      <xsl:variable name="uri" select="preceding-sibling::tr[3]/td[1]/*/a/@href"/>

      <!-- The content gets muddy here. The format is "Lastname,
           Firstname (D - OH)" with lots of unusual linebreaks, etc,
           in the source. So we "normalize-space" to clean it up. -->

      <xsl:variable name="name-party-state" select="normalize-space(preceding-sibling::tr[3]/td[1]/*)"/>
      <!-- Now we cut the name out of the string. -->
      <xsl:variable name="name" select="substring-before($name-party-state, ' -')"/>
      <!-- And we can begin to spit out our Senator's RDF record. -->
      <USSenator rdf:about="{$uri}">
        <FullName><xsl:value-of select="$name"/></FullName>
        <URI><xsl:value-of select="$uri"/></URI>
        <Party>
          <!-- We need to pull out the letter indicating a senator's
               party from the string -->
          <xsl:variable name="party">
            <xsl:value-of select="normalize-space(substring-before(substring-after($name-party-state, '- ('), '-'))"/>              
          </xsl:variable>
          <!-- And now we convert it to a full word. -->
          <xsl:choose>
            <xsl:when test="$party='D'">Democrat</xsl:when>
            <xsl:when test="$party='R'">Republican</xsl:when>
            <xsl:when test="$party='I'">Independent</xsl:when>
          </xsl:choose>
        </Party>
        <State>
          <!-- Same as before, we pull the senator's state from a
               string. -->
          <xsl:value-of select="normalize-space(substring(substring-before($name-party-state, ')'), string-length($name-party-state)-3))"/>              
        </State>
        <Address><xsl:value-of select="normalize-space(preceding-sibling::tr[2]/td/*)"/></Address>
        <Phone><xsl:value-of select="preceding-sibling::tr[1]/td/*"/></Phone>
        <!-- And we include the Class information, as well. -->
        <SenateClass><xsl:value-of select="substring-after(normalize-space(preceding-sibling::tr[3]/td[2]/*),'Class ')"/></SenateClass>

        <!-- Finally, we pull out the contact information, and we're
             done.-->
        <ContactURI><xsl:value-of select="substring-before(substring-after(td/*/a/@href,concat('(',$q)),concat($q,')'))"/></ContactURI>
      </USSenator>
    </xsl:if>
  </xsl:template>
  
  
</xsl:stylesheet>

