<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!--
       Beispiel zur Verarbeitung der XML-basierten Wetterdaten
       von Google Weather mittels XSLT - by Dr. T. Meinike 04/11

       XML-Eingabedaten in dieser Form abfragen:
       http://www.google.com/ig/api?weather=Merseburg&hl=de
       http://www.google.com/ig/api?weather=06217-Germany&hl=de
  -->

  <xsl:output method="xml" encoding="UTF-8" indent="yes"
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>

  <xsl:template match="/xml_api_reply/weather">
    <xsl:variable name="region" select="forecast_information/city/@data"/>

    <html>

      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Wetterdaten für <xsl:value-of select="$region"/></title>
        <style type="text/css">
          h1,h2 {font-family: sans-serif;}
          table,th,td {border: 1px solid #CCC; border-collapse: collapse;}
          th,td {text-align: center; padding: 5px;}
        </style>
      </head>

      <body>
        <h1>Wetterdaten für <xsl:value-of select="$region"/></h1>

        <h2>Aktuell</h2>

        <ul>
          <xsl:apply-templates select="current_conditions"/>
        </ul>

        <h2>Vorhersage</h2>
        
        <table>
          <thead>
            <tr><xsl:apply-templates select="forecast_conditions/day_of_week"/></tr>
          </thead>
          <tbody>
            <tr>
              <xsl:apply-templates select="forecast_conditions/low"/>
            </tr>
            <tr>
              <xsl:apply-templates select="forecast_conditions/icon"/>
            </tr>
            <tr>
              <xsl:apply-templates select="forecast_conditions/condition"/>
            </tr>
          </tbody>
        </table>

        <p><a href="http://www.google.com/ig/api?weather={forecast_information/postal_code/@data}&amp;hl=de">
          XML-Datenquelle: Google Weather API</a></p>
        
      </body>

    </html>  
  </xsl:template>


  <xsl:template match="current_conditions">
    <li>Datum/Zeit: <xsl:value-of select="../forecast_information/current_date_time/@data"/></li>
    <li>Temperatur: <xsl:value-of select="temp_c/@data"/> °C</li>
    <li><xsl:value-of select="humidity/@data"/></li>
    <li><xsl:value-of select="wind_condition/@data"/></li>
    <li><xsl:value-of select="condition/@data"/><br />
    <img src="icons/{substring-after(icon/@data, '/ig/images/weather/')}" alt="{condition/@data}"/></li>
    <!--
         Hinweis: 22 Icons liegen lokal im Unterverzeichnis icons vor. Alternativer Onlinezugriff:
         <img src="http://www.google.com{icon/@data}" alt="{condition/@data}"/>
    -->
  </xsl:template>


  <xsl:template match="forecast_conditions/day_of_week">
    <th>
      <xsl:value-of select="@data"/>
      <xsl:if test="position() = 1"><br />(heute)</xsl:if>
    </th>
  </xsl:template>


  <xsl:template match="forecast_conditions/low">
    <td>
      <xsl:value-of select="concat(@data, ' – ', following-sibling::high/@data, ' °C')"/>
    </td>
  </xsl:template>


  <xsl:template match="forecast_conditions/icon">
    <td>
      <img src="icons/{substring-after(@data, '/ig/images/weather/')}" alt="{following-sibling::condition/@data}"/>
      <!--
           Hinweis: Alternativer Onlinezugriff auf die Icons:
           <img src="http://www.google.com{@data}" alt="{following-sibling::condition/@data}"/>
      -->
    </td>
  </xsl:template>


  <xsl:template match="forecast_conditions/condition">
    <td>
      <xsl:value-of select="@data"/>
    </td>
  </xsl:template>

</xsl:stylesheet>
