Lösungen zu Übung 6: Wetterbericht (xsl:stream)

Im Folgenden finden Sie die Lösungsdateien zu Übung 6 unter Wetterbericht.

Code-Beispiel: Lösungs-XSLT zu Übung 6.

<?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">
  <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>
        <xsl:stream href="http://api.openweathermap.org/data/2.5/weather?zip=72655,de&amp;appid=2de143494c0b295cca9337e1e96b00e0&amp;mode=xml">
          <xsl:apply-templates select="node()" />
        </xsl:stream>
        <!--Für Erweiterung einkommentieren. -->
        <!--<xsl:stream href="../in/openWeather.xml">
              <xsl:apply-templates select="node()"/>
            </xsl:stream>
         -->
      </body>
    </html>
  </xsl:template>
  <!-- HTML-Elemente erstellen -->
  <xsl:template match="current">
    <div class="city">
      <xsl:apply-templates select="*" />
    </div>
  </xsl:template>
  <xsl:template match="city">
    <h1>
      <xsl:value-of select="@name" />
    </h1>
    <xsl:apply-templates select="*" />
  </xsl:template>
  <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="temperature">
    <div class="temperature">
      <p>Aktuelle Temperatur: <xsl:value-of select="hdm:kelvinToCelsius(@value)" /> °C</p>
      <p>Min Temperatur: <xsl:value-of select="hdm:kelvinToCelsius(@min)" /> °C</p>
      <p>Max Temperatur: <xsl:value-of select="hdm:kelvinToCelsius(@max)" /> °C</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>

Code-Beispiel: Vollständiges Output-HTML zu Übung 6.

<?xml version="1.0" encoding="UTF-8"?>
<html>
  <head>
    <title>Wetter</title>
    <a target="rel=&quot;stylesheet&quot;" href="http://href=&quot;../styling/styles.css&quot;"></a>
  </head>
  <body>
    <div class="city">
      <h1>Heslach</h1>
      <iframe src="https://www.google.com/maps/embed/v1/place?q=48.76,9.15&amp;key=AIzaSyAN0om9mFmy1QN6Wf54tXAowK4eT0ZUPrU" class="map"></iframe>
      <div class="sun">
        <p>Sonnenaufgang: 7:15 Uhr</p>
        <p>Sonnenuntergang: 15:43 Uhr</p>
      </div>
      <div class="temperature">
        <p>Aktuelle Temperatur: 3 °C</p>
        <p>Min Temperatur: 1 °C</p>
        <p>Max Temperatur: 6 °C</p>
      </div>
      <p class="lastUpdate">Letzte Aktualisierung: 22:43 Uhr</p>
    </div>
  </body>
</html>
Tipp der data2type-Redaktion:
Zum Thema XSLT bieten wir auch folgende Schulungen zur Vertiefung und professionellen Fortbildung an: