XML technologies / XSLT / XSLT 3.0 / XSLT streaming / First steps with XSLT streaming / xsl:mode on-no-match / on-no-match="fail"

Exercise 1

Use on-no-match="fail", to translate all elements in the XML file openWeather.xml from English to German. The following XML file should be produced as output:

Code example: required output for exercise 1 (excerpt).

<wetterdaten>
  <element>
    <stadt id="2950159" name="Berlin">
      <koordinaten lon="13.41" lat="52.52"/>
      <Land>DE</Land>
      <sonne rise="2015-12-08T07:03:45" set="2015-12-08T14:52:40"/>
    </stadt>
    <temperatur value="284.15" min="284.15" max="284.15" unit="kelvin"/>
    <luftfeuchtigkeit value="76" unit="%"/>
    <luftdruck value="1031" unit="hPa"/>
    <wind>
      <Geschwindigkeit value="3.1" name="Light breeze"/>
      <boeen/>
      <richtung value="150" code="SSE" name="South-southeast"/>
    </wind>
    <wolken value="0" name="clear sky"/>
    <sicht/>
    <faellung mode="no"/>
    <wetter number="800" value="Sky is Clear" icon="01d"/>
    <zeitpunkt value="2015-12-08T11:20:00"/>
  </element>
  ...
</wetterdaten>

Output after applying the transformation to translate elements from English to German.

Use the transformation scenario [Übung 1] on-no-match="fail" (XML >> XML) (see the notes on the exercises) with the following XSLT file:

Code example: stylesheet for exercise 1 on xsl:mode on-no-match="fail".

<?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: Nutzen Sie on-no-match="fail", um alle Elemente der XML-Datei vom Englischen ins Deutsche zu übersetzen. Da die XML-Datei sehr groß ist, wurde für einige Elemente bereits ein Default-Template angelegt, sodass nur noch für die unten genannten Elemente ein Template erzeugt werden muss.--> 
  <!--TODO: Streaming-Modus definieren und on-no-match angeben.-->
  <!--Rootelement-->
  <xsl:template match="/">
    <xsl:apply-templates />
  </xsl:template>
  <xsl:template match="text()">
    <xsl:copy-of select="." />
  </xsl:template>
  <!--Beispiel-->
  <xsl:template match="cityWeather">
    <wetterdaten>
      <xsl:copy-of select="@*" />
      <xsl:apply-templates />
    </wetterdaten>
  </xsl:template>
  <!--TODO: Templates für current, city, coord, country und sun anlegen.-->
  <!--Default-Template für alle restlichen Knoten.-->
  <xsl:template match="temperature | humidity | pressure | wind | speed | gusts | direction | clouds | visibility | precipitation | weather | lastupdate" />
</xsl:stylesheet>

Extension

Delete the template for city from your stylesheet and apply the transformation scenario [Übung 1] on-no-match="fail" (XML >> XML) again to the XML file openWeather.xml.

What happens? And why?

Why is it not possible to translate the attributes as well as the elements?

Tips

Template for the root element

Remember to create a corresponding template for the root element as well: <xsl:template match="/">.

Template for text nodes

Note that a template must also be defined for text nodes (text()).

Solution

The complete output XML and the solution stylesheet can be found under Solutions to Exercise 1 — xsl:mode on-no-match="fail".

   

back to top