A Lab Session on XSLT

Introduction

In this lab, you will work through some exercises in XSLT.

Saxon - XSLT Processor

  1. First, create a directory called saxon.
  2. Saxon is freely available on the Web. Here is a local copy of the version 9.5.1 (Home Edition).
  3. Unzip the file into the 'saxon' directory you have created.
  4. It has a complete documentation and samples. In this lab, you are interested in using the saxon9he.jar file.

Downloading exercise files

Download the source code of the examples.

Unzipping the file will create a directory called xslexample. All XSLT files and related XML documents from the lecture will be there.

Copy saxon9he.jar to the xslexample directory.

Trying out the XSLT examples

Check that JAVA_HOME is set. Test the examples and examine the output files. You should also examine the XSLT files and see if you can understand the transforming process in each example (Any doubts? Ask questions.)

  1. java -jar saxon9he.jar edmond.xml edmond.xsl > out.html
  2. java -jar saxon9he.jar books.xml BIBstyle2.xsl > out.html (Note: BIBstyle2 uses apply-templates element)
  3. java -jar saxon9he.jar books.xml BIBstyle.xsl > out.html (Note: BIBstyle uses for-each element)
  4. java -jar saxon9he.jar Phonebook.xml p2p.xsl > out.xml
  5. java -jar saxon9he.jar gem.xml MyFriends.xsl > out.html
  6. java -jar saxon9he.jar gem.xml GEMholiday.xsl > out.html

Developing your own XSLT programs

gem.xml

Now write an XSLT program over the gem.xml file that produces a listing of all friends and the number of holidays each has taken. The output should be something like this:

<html>
<body>
      <table>
        <tbody>
          <tr>
            <td>Sue</td>
            <td>3</td>
          </tr>
          <tr>
            <td>Bill</td>
            <td>3</td>
          </tr>
          <tr>
            <td>Doug</td>
            <td>2</td>
          </tr>
        </tbody>
      </table>
</body>
</html>

sdb.xml

Now write some XSLT programs over the sdb.xml file that produces the given outputs as shown:

  1. Generate a listing of Assessment Items with weight greater than 25.

    <?xml version="1.0" encoding="utf-8"?>
    <query>
       <row>
          <title>XML</title>
          <weight>30</weight>
       </row>
       <row>
          <title>Exam</title>
          <weight>60</weight>
       </row>
    </query>
    

  2. Generate a listing of students, and the number of assessment result items for each student (hint: use need xslt:variable to 'join' Students and Result),

    <?xml version="1.0" encoding="utf-8"?>
    <query>
       <row>
          <sid>871</sid>
          <sas>3</sas>
       </row>
       <row>
          <sid>862</sid>
          <sas>3</sas>
       </row>
       <row>
          <sid>869</sid>
          <sas>3</sas>
       </row>
       <row>
          <sid>854</sid>
          <sas>2</sas>
       </row>
       <row>
          <sid>831</sid>
          <sas>0</sas>
       </row>
       <row>
          <sid>872</sid>
          <sas>3</sas>
       </row>
       <row>
          <sid>868</sid>
          <sas>3</sas>
       </row>
    </query>
    

Sorting XSLT output

In this section, we will examine the ability to sort the output produced as part of an XSL transformation.

  1. Open the XML data file aus.xml. You should see a list of all eight Australian states and territories. They are classified as "divisions", and each division's name, abbreviation, capital city, area and population is provided.
  2. Try aussort1.xsl via:
    java -jar saxon9he.jar aus.xml aussort1.xsl > out.html
  3. Examine the output file. You should see a list of all divisions in descending order of area (in square kilometres).
  4. Open the XSLT program aussort1.xsl. Note the use of the xsl:sort element to sort the table in descending order of area. Also, note the location of the sort element (ie., inside 'apply-templates')
  5. In a new browser window, open this xslt reference link. You should then scroll down to xsl:sort. Read up about the sort options in XSLT.
  6. Now change the program so that it sorts the output by ascending order of population.

Providing parameters to XSLT programs

We will often wish to parameterise an XSLT program. A simple example would occur when we only want to select some of the possible output that might result from scanning the document. In the next program, we will display information about just one of the states or territories, depending upon a value supplied by the user. The value will be the abbreviation, for example, VIC for Victoria.

  1. Try the following:
    java -jar saxon9he.jar aus.xml ausparam1.xsl short=QLD > out.html
  2. Examine the output file. You should see information specific to Queensland.
  3. Open the file ausparam1.xsl file. You should make sure you understand the following features of the program:
    1. The declaration of the parameter "short".
    2. The use of the parameter in the form $short in the <H2> element.
    3. The use of the expression division/name[@abbr=$short] to ensure that the only divisions selected are those with a name that has an "abbr" attribute equal to the parameter provided. This expression has the effect of choosing name elements for template matching.
    4. The consequent appearance of match="name" to catch these elements.
    5. The use of select="." to display the name element.
    6. The use of select="../cap", for example, to display sibling elements of name.

Indexing elements in XSLT

We can tell an XSLT program that we want to build an index structure. This could be useful if the transformation requires repeated access to elements. Suppose that, our program needs to frequently access divisions by means of abbreviations such as QLD. We can define a key that may then be used to access division by means of their abbreviations.

  1. Try the following:
    java -jar saxon9he.jar aus.xml auskey1.xsl short=QLD > out.html
  2. Examine the output file. You should see information specific to Queensland, just as we did with the previous program.
  3. Open the file auskey1.xsl file. You should make sure you understand the following features of the program:
    1. The use of the xsl:key element to state our need for an index, and its three attributes: (1) the name="by-abbr" which allows us to name the key structure; (2) the match="name" which indicates that we want to access name elements by means of this structure; and (3) the use="@abbr", which indicates that we want to access these elements by means of their abbreviation.
    2. The use of select="key('by-abbr',$short)" to indicate that we only want to access name elements that match the abbreviation indicated in the short parameter.