XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – getting started / Summary and exercises / Exercises
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 1.)
For these exercises, use AltovaXML or Saxon 9 as the XSLT processor (or its GUI Kernow). Installation and operation of the processors are described in XSLT 2.0 processors.
The solutions are in Solutions to the exercises.
The source material is a simple XML document containing a short text message, rather like an email. (Note: Peter Panter and Theobald Tiger were pseudonyms used by Kurt Tucholsky as a critic.)
<?xml version="1.0" encoding="ISO-8859-1"?>
<memo>
<an>Peter Panter</an>
<von>Theobald Tiger</von>
<betreff>Neues Buch von Tucholsky</betreff>
<text>Hallo Peter! Hast Du schon das neue Buch von Tucholsky gelesen? Gruss, Theo!</text>
</memo>
Code example: aufgaben/kap01/memo1.xml.
An unfinished stylesheet is available for processing; complete it:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template ...>
<html>
<body>
<h1>Memo:</h1>
...
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Code example: aufgaben/kap01/memo.xsl.
Exercise 1a: Outputting a string value
Add to the template rule a suitablematchattribute. Complete the stylesheet so that the source document’s string value appears after the heading in a<p>container. Use instructionxsl:value-of.Exercise 2a: Outputting individual element string values
Extend the stylesheet from 1) so that each element’s content appears in its own<p>container in the result document. Remember that XPath expressions represent paths from the current node to the target node!Exercise 2b: Controlling order
Write a variant of 2) that outputs the content of<von>before that of<an>. Also output the bold strings Von: and An: and Betreff: before the corresponding content.Exercise 2c: Controlling order: alternative version
Solve exercise 2a) based on 1a), usingxsl:apply-templates.Exercise 3: Reusing content
An HTML page should have a document title. It should say “Memo von (Absendername) an (Empfängername)”. Any problem?Exercise 4a: Multiple template rules
Extend the stylesheet with multiple template rules invoked byxsl:apply-templates. Set attributematchaccording to the element being processed. Retain the document’s output order.Exercise 4b: Multiple template rules: controlling order
Try to obtain the same result as in 2a).Exercise 5: Reading attributes
Use this source document:
<?xml version="1.0" encoding="ISO-8859-1"?>
<buch titel="XML – Das verflixte Attribut" autor="Mike Müller" uebersetzer="Peter Panter" verlag="Galileo Computing" isbn="0 815 12345 0" ersch-jahr="2010"/>
Code example: aufgaben/kap01/buch-attrib.xml.
Create a stylesheet containing a template matching the document node. Put the basic HTML structure inside it. Add xsl:value-of instructions to read the attributes. Try to obtain formatting roughly as follows:Mike Miller: »XML – Das verflixte Attribut«
in der Übersetzung von Peter Panter
Galileo Computing, 2010
Exercise 6a: Using
xsl:output
Instead of the HTML document generated in exercise 5, output an XHTML document. Usexsl:outputwith appropriateencodingandindentattributes. Set the XHTML namespace on the stylesheet element and, alternatively, on the<html>root element; compare the results. Try omitting attributeindentor setting it toindent="no".Exercise 6b: Using
xsl:output
Try usingmethod="xml". How does the result differ from the previous output? Also trymethod="text".
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn