6. Extensible Style Language


  1. Extensible Style Language (XSL)
  2. XSL example
  3. XSL rules
  4. XSL Formatting
  5. Ways of Presenting XML
  6. XSLT Program Skeleton
  7. Data Model - example document
  8. Data Model - example tree
  9. Data Model Description
  10. XSLT Processing Model
  11. RSS Example
  12. Example of an XSLT Rule
  13. Another Example of an XSLT Rule
  14. Example: RSS headlines
  15. Saxon and XT
  16. Using a stylesheet processing instruction
  17. Example: RSS descriptions
  18. Example: RSS headlines (again)
  19. Some XPath expressions
  20. XPath expressions
  21. Relative expressions
  22. Simple subset of XPath
  23. Example: using XPath (1)
  24. Example: using XPath (2)
  25. Built-in template rules (1)
  26. Built-in template rules (2)
  27. More XPath examples
  28. Predicates
  29. Node-Set Functions
  30. Examples
  31. Example: Notes with table of contents
  32. Notes example (part 1)
  33. Notes example (part 2)
  34. Notes example (part 3)
  35. Notes example (part 4)
  36. Notes example (part 5)
  37. Notes example (complete)
  38. Some other XSLT elements
  39. Exercises
  40. Links to more information

6.1. Extensible Style Language (XSL)

6.2. XSL example

6.3. XSL rules

6.4. XSL Formatting

6.5. Ways of Presenting XML

Different ways of presenting XML

6.6. XSLT Program Skeleton

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="...">
      ...
  </xsl:template>

  <xsl:template match="...">
      ...
  </xsl:template>

  ...

</xsl:stylesheet>

6.7. Data Model - example document

<CD publisher="Deutsche Grammophon"
    length="PT1H13M37S" >
  <composer>Johannes Brahms</composer>
  <performance>
    <composition>Piano Concerto No. 2</composition>
    <soloist>Emil Gilels</soloist>
    <orchestra>Berlin Philharmonic</orchestra>
    <conductor>Eugen Jochum</conductor>
  </performance>
  <performance>
    <composition>Fantasias Op. 116</composition>
    <soloist>Emil Gilels</soloist>
  </performance>
</CD>

6.8. Data Model - example tree

Tree representation of CD example

6.9. Data Model Description

6.10. XSLT Processing Model

6.11. RSS Example

<rss>
  <channel>
    <title> ... </title>
      ...
    <item>
      <title> ... </title>
      <description> ... </description>
      <link> ... </link>
      <pubDate> ... </pubDate>
    </item>
      ...
    <item>
      <title> ... </title>
      <description> ... </description>
      <link> ... </link>
      <pubDate> ... </pubDate>
    </item>
  <channel>
</rss>

6.12. Example of an XSLT Rule

  <xsl:template match="channel">
    <html>
      <xsl:apply-templates select="item"/>
    </html>
  </xsl:template>

6.13. Another Example of an XSLT Rule

  <xsl:template match="item">
    <p>
      <xsl:value-of select="title"/>
    </p>
  </xsl:template>

6.14. Example: RSS headlines

6.15. Saxon and XT

6.16. Using a stylesheet processing instruction

6.17. Example: RSS descriptions

applying the stylesheet rss.xsl comprising

  <xsl:template match="channel">
    <html>
      <head>
        <title><xsl:value-of select="title"/></title>
      </head>
      <body>
        <table border="1">
          <xsl:apply-templates select="item"/>
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="item">
    <tr>
      <td><xsl:value-of select="title"/></td>
      <td><xsl:value-of select="description"/></td>
    </tr>
  </xsl:template>

to rss-fragment.xml yields (rss-fragment.html) rss-fragment-xsl.xml as viewed in a browser with the correct stylesheet processing instruction

6.18. Example: RSS headlines (again)

6.19. Some XPath expressions

6.20. XPath expressions

6.21. Relative expressions

6.22. Simple subset of XPath

6.23. Example: using XPath (1)

6.24. Example: using XPath (2)

6.25. Built-in template rules (1)

6.26. Built-in template rules (2)

