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.

Model answers to all of these exercises can be found here.

Exercise 1

The folder that you have downloaded contains an XML file named "tei.xml" and an XSLT file named "tei-html.xsl". Try to transform the former file using the latter. If you perform the XSLT transformation using the Jupyter notebook "XSLT.ipynb", make sure that the two variables specifying the filenames have the following values.

xml_filename = 'tei.xml'
xsl_filename = 'tei-html.xsl'

Exercise 2

The file named "tei-html.xsl" contains a number of CSS instructions specifying the layout of the webpage. The CSS code starts at line 11. Try to create an HTML file with a different background corner and with a different font type by changing the CSS instructions in the stylesheet.

Exercise 3

The folder that you have downloaded als contains an XSLT file named "columns.xsl". This stylesheet creates a page with two columns. The column on the left presents the diplomatic edition, and the column on the right shows the normalised edition. Try to transform "tei.xml" using this stylesheet. The two variables specifying the filenames need to be given the following values.

xml_filename = 'tei.xml'
xsl_filename = 'columns.xsl'

Exercise 4

Next, try to transform either "jats1.xml" or "jats2.xml" into HTML, using the stylesheet named "jats-html.xml". You can find more information about JATS on the NLM website.

Exercise 5

Try to transform the file "tei.xml" into a PDF fie using the stylesheet "tei-pdf.xsl". You can perform this transformation using XSL:FO in Oxygen.

Exercise 6

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.

  • From the folder that you have downloaded, open the file "letter.xml" in an XML editor.
  • Also open the file "xslt-template.xsl" and rename it into "letter.xsl".
  • Build the style sheet (i.e. the file "letter.xsl"):
    • In the match attribute of <xsl:template>, mention the root element of the document you want to transform
    • Within the body of the template, add the basic HTML tags that can create the webpage presenting the contents of the letter. For this purpose, you can copy the HTML tags below.
    • Add a number of <xsl:value-of> statements to copy and paste contents from the XML source document.
    • Transform the XML file into an HTML document named "letter.html".
<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 7

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:

  • Open the XML file that you want to transform in an XML editor.
  • Open the file "xslt-template.xsl" and rename it. Use "bibliography.xsl" as a file name, for instance.
  • In the match attribute of the <xsl:template> element, mention the root element of the XML file.
  • Set up the basic HTML file within the body of the <xsl:template> element. Use the HTML codes below as a basis.
  • Add the codes that are needed to ensure that the stylesheet displays ALL the titles in the XML file.

<html>
<header>
<title>
Bibliography
</title>
</header>
<body>

<p>
<!--
Display the full title of EACH item. The title must be 
followed by a line break ('<br/ >')
-->
</p>

</body>

</html>

Exercise 8

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 9

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.

The result should look as follows:

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 10

Write a stylesheet that can filter the titles in "bibliography.xml". Firstly, create a bibliography containing only the English titles. Secondly, create a bibliography listing the titles written after the year 2000. Obviously, you can re-use (parts of) the code that you have created for exercise 4.

Use the HTML code below as a basis.

<html>
<header>
<title>
Bibliography
</title>
</header>
<body>

<h2>English titles</h2>

<p>
<!--
Display the titles, followed by a line break.
-->
</p>

<h2>Titles written after 2000. s</h2>

<p>
<!--
Display the titles, followed by a line break.
-->
</p>

</body>
</html>

This HTML result can be created using a stylesheet containing only one <xsl:template>. With this organisation of the XSLT file, you will probably need to repeat some code. You can make the code in the stylesheet more efficient by working with multiple templates. You could create a separate stylesheet for the <item> element and involke this second template using <xsl:apply-templates>, as explained in the last two sections of the XSLT tutorial. OPTIONALLY, you can try to develop such a stylesheet containing two templates.

Exercise 11

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">