XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – getting started / Summary and exercises / Exercise solutions

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 1.)

The corresponding questions appear under Exercises.

Exercise 1a: Outputting a string value:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3.org/1999/xhtml">
  <xsl:output method="xhtml" encoding="ISO-8859-1" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" />
  <xsl:template match="/">
    <html>
      <head>
        <title>XSLT 2.0 und XPath 2.0: Stringwert 1a)</title>
      </head>
      <body>
        <h1>Memo:</h1>
        <p><xsl:value-of select="memo"/></p>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Code example: loesungen/01/memo1.xsl.

The template rule uses match="/" to match the document node. Starting there as the current node, the document content’s string value can be read inside a literal result <p> container using <xsl:value-of select="memo"/>.

Alternatively, set the template rule to match="memo". Then the built-in document rule handles the document node and invokes the explicit template. Since <memo> is now current, extract the string with <xsl:value-of select="."/>.

Exercise 1b: Outputting a string value, alternative version

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3.org/1999/xhtml">
  <!-- Output-Deklaration siehe 1.a) -->
  <xsl:template match="/">
    <html>
      <head>
        <title>XSLT 2.0 und XPath 2.0: Stringwert 1b)</title>
      </head>
      <body>
        <h1>Memo:</h1>
        <p><xsl:apply-templates/></p>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Code example: loesungen/01/memo1b.xsl.

The only difference is using <xsl:apply-templates/> instead of <xsl:value-of select="memo"/>. Rather than directly extracting a string, this invokes a cascade of built-in rules. With no explicit template rules for any nodes, they copy all source text nodes, including whitespace nodes, into the result.

Exercise 2a: Outputting individual elements’ string values

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3.org/1999/xhtml">
  <!-- Output-Deklaration siehe 1.a) -->
  <xsl:template match="/">
    <html>
      <head>
        <title>XSLT 2.0 und XPath 2.0: Stringwert 2a)</title>
      </head>
      <body>
        <h1>Memo:</h1>
        <p><xsl:value-of select="memo/an"/></p>
        <p><xsl:value-of select="memo/von"/></p>
        <p><xsl:value-of select="memo/betreff"/></p>
        <p><xsl:value-of select="memo/text"/></p>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Code example: loesungen/01/memo2.xsl.

Separate containers require several instructions. Account for the path from the current node to each element; here it goes from the document node through the root element.

With the template rule set to match="memo", <memo> becomes current, so each child name suffices: <xsl:value-of select="an"/> etc.

Exercise 2b: Controlling order

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3.org/1999/xhtml">
  <!-- Output-Deklaration siehe 1.a) -->
  <xsl:template match="/">
    <html>
      <head>
        <title>XSLT 2.0 und XPath 2.0: Reihenfolge 2b)</title>
      </head>
      <body>
        <h1>Memo:</h1>
        <p><b>Von: </b><xsl:value-of select="memo/von"/></p>
        <p><b>An: </b><xsl:value-of select="memo/an"/></p>
        <p><b>Betreff: </b><xsl:value-of select="memo/betreff"/></p>
        <p><xsl:value-of select="memo/text"/></p>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Code example: loesungen/01/memo2b.xsl.

Instruction xsl:value-of accesses the source directly, so changing output order only requires moving instructions in the template. What matters is the information’s hierarchical position relative to the current node, defining the required XPath expression, rather than its source document order.

Exercise 2c: Controlling order, alternative version

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3.org/1999/xhtml">
  <!-- Output-Deklaration siehe 1.a) -->
  <xsl:template match="/">
    <html>
      <head>
        <title>XSLT 2.0 und XPath 2.0: Reihenfolge 2c)</title>
      </head>
      <body>
        <h1>Memo:</h1>
        <p><b>Von: </b><xsl:apply-templates select="memo/von"/></p>
        <p><b>An: </b><xsl:apply-templates select="memo/an"/></p>
        <p><b>Betreff: </b><xsl:apply-templates select="memo/betreff"/></p>
        <p><xsl:apply-templates select="memo/text"/></p>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Code example: loesungen/01/memo2c.xsl.

Achieving this with xsl:apply-templates takes similar effort. Instruction positions again determine output order. Values are not directly extracted: each selected element forms a single-node sequence, then the corresponding built-in rule processes it.

Exercise 3: Reusing content

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3.org/1999/xhtml">
  <xsl:output method="xhtml" encoding="ISO-8859-1" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" />
  <xsl:template match="/">
    <html>
      <head>
        <title>
          <xsl:text>Memo von </xsl:text>
          <xsl:value-of select="memo/von"/>
          <xsl:text> an </xsl:text>
          <xsl:value-of select="memo/an"/>
        </title>
      </head>
      <body>
        <h1>Memo:</h1>
        <p><b>Von: </b><xsl:apply-templates select="memo/von"/></p>
        <p><b>An: </b><xsl:apply-templates select="memo/an"/></p>
        <p><b>Betreff: </b><xsl:apply-templates select="memo/betreff"/></p>
        <p><xsl:apply-templates select="memo/text"/></p>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Code example: loesungen/01/memo3.xsl.

