XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round three / Summary and exercises / Solutions to the exercises
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 3.)
The questions for these solutions are under Exercises.
Exercise 1: Named templates
The solution consists of a named template called through xsl:call-template after the chapter’s contents have been assembled and processed. The named template is entirely static and always produces the same output.
<xsl:template match="kapitel">
<xsl:apply-templates/>
<xsl:call-template name="kapitelende"/>
</xsl:template>
<xsl:template name="kapitelende">
<div align="center">*** Ende des Kapitels ***</div>
</xsl:template>
Output result (excerpt):
… Here, however, the template called by name is pulled directly into processing, the pull approach.
*** Ende des Kapitels ***
Code example: loesungen/03/a1-artikel.html.
Exercise 2: Variables and parameters
Each chapter’s title is read and passed as a parameter to the named template that writes the separator. The destination parameter has a default value as a fallback.
<xsl:template match="kapitel">
<xsl:apply-templates/>
<xsl:call-template name="kapitelende">
<xsl:with-param name="text" select="titel"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="kapitelende">
<xsl:param name="text">diesem Kapitel</xsl:param>
<div align="center">*** Ende von <xsl:value-of select="$text"/> ***</div>
</xsl:template>
Code example: loesungen/03/a2-artikel.xsl.
Output result (excerpt):
… Here, however, the template called by name is pulled directly into processing, the pull approach.
*** Ende von Kapitel 1 ***
Code example: loesungen/03/a2-artikel.html.
Exercise 3: Runtime identifiers
Using generate-id(), a table of contents with jump links is prepended to an XML article’s output.
<xsl:template match="/">
<html>
<head>
<title>XSLT 2.0 und XPath 2.0: generate-id</title>
</head>
<body>
<h1>Inhaltsverzeichnis mit generate-id()</h1>
<ul>
<xsl:for-each select="//titel">
<li>
<a href="#{generate-id()}">
<xsl:value-of select="."/>
</a>
</li>
</xsl:for-each>
</ul>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="kapitel">
<h3 id="{generate-id(titel)}">
<xsl:value-of select="titel"/>
</h3>
<p>
<xsl:value-of select="text"/>
</p>
<p>
<a href="#">nach oben</a>
</p>
<hr width="60%" noshade="noshade" size="1"/>
</xsl:template>
Code example: loesungen/03/a4-generate-id.xsl.
Exercise 4: Unparsed entities
Graphics declared in the DTD as unparsed entities are included in the corresponding template with function unparsed-entity-uri(). To give the image the desired attribute values, a declared attribute set is addressed using xsl:attribute-set and included in Literal Result Element <img> through its attribute xsl:use-attribute-sets.
Note that use-attribute-sets is given a prefix xsl here because it is used within a Literal Result Element and is in the XSLT namespace. The Literal Result Element must not be in the XSLT namespace.
<xsl:attribute-set name="bild-att">
<xsl:attribute name="hspace">10</xsl:attribute>
<xsl:attribute name="vspace">0</xsl:attribute>
</xsl:attribute-set>
<xsl:template match="buch">
<xsl:apply-templates select="bild"/>
...
</xsl:template>
<xsl:template match="bild">
<img src="{unparsed-entity-uri(@quelle)}" xsl:use-attribute-sets="bild-att"/>
</xsl:template>
Code example: loesungen/03/a4-buchdaten.xsl.
Exercise 5: Unparsed entities
The template for <buch> elements is extended to output an HTML <img> whose width is set through an attribute value template to the value read from element <verkauf>, a child of the current node.
<xsl:template match="buch">
<xsl:apply-templates select="bild"/>
...
<p><img src="rot.gif" height="5" width="{verkauf}"/> Verkaufte Exemplare: <xsl:value-of select="verkauf"/></p>
<hr/>
</xsl:template>
Code example: loesungen/03/a4-buchdaten.xsl.
Exercise 6: The XSLT function document()
The external document is stored in a global variable using XSLT function document(). The book template references this variable and selects and outputs the matching short review for the current title from the external document.
<!-- globale Variable für externes Dokument: -->
<xsl:variable name="inhalt" select="document('a6-buchdaten-inhalte.xml')"/>
<!-- hier Elemente "buch" verarbeiten: -->
<xsl:template match="buch">
<xsl:apply-templates select="bild"/>
...
<!-- hier ist die Inhaltsangabe eingebunden: -->
<xsl:variable name="der_titel" select="buchtitel"/>
<p><xsl:apply-templates select="$inhalt//inhalt[@titel=$der_titel]"/></p>
<!-- hier Breite der Verkaufsgrafik formatieren: -->
<p><img src="rot.gif" height="5" width="{verkauf}"/>Verkaufte Exemplare: <xsl:value-of select="verkauf"/></p>
<hr/>
</xsl:template>
<!-- hier Elemente "inhalt" verarbeiten: -->
<xsl:template match="inhalt">
<p><xsl:value-of select="."/></p>
</xsl:template>
Code example: loesungen/03/a4-buchdaten.xsl.
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