6.27. More XPath examples

6.28. Predicates

6.29. Node-Set Functions

6.30. Examples

6.31. Example: Notes with table of contents

6.32. Notes example (part 1)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="chapter"/>

<xsl:template match="/html">
  <html>
    <!-- Part 2 goes here -->
    <body>
      <!-- Output heading -->
      <h1>
        <xsl:value-of select="$chapter"/>.
        <xsl:value-of select="head/title"/>
      </h1>
    <hr />

    <!-- Part 3 (table of contents) goes here -->
    <!-- Output the contents, numbering each h1 as an h2 -->
    <xsl:apply-templates select="body/div" />

    </body>
  </html>
</xsl:template>

<!-- Parts 4 (matching slides) and 5 (matching other divs) go here -->

</xsl:stylesheet>

6.33. Notes example (part 2)

<!-- Output title and scripts -->
<head>
  <title><xsl:value-of select="head/title"/></title>
  <link rel="stylesheet" href="../notes.css" type="text/css" />
  <xsl:copy-of select="head/style"/>
  <xsl:copy-of select="head/script[not(contains(@src,'slidy.js'))]"/>
</head>

6.34. Notes example (part 3)

<!-- Table of contents has one entry for each h1 -->
<ol>
  <xsl:for-each select="body/div[@class='slide']/h1">
    <li><xsl:value-of select="."/></li>
  </xsl:for-each>
</ol>
<hr />

6.35. Notes example (part 4)

<xsl:template match="div[@class='slide']">
  <xsl:variable name="section"><xsl:number count="div[@class='slide']/></xsl:variable>
  <xsl:for-each select="*">
    <xsl:choose>
      <xsl:when test="name()='h1'">
        <h2>
          <xsl:value-of select="$chapter"/>.
          <xsl:value-of select="$section"/>.
          <xsl:value-of select="."/>
        </h2>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:for-each>
</xsl:template>

6.36. Notes example (part 5)

<xsl:template match="div">
  <xsl:if test="@class='handout' or @class='page-break'">
    <xsl:copy-of select="."/>
  </xsl:if>
</xsl:template>

6.37. Notes example (complete)

6.38. Some other XSLT elements

6.39. Exercises

  1. Consider an XML represention of information about students on an MSc programme. All information should be represented using elements rather than attributes. The root element of the document is programme. A programme has a degree, whose value might be "MSc", and a year, whose value might be "2014/2015". These elements are followed by the results for the programme. The results are partitioned into distinction, merit, pass and fail. Within each is a sequence of name elements, each containing the name of a person having achieved the corresponding result for the programme.

    Write an XSL template rule that, when matched against an XML document described above, produces an HTML document comprising a list of names of those students who obtained distinctions. The title of the document should be assembled from the contents of the degree and year elements, so that the answer when run on the document with the values suggested above would be "MSc (2014/2015)". There should be a level-2 heading "List of Distinctions", followed by an unnumbered list of names of students who obtained a distinction.


  2. Write an XSLT program which will transform an XML document of the form:
    <teaches>
      <teaches-tuple course="IWT" lecturer="Peter Wood"/>
      <teaches-tuple course="CS" lecturer="Szabolcs Mikulas"/>
    </teaches>
    
    into one of the form:
    <teaches>
      <teaches-tuple>
        <course>IWT</course>
        <lecturer>Peter Wood</lecturer>
      </teaches-tuple>
      <teaches-tuple>
        <course>CS</course>
        <lecturer>Szabolcs Mikulas</lecturer>
      </teaches-tuple>
    </teaches>
    
    You can assume that teaches is the root element, and that the course and lecturer attributes are required. Obviously your program should work for any number of occurrences of the teaches-tuple element.


  3. For this exercise the source XML document is booker.xml. This file contains information about winners of the Booker prize. You should save a copy of this file in the directory where you intend to do the exercise. You will need to look at the document in order to see how the elements are structured.

6.40. Links to more information

XSLT is covered in Chapter 6 and 7 of [Jacobs] and in Chapter 5 of [Moller and Schwartzbach].