HTML head output uses xsl:value-of because only strings are needed there. Body output is unchanged from the preceding solution.

Exercise 4a: Multiple template rules

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3.org/1999/xhtml">
  <xsl:output method="xhtml" encoding="ISO-8859-1" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" />
  <xsl:template match="/">
    <html>
      <head>
        <title>
          <xsl:text>Memo von </xsl:text>
          <xsl:value-of select="memo/von"/>
          <xsl:text> an </xsl:text>
          <xsl:value-of select="memo/an"/>
        </title>
      </head>
      <body>
        <h1>Memo:</h1>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="an">
    <p><b>An: </b><xsl:apply-templates/></p>
  </xsl:template>
  <xsl:template match="von">
    <p><b>Von: </b><xsl:apply-templates/></p>
  </xsl:template>
  <xsl:template match="betreff">
    <p><b>Betreff: </b><xsl:apply-templates/></p>
  </xsl:template>
  <xsl:template match="text">
    <p><xsl:apply-templates/></p>
  </xsl:template>
</xsl:stylesheet>

Code example: loesungen/01/memo4.xsl.

Each output element has its own template rule containing the required literal result elements and an xsl:apply-templates instruction.

Since document order is suitable, the document-node template uses an xsl:apply-templates instruction, as in 1b. It selects a sequence containing element <memo>. Its built-in element rule then builds the sequence activating the remaining templates.

Exercise 4b: Multiple rules, controlling order

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3.org/1999/xhtml">
  <xsl:output method="xhtml" encoding="ISO-8859-1" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" />
  <xsl:template match="/">
    <html>
      <title>
        <xsl:text>Memo von </xsl:text>
        <xsl:value-of select="memo/von"/>
        <xsl:text> an </xsl:text>
        <xsl:value-of select="memo/an"/>
      </title>
      <body>
        <h1>Memo:</h1>
        <xsl:apply-templates select="memo/von"/>
        <xsl:apply-templates select="memo/an"/>
        <xsl:apply-templates select="memo/betreff"/>
        <xsl:apply-templates select="memo/text"/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="an">
    <p><b>An: </b><xsl:apply-temp¬lates/></p>
  </xsl:template>
  <xsl:template match="von">
    <p><b>Von: </b><xsl:apply-tem¬plates/></p>
  </xsl:template>
  <xsl:template match="betreff">
    <p><b>Betreff: </b><xsl:apply-templates/></p>
  </xsl:template>
  <xsl:template match="text">
    <p><xsl:apply-templa¬tes/></p>
  </xsl:template>
</xsl:stylesheet>

Code example: loesungen/01/memo4b.xsl.

Changing order requires specific sequences in the desired order, like solution 2c, but here each element has an explicit rule.

Exercise 5: Reading attributes, HTML output

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <!-- Beachten Sie das Fehlen des Default-Namensraums! -->
  <!-- KEINE Output-Deklaration, d.h. HTML-Erzeugung -->
  <xsl:template match="/">
    <html>
      <head>
        <title>XSLT 2.0 und XPath 2.0: Buchdatei 5)</title>
      </head>
      <body>
        <h2><xsl:value-of select="buch/@autor"/><xsl:text>: "</xsl:text><xsl:value-of select="buch/@titel"/><xsl:text>"</xsl:text></h2>
        <p><xsl:text>in der Übersetzung von </xsl:text><i><xsl:value-of select="buch/@übersetzer"/></i></p>
        <p><xsl:value-of select="buch/@verlag"/><xsl:text>, </xsl:text><xsl:value-of select="buch/@ersch-jahr"/></p>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Code example: loesungen/01/buch-attrib-html.xsl.

Reading attributes resembles reading element string values, except that the location step must switch to the attribute axis. The result includes the usual HTML content meta tag. Umlauts use entities and encoding UTF-8.

Result:

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>XSLT 2.0 und XPath 2.0: Buchdatei 5)</title>
  </head>
  <body>
    <h2>Mike Miller: "XML – Das verflixte Attribut"</h2>
    <p>in der &Uuml;bersetzung von <i>Peter Panter</i></p>
    <p>Galileo Verlag, 2010</p>
  </body>
</html>

Code example: loesungen/01/buch-attrib-html.html.

