XSLT Exercises

To do these exercises, you need to download a number of sample XML files. They have been placed in this folder (from which you also downloaded the XSLTs used for the Transformation of XML).

N.B. Model answers to the exercises below can be found here.

Exercise 1

Follow the steps specified in the list below to create a stylesheet that can transform letter.xml into an HTML document that looks as follows:

Letter from De Erven F. Bohn to W. Blackwood and sons, January 22nd, 1873

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
  • From the folder that you have downloaded, open the file “letter.xml” in Oxygen. Choose File > New in Oxygen and select ‘XSL stylesheet’. Save the new file as “letter.xsl” in the same directory as the XML file.
  • Build the style sheet. The HTML codes that you need to use in your XSLT stylesheet for this exercise are provided below. Add XSLT-code on the positions of the comments.

Note that XSLT transforms the structure of an XML source file into a result file of many types. You will have to insert the structural code of the result file yourself: if you want to create an HTML file from an XML file with XSLT, this implies that you will have to add the fundamental elements of the HTML-result file.

<html>
  <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />         
     <title><!-- title from head --></title>
  </head>
 <body>
   <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>
  </body>
 </html>

Exercise 2

Create a stylesheet that can transform “literatureList.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

Exercise 3

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

Exercise 4

Building on the stylesheet that you have created for exercise 3, create a stylesheet that can transform “literatureList.xml” into an HTML file as below.
Note that it displays the full name of the author (inverted order), the title and the full imprint (in the form Place: Publisher, Year). Sort the list by the last name of the author.

  • Brinker-von der Heyde, Claudia. Die literarische Welt des Mittelalters. Darmstadt: Wissenschaftliche Buchgesellschaft, 2007.
  • Burke, Peter. A Social History of the Media, from Gutenberg to the Internet. London: Polity Press, 2005.
  • Darnton, Robert. The kiss of Lamourette : reflections in cultural history. New York: Norton, 1990.
  • Eisenstein, Elizabeth Lewisohn. The printing revolution in early modern Europe. Cambridge University Press, 1983.
  • Febvre, Lucien. l’Apparition du livre. Paris: É́ditions Albin Michel, 1971.
  • Funke, Fritz. Buchkunde :ein Überblick über die Geschichte des Buches. München: Saur, 1992.
  • Martin, Henri-Jean. Les métamorphoses du livre : entretiens avec Jean-Marc Chatelain et Christian Jacob. Paris: Albin Michel, 2004.
  • Thompson, John B. Books in the digital age: the transformation of academic and higher education publishing in Britain and the United States. Cambridge: Polity, 2005.

 

Exercise 5

Write four separate stylesheets (1-4) that can make the bulleted listst with the following selections from “literatureList.xml”. You should write separate stylesheets for these four subquestions. Note that it might be wise to re-use (parts of) the stylesheet you have built 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 a single XSLT stylesheet  that displays quantitative information about the file “collection.xml”:

  • The total number of letters in the collection.
  • The number of letters written before 1900.
  • The number of letters in English.

Create a list displaying all the letters which are addressed to A.W. Sijthoff. Show only the shelf marks.

 

Exercise 7

 

Create an XSLT that can transform the file “collection.xml” into the HTML document below:

 

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 8

Create a stylesheet that can transform “poem.xml” (from the folder you have downloaded) into an HTML document that is presented looks as follows:

Snow

The room was suddenly rich and the great bay-window was
Spawning snow and pink roses against it
Soundlessly collateral and incompatible:
World is suddener than we fancy it.

World is crazier and more of it than we think,
Incorrigibly plural. I peel and portion
A tangerine and spit the pips and feel
The drunkenness of things being various.

And the fire flames with a bubbling sound for world
Is more spiteful and gay than one supposes –
On the tongue on the eyes on the ears in the palms of one’s hands –
There is more than glass between the snow and the huge roses.

 

Exercise 9

Download and open the file “wilde-windermere.xml“. This contains the full text of the play Lady Windermere’s Fan by Oscar Wilde, in TEI-markup. Write a stylesheet which can make the following selections in the play:

  1. All parts spoken by Lord Darlington;
  2. Parts in which Lord Windermere speaks of Mrs Erlynne;
    N.B. use contains( element , ‘text’ ). Also note that you can COMBINE two different conditions using the keyword ‘and’ , as follows: condition1 and condition2
  3. Stage directions indicating the entrance of a character on the stage;
  4. Parts spoken by Lady Windermere which consist of less than 50 characters. To test string-length. N.B. use string-length(p) &lt; 50 

For questions 1, 2 and 3, you can use the code below as a basis. In this code, you only need to add the appropriate XPath expressions in for-each and in value-of.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 version="1.0">
 <xsl:template match="TEI">
 <html>
 
 <body>
 <p>
 
 <h3><!-- Description of the list--></h3>
 <ul>
 
 <!-- 
 <xsl:for-each select="">
 <li>
 <xsl:value-of select="" />
 </li>
 </xsl:for-each> -->
 
 </ul>
 
 
 </p>
 </body>
 </html>
 </xsl:template>
 
</xsl:stylesheet>

Xpath can also be used to produce quantitative information about an XML file. The count() function is very useful for this purpose. Write an XSLT stylesheet which can produce data about the following aspects of the TEI file.

  1. The number of parts spoken by Lady Windermere
  2. The number of parts spoken by Lady Windermere in the first act

Use the code below as a basis.

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

<h3>The number of parts spoken by Lady Windermere:</h3>
 <xsl:value-of select=""/>

<h3>The number of parts spoken by Lady Windermere in the first act</h3>
 <xsl:value-of select=""/>
 </p>
 </body>
 </html>
 </xsl:template>
 
</xsl:stylesheet>

Note that you do not have to build full XSLT stylesheets (you still can do so, of course). Oxygen provides an XPath engine in which you can test your XPath expressions. You can do this exercise by firstly writing down the expression in a text file. The complete Path expression can then be copied and pasted into the XPath engine in Oxygen.

Exercise 10

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\”>

 

Exercise 11

Create XSLT stylesheets that can help to carry out the transformation scenarios below.

ShakespearePoems.xml This is an encoded version of Shakespeare’s Complete Poems. Create a stylesheet which can produce the following sentence: “This volume contains … sonnets,
and … other poems.”. Note that all poems are encoded in a <div>
element. The two different types of poems can be distinguished using the
“type” attributes of these <div> elements.Also print the full text of Sonnet 17. To select the sonnet, make use of the “n” attribute of the <div> element.
Mendeley.xml Write a stylesheet which can produce the following sentence: “This bibliography contains … references. … of these are journal articles. Information about the reference type can be found in the <ref-type> element.
Create a bulleted list containing all the references to articles in the journal “Digital Humanities Quarterly”. The journal title is mentioned in the <periodical> element). For each of these refereces, display the author name, the full title in italcs, and the year of publication. The references must be sorted alphabetically, using the last name of the authors.
Ead.xml Create a bulleted list which displays all the correspondence files which are described in the XML document. The letters are all encoded using the <c02> element. Of these letters, you only need to display the <unittitle>. The prints must be sorted according to the title of these correspondence files. These are encoded in the <unittitle> element.
Display the following sentence: “This file contains … correspondence
files.”.
Nyt-RSS.XML Create a stylesheet that can produce the following sentence: “This file contains … news items. …. Of these are about Paris.”. When the <description> element of an item contains the word ‘Paris’, you can
assume that this item is about this city. Additionally, create a bulleted
list which shows all items in which the title contains the word ‘Trump’. For
each items, display the title in bold, the description and the publication
data. Sort the list alphabetically by title.
DHQ.xml This file is a TEI-encoded version of an article 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. Display only those references whose title consist of less than 25 characters.
goodReads.xml This file contains data from the GoodReads website. It has data about a specific title, next to data about books, which, according to readers, are related to this title. In the section that is encoded using the element <similar_books>, count both the total number of books, and the number of books which have an average rating of four and higher. Additionally create a bulleted list which shows all books which were published after 2013.
Show the author name, the title in italcs and the year of publication. This list must be sorted alphabetically
by title.