XML technologies / XSLT / XSLT 3.0 / XSLT streaming / Elements / xsl:stream / Weather report
Exercise 6
Write an XSLT transformation that retrieves current weather data from openweathermap.org for a given postal code using xsl:stream and presents them in an HTML weather report.
Since there is no local input file, use / as the value of the match attribute for the initial template. The transformation can then be applied to the XSLT.
Code example: Required output for Exercise 6 (excerpt).
<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>Transformed weather report in HTML.
Use the transformation scenario [Übung 6] xsl:stream - Wetterbericht (XSL >> HTML) (see the notes on the exercises) and the following XSLT file:
Code example: Stylesheet for Exercise 6: Weather report.
<?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&appid=2de143494c0b295cca9337e1e96b00e0&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&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,'&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>
Tips
Retrieving weather data
Weather data can be retrieved through this URL. The postal code specifies the city: "http://api.openweathermap.org/data/2.5/weather?zip={PLZ},de&appid=2de143494c0b295cca9337e1e96b00e0&mode=xml".
Embedding a Google map
Using an iframe makes embedding a Google map easy. Longitude and latitude specify the location.
<iframe src="https://www.google.com/maps/embed/v1/place?q={Breitengrad},{Längengrad}&key=AIzaSyAN0om9mFmy1QN6Wf54tXAowK4eT0ZUPrU" class="map"/>
Styling
Include the CSS file styles.css and use the classes specified in the required output for Exercise 6.
Helper functions
To convert Kelvin to Celsius, use the function hdm:kelvinToCelsius.
To format the time, use the function hdm:formatDate.
Extension
Copy the file openWeather.xml from the demonstration-data folder to the in folder.
Adapt the transformation to show local weather first, followed by weather for the cities in the XML file.
Solution
The complete output HTML and the solution stylesheet can be found under Solutions to Exercise 6: weather report.