Tecnologías XML / XSLT / XSLT 3.0 / Streaming XSLT / Elementos / xsl:stream / Informe meteorológico

Ejercicio 6

Escriba una transformación XSLT que obtenga los datos meteorológicos actuales de openweathermap.org para un código postal mediante xsl:stream y los presente en un informe meteorológico HTML.

Como no hay un archivo de entrada local, utilice / como valor del atributo match para la plantilla de entrada. Después puede aplicarse la transformación al XSLT.

 

Ejemplo de código: salida requerida del ejercicio 6 (fragmento).

<html>
  <head>
    <title>Wetter</title>
    <link href="../styling/styles.css" rel="stylesheet" />
    <meta charset="utf-8"/>
  </head>
  <body>
    <div class="city">
      <h1>Heslach</h1>
      <iframe src="..." class="map"></iframe>
      <div class="sun">
        <p>Sonnenaufgang: 7:03 Uhr</p>
        <p>Sonnenuntergang: 15:27 Uhr</p>
      </div>
      <div class="temperature">
        <p>Aktuelle Temperatur: 3 °C</p>
        <p>Min Temperatur: 3 °C</p>
        <p>Max Temperatur: 3 °C</p>
      </div>
      <p class="lastUpdate">Letzte Aktualisierung: 20:20 Uhr</p>
    </div>
  </body>
</html>

Informe meteorológico transformado a HTML.

Utilice el escenario de transformación [Übung 6] xsl:stream - Wetterbericht (XSL >> HTML) (véanse las explicaciones de los ejercicios) y el siguiente archivo XSLT:

Ejemplo de código: hoja de estilo del ejercicio 6: informe meteorológico.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:hdm="http://www.hdm-stuttgart.de" exclude-result-prefixes="xs hdm" version="3.0">
  <!-- OpenWeatherMap-URL: (PLZ ersetzen) http://api.openweathermap.org/data/2.5/weather?zip=PLZ,de&amp;appid=2de143494c0b295cca9337e1e96b00e0&amp;mode=xml        
       Output-Vorlage:
       <html>
         <head>
           <title>Wetter</title>
           <link href="../styling/styles.css" rel="stylesheet" />
           <meta charset="utf-8"/>
         </head>
         <body>
           <div class="city">
             <h1>Bempflingen</h1>
             <iframe src="https://www.google.com/maps/embed/v1/place?q=48.57,9.27&amp;key=AIzaSyAN0om9mFmy1QN6Wf54tXAowK4eT0ZUPrU" class="map"></iframe>
             <div class="sun">
               <p>Sonnenaufgang: 7:13 Uhr</p>
               <p>Sonnenuntergang: 15:46 Uhr</p>
             </div>
             <div class="temperature">
               <p>Aktuelle Temperatur: 3 °C</p>
               <p>Min Temperatur: 2 °C</p>
               <p>Max Temperatur: 4 °C</p>
             </div>
             <p class="lastUpdate">Letzte Aktualisierung: 12:34 Uhr</p>
           </div>
         </body>
       </html>    
  -->
  <xsl:output method="xhtml" indent="yes" />
  <xsl:mode streamable="yes" on-no-match="shallow-skip" />
  <xsl:template match="/">
    <html>
      <head>
        <title>Wetter</title>
        <link href="../styling/styles.css" rel="stylesheet" />
        <meta charset="utf-8"/>
      </head>
      <body>
        <!-- HIER WETTER-SEITE EINSETZEN -->
      </body>
    </html>
  </xsl:template>
  <!-- HIER CITY-DIV ERSTELLEN -->
  <!-- HIER CITY-H1 ERSTELLEN -->
  <!-- HIER TEMPERATURE-DIV ERSTELLEN -->
  <!-- die restlichen HTML-Elemente erstellen -->
  <xsl:template match="coord">
    <xsl:variable name="googleMapsUrl" select="concat('https://www.google.com/maps/embed/v1/place?q=',@lat,',',@lon,'&amp;key=AIzaSyAN0om9mFmy1QN6Wf54tXAowK4eT0ZUPrU')" />
    <iframe src="{$googleMapsUrl}" class="map" />
  </xsl:template>
  <xsl:template match="sun">
    <div class="sun">
      <p>Sonnenaufgang: <xsl:value-of select="hdm:formatDate(@rise)" /></p>
      <p>Sonnenuntergang: <xsl:value-of select="hdm:formatDate(@set)" /></p>
    </div>
  </xsl:template>
  <xsl:template match="lastupdate">
    <p class="lastUpdate">Letzte Aktualisierung: <xsl:value-of select="hdm:formatDate(@value)" /></p>
  </xsl:template>
  <!-- Hilfs-Funktionen -->
  <xsl:function name="hdm:kelvinToCelsius">
    <xsl:param name="kelvin" as="xs:float" required="yes" />
    <xsl:value-of select="round(($kelvin - 273.15) * 1) div 1" />
  </xsl:function>
  <xsl:function name="hdm:formatDate">
    <xsl:param name="date" as="xs:dateTime" required="yes" />
    <xsl:value-of select="format-dateTime($date, '[H]:[m] Uhr')" />
  </xsl:function>
</xsl:stylesheet>

Consejos

Obtener datos meteorológicos

Los datos meteorológicos pueden obtenerse mediante esta URL. El código postal indica la ciudad deseada: "http://api.openweathermap.org/data/2.5/weather?zip={PLZ},de&appid=2de143494c0b295cca9337e1e96b00e0&mode=xml".

Insertar un mapa de Google

Un elemento iframe permite insertar fácilmente un mapa de Google. La posición se indica mediante longitud y latitud.

<iframe src="https://www.google.com/maps/embed/v1/place?q={Breitengrad},{Längengrad}&key=AIzaSyAN0om9mFmy1QN6Wf54tXAowK4eT0ZUPrU" class="map"/>

Estilos

Incluya el archivo CSS siguiente: styles.css y utilice las clases indicadas en la salida requerida del ejercicio 6.

Funciones auxiliares

Para convertir kelvin a grados Celsius, utilice la función hdm:kelvinToCelsius.

Para dar formato a la hora, utilice la función hdm:formatDate.

Ampliación

Copie el archivo openWeather.xml del directorio de demostración al directorio in.

Adapte la transformación para mostrar primero el tiempo local y debajo el de las ciudades del archivo XML.

Solución

El HTML de salida completo y la hoja de estilo de solución se encuentran en Soluciones del ejercicio 6: informe meteorológico.

   

volver arriba