7. Extensible Style Language


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

7.1. Extensible Style Language (XSL)

7.2. XSLT example

7.3. XSLT rules

7.4. 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>

7.5. 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>

7.6. Data Model - example tree

Tree representation of CD example

7.7. Data Model Description

7.8. XSLT Processing Model

7.9. 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>

7.10. Example of an XSLT Rule

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

7.11. Another Example of an XSLT Rule

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

7.12. Example: RSS headlines

7.13. Saxon and XT

7.14. Using a stylesheet processing instruction

7.15. 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

7.16. Example: RSS headlines (again)

7.17. Some XPath expressions

7.18. XPath expressions

7.19. Relative expressions

7.20. Simple subset of XPath

7.21. Example: using XPath (1)

7.22. Example: using XPath (2)

7.23. Built-in template rules (1)

7.24. Built-in template rules (2)

7.25. More XPath examples

7.26. Predicates

7.27. Node-Set Functions

7.28. Examples

7.29. Some other XSLT instructions

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

7.30. Further XSLT elements

7.31. Querying and transforming JSON

7.32. 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 "2018/2019". 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 (2018/2019)". 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.

7.33. Links to more information

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