Universiteit Leiden

XSLT Exercises

The files that are mentioned in the exercises below can all be found in the folder "XSLT.zip". Download this zipped folder, and unzip/unpack it in your working directory.

Exercise 1

Write a stylesheet that can transform the XML file named "letter.xml" into an HTML document containing the following information:

Letter from De Erven F. Bohn to W. Blackwood and sons

Dear Sirs!

We beg to apply to you the kind request for sending us one week before the publication one copy of Bulwer's novel: Kenelm Chillingly, His adventures and opinions, which book you have in the press, for what we are inclined to pay 30£. When it were possible to send us already now the first volume by the post; it would be yet more agreeable. Mr H.A. Kramers at Rotterdam readily will be our pledge.


yours truly

De Erven F. Bohn

Note that you don't necessarily need to replicate the font colours or the background colour exactly. The main aim of this exercise is to the extract the necessary information from the XML source file and to copy it into the HTML file that results from the transformation. Follow the steps below.

  <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  <title><!-- title from head --></title>

  <h2><!-- title from head --></h2>  
   <p> <!-- Opening salute from body/greeting --></p>  
  <p><!-- Body of the letter from body/p --></p>  

   <p><!-- Closing salute from body/salute --></p>    
 <p align="center"><!-- Name of the letter's sender from body/signed--></p>  


Exercise 2

Create a stylesheet that can transform "bibliography.xml" (from the folder you have downloaded) into an HTML document that looks as follows:

  • A Social History of the Media, from Gutenberg to the Internet
  • The printing revolution in early modern Europe
  • The kiss of Lamourette : reflections in cultural history
  • Buchkunde: ein Überblick über die Geschichte des Buches
  • Books in the digital age: the transformation of academic and higher education publishing in Britain and the United States
  • Die literarische Welt des Mittelalters
  • l'Apparition du livre
  • Les métamorphoses du livre: entretiens avec Jean-Marc Chatelain et Christian Jacob

Follow the same steps as in exercise 1:

Exercise 3

Using the result of exercise 2, create a stylesheet that can display all the book titles in "bibliography.xml" in alphabetical order.

To do this, make use of the <xsl:sort> element, as explained in section 8 of the XSLT tutorial. This element must be the first element within the body of the <xsl:for-each> element.

Exercise 4

Building on the stylesheet that you have created for exercise 3, create a stylesheet that can transform "bibliography.xml" into an HTML file which presents all the titles in the following citation style:
[Full name of the author (inverted order)], [Title (in italics)]. [Place]: [Publisher], [Year].
Sort the list by the last name of the author. Add all the punctuation in <xsl:text> elements.

Exercise 5

Write four separate stylesheets that can filter the titles in "bibliography.xml", using the criteria that are given below. Obviously, you can re-use (parts of) the code that you have created for exercise 4.

  1. Only the English titles
  2. Titles written after the year 2000.
  3. The titles that are NOT written in French.
  4. The titles published in New York.

Exercise 6

Create an XSLT stylesheet that can transform the XML file named "collection.xml" into a web page (i.e. an HTML file) that presents the information about the letters as follows:

Letter Collection of the University of Leiden

  • Letter from Antheunis-Conscience to Sijthoff, Albertus Willem
  • Letter from Behrend, Fritz to Vreese, Willem Lodewijk de
  • Letter from Blackwood, William to Tiele, Cornelis Petrus
  • Letter from Butz, Kilian to Kraus, Fritz Rudolf
  • Letter from De Erven F. Bohn to William Blackwood & Sons
  • Letter from De Erven F. Bohn to Lytton, Lord
  • Letter from De Erven F. Bohn, to Sampson Low, Marston and Company, London
  • Letter from Fuhri, Koenraad to Kruseman, Arie Cornelis
  • Letter from Gamillscheg, Ernst to Huizinga, Johan
  • Letter from Hamel, J.A. van to Sijthoff, Albertus Willem
  • Letter from Harting, P. to Muller, Jacob Wijbrand
  • Letter from Hawkshaw, John to Tideman, Jan
  • Letter from Laufer, Berthold to Schmeltz, Johannes Diederich Eduard
  • Letter from Muller, Frederik to Tideman, Jan
  • Letter from Muller, Frederik to Sijthoff, Albertus Willem
  • Letter from Muller, Frederik to Sijthoff, Albertus Willem
  • Letter from Schneider, Lina to Tiele, Cornelis Petrus
  • Letter from Sijthoff, Albertus Willem to Hensbroek, Pieter Andreas Martin Boele van
  • Letter from Suringar, Gerard Tjaard Nicolaas to Kruseman, Arie Cornelis
  • Letter from Wirth, Herman Felix to De Erven F. Bohn


Exercise 7

Building on the code that you created for exercise 7, create an XSLT stylesheet which only displays the letters in the file "collection.xml" written before 1900.
Add the criterion in square brackets after the element mentioned in the <xsl:for-each> element.

Exercise 8

Building on the code that you created for exercise 6, create an XSLT stylesheet that displays quantitative information about the file "collection.xml". The web page that is created should display the following information:

  • The total number of letters in the collection.
  • The number of letters written before 1900.
  • The number of letters in English.
  • The number of letters addressed to 'Sijthoff, Albertus Willem'.

These number should not be typed in manually; they must be calculated by the stylesheet.

Exercise 9

Download the file goodReads.xml from the following address: https://raw.githubusercontent.com/peterverhaar/dmt-2020/master/-Files/goodReads.xml

The GoodReads Api enables you to download data about a specific book title, identified by an ISBN. The file goodReads.xml contains all the data that is available for the title with ISBN 9780385537148.

Among other things, the file contains bibliographic data about the book, and data about books, which, according to readers, are related to this title. In the section that is encoded using the element , count both the total number of shelves the book is added to, and the number of shelves that have a count of 25 or higher (if applicable). In addition, create a bulleted list which shows all books this title is related to. Only show those titles which have a rating of 4 or higher. Show the author name, the title in italics and the year of publication. This list must be sorted on the basis of the rating, in a descending order.

Exercise 10

Download the following TEI file: https://raw.githubusercontent.com/peterverhaar/dmt-2020/master/-Files/Hamlet.xml

Write a stylesheet that can count the number of acts, or, if no acts are available, the number of stage directions. Provide this information by writing the following sentence: "This play consists of ... [acts / stage directions ].". Next, choose a character from the play, and calculate data about the number of speeches spoken by this character (produce the sentence "The character [enter name of character here] has ... speeches"). All the speeches in the play are encoded using the <sp> element. Create a list of all the spoken parts in the play which consists of less than 100 characters. Also display the total number of characters in between brackets. N.B. The length of a string of text can be calculated using the string-length() function. The files that were made available have been encoded in the Text Creation Partnership.

Exercise 11

Download the file DHQ.xml from the following address: https://raw.githubusercontent.com/peterverhaar/dmt-2020/master/-Files/DHQ.xml

This file is a TEI-encoded version of an article that was published in the Digital Humanities Quarterly. Create a stylesheet that can produce the following sentence: "This article contains [...] paragraphs, and [...] section headings. [...] paragraphs contain a footnote". Footnotes are encoded using the <ptr> element. Also create a bulleted list which displays the full references in the article's bibliography. These references can be found in the <listBibl> element. Each reference is encoded using the <bibl> element. Display only those references whose title consist of less than 25 characters.

Exercise 12

The stylesheet "tei-html.xsl" (in the zipped folder you've previously downloaded) can transform the descriptive markup in the TEI document to HTML elements that specify the typographic appearance. Read this XSLT stylesheet and try to determine which rules are given for the following TEI elements:

  1. <gap>
  2. <supplied>
  3. <pb>
  4. <lb>
  5. <title>
  6. <hi rend="underlined">