XSLT Keys

Keys are an important feature of XSLT. They allow one to look up values using a simple key rather than an elaborate XPath expression. For this lab assignment, review section “5.9 Declaring Keys and Performing Lookups” in Ducharme’s XSLT Quickly.

Ducharme’s is a very simple example. You may wish to look at some others:

For the lab assignment, write XSLT that transforms the XML below (works.xml) into this html output.

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE works [  
<!ELEMENT works (work+,authors?)>  
<!ELEMENT work (title,date?)>  
<!ATTLIST work  
  id ID #REQUIRED  
  author IDREF #IMPLIED>  
<!ELEMENT title (#PCDATA)>  
<!ELEMENT date (#PCDATA)>  
<!ATTLIST date  
  when CDATA #IMPLIED  
  from CDATA #IMPLIED  
  to   CDATA #IMPLIED>  
<!ELEMENT authors (author+)>  
<!ELEMENT author (name,date?)>  
<!ATTLIST author  
  id ID #REQUIRED>  
<!ELEMENT name (#PCDATA)>  
]>  
<works>  
  <work id="pb1" author="acs">  
    <title>Poems and Ballads, First Series</title>  
    <date when="1866"/>  
  </work>  
  <work id="sbs" author="acs">  
    <title>Songs Before Sunrise</title>  
    <date when="1871"/>  
  </work>  
  <work id="dp" author="rb">  
    <title>Dramatis Personae</title>  
    <date when="1864"/>  
  </work>  
  <work id="sordello" author="rb">  
    <title>Sordello</title>  
    <date when="1840"/>  
  </work>  
  <work id="pcl" author="alt">  
    <title>Poems, Chiefly Lyrical</title>  
    <date when="1830"/>  
  </work>  
  <work id="p1842" author="alt">  
    <title>Poems</title>  
    <date when="1842"/>  
  </work>  
  <work id="p1833" author="alt">  
    <title>Poems</title>  
    <date when="1833"/>  
  </work>  
  <authors>  
    <author id="acs">  
      <name>Swinburne, Algernon Charles</name>  
      <date from="1837" to="1909"/>  
    </author>  
    <author id="alt">  
      <name>Tennyson, Alfred Tennyson, Baron</name>  
      <date from="1809" to="1892"/>  
    </author>  
    <author id="rb">  
      <name>Browning, Robert</name>  
      <date from="1812" to="1889"/>  
    </author>  
  </authors>  
</works>

In your solution, declare keys that allow you to look up the author name, author birth date, and author death date. For example, a key to look up the author’s name might be declared as follows:

<xsl:key name="authorNameKey" match="author/name" use="../@id"/>

And the author’s name might then be included the output with the following code:

<xsl:value-of select="key('authorNameKey',@author)"/>

Notice that the output is sorted by two sort criteria, first by author name, and then by date of the work. Your solution should be sorted, using XSLT, the same way.