XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round two / Summary and exercises / Solutions to the exercises
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 2.)
The exercises corresponding to these solutions are in Exercises.
The following exercises use file buchhandel.xsl.
Exercise 1: Filtering and sorting
The essential step is filtering the node sequence with a predicate to exclude books published from 2000 onward. Sorting follows.
For clarity, the publication year is also output when formatting element <buchverlag>:
<xsl:template match="buchhandel">
<xsl:apply-templates select="buch[@ersch-jahr < 2000]">
<xsl:sort select="@ersch-jahr"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="buchverlag">
<br/><b>Verlag: </b><xsl:apply-templates/>,
<!-- Ausgabe Erscheinungsjahr -->
<xsl:value-of select="../@ersch-jahr"/>;
</xsl:template>
Code example: loesungen/02/buchhandel-a1.xsl (excerpt).
Output (excerpt):
Unter Korallen und Haien von Hass, Hans;
Verlag: Ozean Verlag, 1950; Preis: 22.95 Euro
(ISBN liegt nicht vor.)
Ganz oben von Hansen, Carl;
Verlag: Berg Verlag, 1960; Preis: 20.95 Euro
(ISBN liegt nicht vor.)
Code example: loesungen/02/buchhandel-a1.html.
Exercise 2: Filtering and sorting
The approach resembles exercise 1. Combine the two conditions with logical OR in the predicate. The result is their union. When sorting by author, limit the sort criterion to one node using a numeric predicate:
<xsl:template match="buchhandel">
<xsl:apply-templates select="buch[@ersch-jahr > 1998 or @kat='reisen']">
<xsl:sort select="buchautor[1]"/>
</xsl:apply-templates>
</xsl:template>
Code example: loesungen/02/buchhandel-a2.xsl (excerpt).
Output (excerpt):
Yet Another Hitchhiker's Guide to the Galaxy von Adams, Douglas;
Verlag: Spacetrotter Publishing, 2001; Preis: 17.00 Dollar
(ISBN: 3-899-77610-5)
Hitchhiker's Guide to the Galaxy von Adams, Douglas;
Verlag: Spacetrotter Publishing, 1997; Preis: 12.00 Dollar
(ISBN: 3-899-77615-4)
Code example: loesungen/02/buchhandel-a2.html.
Exercise 3a: Filtering, counting and summing
Again, the solution centers on a compound predicate with a logical operator. The same predicate constructs the processed node sequence and is used for counting, summing prices and calculating the average price:
<xsl:template match="buchhandel">
<p><b>Statistik:</b></p>
<p>Bücher in der Liste: <xsl:value-of select="fn:count( buch[buchverlag='Berg Verlag' or buchverlag='Ozean Verlag'] )"/></p>
<p>Gesamtpreis aller Bücher: <xsl:value-of select="format-number(fn:sum(buch[buchverlag='Berg Verlag' or buchverlag='Ozean Verlag']/preis),'##00.00')"/></p>
<p>Durchschnittspreis aller Bücher: <xsl:value-of select="format-number(fn:sum(buch[buchverlag='Berg Verlag' or buchverlag='Ozean Verlag']/preis) div fn:count(buch[buchverlag='Berg Verlag' or buchverlag='Ozean Verlag']),'##00.00')"/></p>
<xsl:apply-templates select="buch[buchverlag='Berg Verlag' or buchverlag='Ozean Verlag']"/>
</xsl:template>
Code example: loesungen/02/buchhandel-a3.xsl.
Output (excerpt):
Statistik:
Bücher in der Liste: 8
Gesamtpreis aller Bücher: 135.85
Durchschnittspreis aller Bücher: 16.98
Unter Korallen und Haien von Hass, Hans;
Verlag: Ozean Verlag, 1950; Preis: 22.95 Euro
(ISBN liegt nicht vor.)
Code example: loesungen/02/buchhandel-a3.html.
Exercise 3b: Filtering and calculating
Books are filtered by currency “Dollar” when constructing the node sequence. The predicate is a path from the current node to its child <preis> and that child’s attribute waehrung. Conversion and formatting occur directly when outputting the price, using a dollar/euro conversion factor of 0.68054.
The stylesheet must use a decimal point for this conversion factor, rather than the decimal comma shown in the German text!
<xsl:template match="buchhandel">
<p><b>Statistik:</b></p>
<!-- Bücher mit Dollarpreis zählen: -->
<p>Bücher in der Liste: <xsl:value-of select="fn:count(buch[preis/@waehrung='Dollar'])"/></p>
<!-- Bücher mit Dollarpreis ausgeben: -->
<xsl:apply-templates select="buch[preis/@waehrung='Dollar']"/>
</xsl:template>
<xsl:template match="preis">
Preis: <xsl:apply-templates/><xsl:text> </xsl:text><xsl:value-of select="@waehrung"/>
entspricht <xsl:value-of select="format-number(. * 0.68054, '##0.00')"/> Euro
</xsl:template>
Code example: loesungen/02/buchhandel-a3b.xsl.
Output (excerpt):
Statistik:
Bücher in der Liste: 5
Where Wizards Stay Up Late von Hafner, Katie; Lyon, Matthew;
Verlag: Touchstone, 1996; Preis: 14.00 Dollar entspricht 9.52 Euro
(ISBN: 3-899-77609-7)
Yet Another Hitchhiker's Guide to the Galaxy von Adams, Douglas;
Verlag: Spacetrotter Publishing, 2001; Preis: 17.00 Dollar entspricht 11.56 Euro
(ISBN: 3-899-77610-5)
Code example: loesungen/02/buchhandel-a3b.html.
Exercise 4: Conditions
The node sequence is sorted with xsl:sort using the title as the sort criterion. Notes are inserted with xsl:choose, using two elements xsl:when with suitable conditions, but no xsl:otherwise.
<xsl:template match="buchhandel">
<p><b>Bücher nach Titel:</b></p>
<xsl:apply-templates><xsl:sort select="buchtitel"/></xsl:apply-templates>
</xsl:template>
<xsl:template match="buch">
<p><!-- neu oder vergriffen? -->
<xsl:choose>
<xsl:when test="@ersch-jahr > 2003">
<b>Neu erschienen:</b><br/>
</xsl:when>
<xsl:when test="@ersch-jahr < 1975">
<i>Vergriffen!</i><br/>
</xsl:when>
</xsl:choose>
<!-- jetzt die Buchdaten ausgeben: -->
<xsl:apply-templates/>
<!-- liegt ISBN-Nummer vor? -->
<xsl:choose>
<xsl:when test="@isbn != ''">
<br/> (ISBN: <xsl:value-of select="@isbn"/>)
</xsl:when>
<xsl:otherwise>
<br/> (ISBN-Nummer liegt nicht vor.)
</xsl:otherwise>
</xsl:choose>
</p>
</xsl:template>
Code example: loesungen/02/buchhandel-a4.xsl.
Output (excerpt):
Bücher nach Titel:
Vergriffen!
Ganz oben von Hansen, Carl;
Verlag: Berg Verlag, 1960; Preis: 20.95 Euro
(ISBN liegt nicht vor.)
Neu erschienen:
Irreguläre Ausdrücke von Esser, Klaus;
Verlag: Java Verlag, 2004; Preis: 27.00 Euro
(ISBN liegt nicht vor.)
Code example: loesungen/02/buchhandel-a4.html.
Exercise 5: Grouping and numbering
Grouping is simple with xsl:for-each-group, using the publication year as current-grouping-key() and outputting the titles as a sequence current-group() using xsl:for-each (loop ). Numbering is indirect through an HTML list <ol>. Alternatively, position() could be used, but not xsl:number unless it has attribute value, since book titles are not output in document order:
<xsl:template match="/">
<html>
<head>
<title>XSLT 2.0 und XPath 2.0: Gruppieren</title>
</head>
<body>
<h1>Bücher nach Erscheinungsjahr gruppiert</h1>
<!-- Bildung der Gruppen: -->
<xsl:for-each-group select="buchhandel/buch" group-by="@ersch-jahr">
<xsl:sort select="@ersch-jahr"/>
<h3>Erscheinungsjahr: <xsl:value-of select="current-grouping-key()"/></h3>
<ol>
<!-- Ausgabe der Einzeltitel: -->
<xsl:for-each select="current-group()/buchtitel">
<li>
<xsl:value-of select="."/>
</li>
</xsl:for-each>
</ol>
</xsl:for-each-group>
</body>
</html>
</xsl:template>
Code example: loesungen/02/buchhandel-a5.xsl.
Output (excerpt):
Erscheinungsjahr: 2003
1. Java für Theoretiker Bd. 1
2. Reguläre Ausdrücke
3. Java für Theoretiker Bd. 2
Code example: loesungen/02/buchhandel-a5.html.
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