Exercise 6a: Using xsl:output, method="xhtml"

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3.org/1999/xhtml">
  <!-- Default-Namensraum für XHTML gesetzt -->
  <!-- Output-Steuerung XHTML: -->
  <xsl:output method="xhtml" encoding="ISO-8859-1" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" />
  <xsl:template match="/">
    <html>
      <head>
        <title>XSLT 2.0 und XPath 2.0: Buchdatei 6a)</title>
      </head>
      <body>
        <h2><xsl:value-of select="buch/@autor"/><xsl:text>: "</xsl:text><xsl:value-of select="buch/@titel"/><xsl:text>"</xsl:text></h2>
        <p><xsl:text>in der Übersetzung von </xsl:text><i><xsl:value-of select="buch/@übersetzer"/></i></p>
        <p><xsl:value-of select="buch/@verlag"/><xsl:text>, </xsl:text><xsl:value-of select="buch/@ersch-jahr"/></p>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Code example: loesungen/01/buch-attrib-xhtml.xsl.

With the appropriate xsl:output, XHTML version 6a differs from solution 5: an XML prolog, a DOCTYPE declaration, the XHTML namespace on the root and no HTML entities for umlauts. The content meta tag is included too, but specifies encoding ISO-8859-1.

Result:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/xhtml; charset=ISO-8859-1"/>
    <title>XSLT 2.0 und XPath 2.0: Buchdatei 6a)</title>
  </head>
  <body>
    <h2>Mike Miller: "XML – Das verflixte Attribut"</h2>
    <p>in der Übersetzung von <i>Peter Panter</i></p>
    <p>Galileo Verlag, 2010</p>
  </body>
</html>

Code example: loesungen/01/buch-attrib-xhtml.html.

Exercise 6b: Using xsl:output, method="xml"

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <!-- Beachten Sie das Fehlen des Default-Namensraums! -->
  <!-- Output-Steuerung XML: -->
  <xsl:output method="xml" encoding="ISO-8859-1" indent="yes"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>XSLT 2.0 und XPath 2.0: Buchdatei 6b)</title>
      </head>
      <body>
        <h2><xsl:value-of select="buch/@autor"/><xsl:text>: "</xsl:text><xsl:value-of select="buch/@titel"/><xsl:text>"</xsl:text></h2>
        <p><xsl:text>in der Übersetzung von </xsl:text><i><xsl:value-of select="buch/@übersetzer"/></i></p>
        <p><xsl:value-of select="buch/@verlag"/><xsl:text>, </xsl:text><xsl:value-of select="buch/@ersch-jahr"/></p>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Code example: loesungen/01/buch-attrib-xml.xsl.

With output method method="xml", the XHTML namespace may be omitted but need not be. Retaining it indirectly produces XHTML without a doctype. Umlauts are not escaped with HTML entities here either.

Result:

<?xml version="1.0" encoding="ISO-8859-1"?>
<html>
  <head>
    <title>XSLT 2.0 und XPath 2.0: Buchdatei 6b)</title>
  </head>
  <body>
    <h2>Mike Miller: "XML – Das verflixte Attribut"</h2>
    <p>in der Übersetzung von <i>Peter Panter</i></p>
    <p>Galileo Verlag, 2010</p>
  </body>
</html>

Code example: loesungen/01/buch-attrib-xml.html.

Without the XHTML namespace or automatically inserted HTML/XHTML content-type meta tag, this is, for the XSLT processor, just XML using HTML tag names.

Exercise 6c: Using xsl:output, method="text"

Processing with the same stylesheet but an output declaration specifying "text":

...
  <xsl:output method="text" encoding="ISO-8859-1" indent="yes"/>
  ...

Code example: loesungen/01/buch-attrib-text.xsl (excerpt).

… produces plain text from the string content of the source and literal result elements. Attribute indent is ignored and all HTML tags are discarded.

Result:

BuchdateiMike Miller: "XML – Das verflixte Attribut" 
in der Übersetzung von Peter Panter, Galileo Verlag, 2010

Code example: loesungen/01/buch-attrib-text.txt.

Conversely, the encoding attribute is not ignored, as omitting it demonstrates. Also remove the xsl:text instructions. The otherwise deliberately removed line breaks now appear in the result:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <!-- Output-Steuerung Text: -->
  <xsl:output method="text"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>XSLT 2.0 und XPath 2.0: Buchdatei 6c)</title>
      </head>
      <body>
        <h2><xsl:value-of select="buch/@autor"/>:
        "<xsl:value-of select="buch/@titel"/>"
        </h2>
        <p>in der Übersetzung von 
        <i><xsl:value-of select="buch/@übersetzer"/></i></p>
        <p><xsl:value-of select="buch/@verlag"/>,
        <xsl:value-of select="buch/@ersch-jahr"/></p>
      </body>
    </html>
  </xsl:template> 
</xsl:stylesheet>

Code example: loesungen/01/buch-attrib-text-b.xsl.

The text file now looks as follows. Note the effect of missing encoding on umlauts:

Result:

BuchdateiMike Miller: 
"XML – Das verflixte Attribut"
in der Übersetzung von 
Peter PanterGalileo Verlag,
2010

Code example: loesungen/01/buch-attrib-text-b.txt.

   

back to top