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

   

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

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.

 

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 an XSLT that can transform the file "collection.xml" into the HTML document below:

Letter Collection of the University of Leiden

 

Exercise 7

Building on the code that you created for exercise 6, create an XSLT stylesheet that displays quantitative information about the file "collection.xml":

 

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>
<!-- Title t-->
</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">