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. Some other XSLT instructions
  32. Further XSLT elements
  33. Querying and transforming JSON
  34. The JSONiq query language
  35. JSONiq variable binding and iteration
  36. Combining for, let, where and return
  37. Navigating JSON objects
  38. Navigating JSON arrays
  39. Joining JSON data
  40. Exercises
  41. 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"

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

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



6.7. Data Model - example document

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

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

    <title> ... </title>
      <title> ... </title>
      <description> ... </description>
      <link> ... </link>
      <pubDate> ... </pubDate>
      <title> ... </title>
      <description> ... </description>
      <link> ... </link>
      <pubDate> ... </pubDate>

6.12. Example of an XSLT Rule

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

6.13. Another Example of an XSLT Rule

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

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">
        <title><xsl:value-of select="title"/></title>
        <table border="1">
          <xsl:apply-templates select="item"/>

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

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. Some other XSLT instructions

  <xsl:when test="...">
<xsl:if test="...">
  <xsl:copy-of select="..."/>

6.32. Further XSLT elements

6.33. Querying and transforming JSON

6.34. The JSONiq query language

6.35. JSONiq variable binding and iteration

6.36. Combining for, let, where and return

6.37. Navigating JSON objects

6.38. Navigating JSON arrays

6.39. Joining JSON data

6.40. Exercises

  1. Consider an XML representation 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 "2017/2018". 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 (2017/2018)". 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-tuple course="IWT" lecturer="Peter Wood"/>
      <teaches-tuple course="CS" lecturer="Szabolcs Mikulas"/>
    into one of the form:
        <lecturer>Peter Wood</lecturer>
        <lecturer>Szabolcs Mikulas</lecturer>
    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.41. Links to more information

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