XSLT-1.0-Code von »Google_Weather.xsl«

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3 
  4   <!--
  5        Beispiel zur Verarbeitung der XML-basierten Wetterdaten
  6        von Google Weather mittels XSLT - by Dr. T. Meinike 04/11
  7 
  8        XML-Eingabedaten in dieser Form abfragen:
  9        http://www.google.com/ig/api?weather=Merseburg&hl=de
 10        http://www.google.com/ig/api?weather=06217-Germany&hl=de
 11   -->
 12 
 13   <xsl:output method="xml" encoding="UTF-8" indent="yes"
 14     doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
 15     doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
 16 
 17   <xsl:template match="/xml_api_reply/weather">
 18     <xsl:variable name="region" select="forecast_information/city/@data"/>
 19 
 20     <html>
 21 
 22       <head>
 23         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 24         <title>Wetterdaten für <xsl:value-of select="$region"/></title>
 25         <style type="text/css">
 26           h1,h2 {font-family: sans-serif;}
 27           table,th,td {border: 1px solid #CCC; border-collapse: collapse;}
 28           th,td {text-align: center; padding: 5px;}
 29         </style>
 30       </head>
 31 
 32       <body>
 33         <h1>Wetterdaten für <xsl:value-of select="$region"/></h1>
 34 
 35         <h2>Aktuell</h2>
 36 
 37         <ul>
 38           <xsl:apply-templates select="current_conditions"/>
 39         </ul>
 40 
 41         <h2>Vorhersage</h2>
 42 
 43         <table>
 44           <thead>
 45             <tr><xsl:apply-templates select="forecast_conditions/day_of_week"/></tr>
 46           </thead>
 47           <tbody>
 48             <tr>
 49               <xsl:apply-templates select="forecast_conditions/low"/>
 50             </tr>
 51             <tr>
 52               <xsl:apply-templates select="forecast_conditions/icon"/>
 53             </tr>
 54             <tr>
 55               <xsl:apply-templates select="forecast_conditions/condition"/>
 56             </tr>
 57           </tbody>
 58         </table>
 59 
 60         <p><a href="http://www.google.com/ig/api?weather={forecast_information/postal_code/@data}&amp;hl=de">
 61           XML-Datenquelle: Google Weather API</a></p>
 62 
 63       </body>
 64 
 65     </html>
 66   </xsl:template>
 67 
 68 
 69   <xsl:template match="current_conditions">
 70     <li>Datum/Zeit: <xsl:value-of select="../forecast_information/current_date_time/@data"/></li>
 71     <li>Temperatur: <xsl:value-of select="temp_c/@data"/> °C</li>
 72     <li><xsl:value-of select="humidity/@data"/></li>
 73     <li><xsl:value-of select="wind_condition/@data"/></li>
 74     <li><xsl:value-of select="condition/@data"/><br />
 75     <img src="icons/{substring-after(icon/@data, '/ig/images/weather/')}" alt="{condition/@data}"/></li>
 76     <!--
 77          Hinweis: 22 Icons liegen lokal im Unterverzeichnis icons vor. Alternativer Onlinezugriff:
 78          <img src="http://www.google.com{icon/@data}" alt="{condition/@data}"/>
 79     -->
 80   </xsl:template>
 81 
 82 
 83   <xsl:template match="forecast_conditions/day_of_week">
 84     <th>
 85       <xsl:value-of select="@data"/>
 86       <xsl:if test="position() = 1"><br />(heute)</xsl:if>
 87     </th>
 88   </xsl:template>
 89 
 90 
 91   <xsl:template match="forecast_conditions/low">
 92     <td>
 93       <xsl:value-of select="concat(@data, ' – ', following-sibling::high/@data, ' °C')"/>
 94     </td>
 95   </xsl:template>
 96 
 97 
 98   <xsl:template match="forecast_conditions/icon">
 99     <td>
100       <img src="icons/{substring-after(@data, '/ig/images/weather/')}" alt="{following-sibling::condition/@data}"/>
101       <!--
102            Hinweis: Alternativer Onlinezugriff auf die Icons:
103            <img src="http://www.google.com{@data}" alt="{following-sibling::condition/@data}"/>
104       -->
105     </td>
106   </xsl:template>
107 
108 
109   <xsl:template match="forecast_conditions/condition">
110     <td>
111       <xsl:value-of select="@data"/>
112     </td>
113   </xsl:template>
114 
115 </xsl:stylesheet>

Code formatiert mit »Highlight 3.4«