XML technologies / XSLT / XSLT 3.0 / XSLT streaming / Elements / xsl:iterate / Stuttgart temperature curve, 2015
Exercise 9
Write an XSLT transformation that converts the input file WetterdatenStuttgart.xml into a CSV file. See the following figure:
Figure: Required output for the Stuttgart temperature curve, 2015.
A table showing minimum and maximum temperatures for each month of 2015.
Use the transformation scenario [Übung 9] xsl:iterate - Temperaturkurve Stuttgart 2015 (XML >> CSV) (see the notes on the exercises) and the following XSLT file. Iterate over the months to select each month’s minimum and maximum temperatures.
Code example: Stylesheet for exercise 9, Stuttgart temperature curve, 2015.
<?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:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-prefixes="xs math" version="3.0">
<!--AUFGABE: Erstellen Sie eine CSV-Datei, die als Tabelle für jeden Monat die min- und max-Temperatur enthält. Nutzen Sie hierfür xsl:iterate. -->
<!--ERWEITERUNG: Geben Sie zusätzlich für jeden Monat die Summe der Niederschläge aus den vorangegangenen Monaten + des aktuellen Monats aus. -->
<xsl:mode streamable="yes" />
<xsl:output method="text" />
<xsl:template match="year">
<!-- Columns header -->
<!-- für ERWEITERUNG auskommentieren -->
<xsl:text>month; min temp; max temp
</xsl:text>
<!-- für ERWEITERUNG einkommentieren -->
<!-- <xsl:text>month; min temp; max temp; rainSum
</xsl:text> -->
<!-- TODO: Beginn xsl:iterate -->
<!-- TODO: Ausgabe der Werte; jeweils durch ein Semikolon zu trennen -->
<!-- Zeilenumbruch -->
<!-- TODO: Ende xsl:iterate -->
</xsl:template>
</xsl:stylesheet>
Extension
In addition to the temperatures, a further column should now show cumulative precipitation for the preceding months and the current month.
Tip
Calculating sums
Use the xsl:next-iteration construct to assign a specific value to a parameter for the next iteration.
Variant
Create a new table for Stuttgart in 2015 showing average temperatures (min and max) and total precipitation. See the following figure:
Figure: Required output for the Stuttgart temperature curve, 2015 (variant).
A table of average minimum and maximum temperatures and total precipitation for 2015.
Use the transformation scenario [Übung 9 - Variante] xsl:iterate - Durchschnitt Stuttgart (XML >> CSV) and the following XSLT file:
Code example: Stylesheet for exercise 9, Stuttgart temperature curve, 2015 (variant).
<?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:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-prefixes="xs math" version="3.0">
<!-- AUFGABE: Geben Sie in einer Tabelle die min- und max-Durchschnittstemperaturen für das gesamte Jahr 2015 sowie die Gesamtniederschlagsmenge an. Nutzen Sie hierfür xsl:iterate, xsl:next-iteration und xsl:on-completion. -->
<xsl:mode streamable="yes"/>
<xsl:output method="text"/>
<xsl:template match="year">
<!-- Columns header -->
<xsl:text>year; avg. min temp; avg max temp; rainSum
</xsl:text>
<!--TODO: xsl:iterate mit on-completion und next-iteration anlegen. -->
</xsl:template>
</xsl:stylesheet>Tip
Totals only
Use the xsl:on-completion construct to output totals once after the final iteration.
Solution
The complete output CSV and solution stylesheets for the main exercise and its variant are available under Solutions to Exercise 9: Stuttgart temperature curve, 2015.