ICT336 title
Murdoch University logo

Laboratory Week 7b - XSLT


Learning Objectives:

  1. Learn how to use XSLT to transform an XML document.

Required Reading:

Lectures: Week 6 lecture 2
Sybex Textbook XML Complete Chapter 12.

Access to Software:

In the following exercises, we will transform XML documents with XSLT style sheets using the Sablotron toolkit. The necessary programs are available on gryphon.murdoch.edu.au. If you wish to install the toolkit on your own machine off campus, see the unit's downloads page.

You will also use web browsers with XSLT support to do the transformations. See the downloads page for a list of recommended browsers. The correct versions of Microsoft IE and Netscape Navigator is available in the internal labs.

Access to required files:

The following exercises make use of files available in the unit coordinator's directory (~hiew/examples/xsl/) on gryphon. Use the command "cp" to copy the file over to your work directory on gryphon. Eg.

gryphon:~> mkdir ~/labwk7b
gryphon:~> cp ~hiew/examples/xsl/* ~/labwk7b

Then use your secure FTP client to download all the files to your local machine.

Instructions:

  1. Start a ssh session to gryphon.murdoch.edu.au. Copy this lab's example files to your own directory using instructions given in "Access to Example Scripts" above. Use the Sablotron command "sabcmd" to transform the course.xml document using the XSLT style sheet default.xsl. Eg.

    gryphon:~labwk7b> sabcmd default.xsl course.xml

    You will see the result of the the transformation display on the command line

  2. Using a text editor, open and read all the example .xsl files. Understand how each one works:
    • default.xsl: an empty style-sheet - ie. it will use the default transformation.
    • donothing.xsl: overrides the default templates with an empty template, so the style sheet does nothing.
    • nodes.xsl: Printing the name of every node in the source tree.
    • unit_titles1.xsl: extracting all unit titles.
    • unit_titles2.xsl: extracting all unit titles - a much better implementation.
    • anotherxml.xsl: transforming to another XML format.
    • tohtml.xsl: transforming to HTML.

    For each of the style sheets, repeat step 1 to see the result of how they transform the course.xml document.
  3. FTP all the example XML adn XSL files from gryphon to your local or network drive. Load course.xml using a web browser with XSLT support (see Access to Software above) using File>Open... menu item. You will see the default display option for XML files by the browser. Eg. IE5+ display the XML document in a tree structure, where you can click on the + and - to browse the tree. Mozilla-based browsers display the default XSLT transformation of the XML document, where only the text appears.
  4. To change the default display option of web browsers, you can include a style-sheet processing instruction in the XML document. The file course-ss.xml is an example of a document containing such a processing instruction. Using a text editor, open course-ss.xml. Replace "mystylesheet.xsl" with the style-sheet file you want to use (eg. "tohtml.xsl"). Now load course-ss.xml using the web browser again. You will see the result of the XML document transformed by the XSLT style sheet, but now properly rendered by the browser if it is HTML.
  5. Repeat step 4 for each of the example style sheets available.

Exercises:

  1. Write an XSLT style sheet that converts course.xml to an HTML document, with the following format:
<HTML>
<BODY>
<H3>Bachelor of Science - Internet Computing</H3>
<H3>3 years</H3>
<P>
The unit ICT336 Internet Systems Programming is run by: <br> Hiew, Hong Liang (h.hiew@murdoch.edu.au)<br>
</P>
<P>
The unit ICT108 Introduction to Multimedia and the Internet is run by: <br> Rai, Shri (s.rai@murdoch.edu.au)<br>
</P>
</BODY>
</HTML>

Don't worry if you do not get the newlines and indentations exactly as above. The important thing to get right is the structure of the the HTML.

  1. Make course.xml publically available online using your Apache web server. When someone accesses the document, they should see the HTML document in exercise 1, properly rendered by their browser.
  2. Write XSLT style sheet that extracts the unit code (NOT including the unit name) of every unit. Does the structure of the document make it obvious where this piece of information is? Would it be appropriate to change the DTD of course.xml to support this operation? If yes, make the relevant changes and implement a new style sheet?
  3. Write XSLT style sheet that displays the number of units in a course XML file (Hint: read the lecture notes on XPath functions, and look up the the available functions in the textbook. The function sum() is NOT the answer).
  4. The following is an example DTD for a new XML application (ie. a new language). It describes pages on a brochure for course information.Write an XSLT style sheet that transform XML documents of the course DTD to a documents in this new brochure DTD. Try it with the given course.xml document.

    <!ELEMENT brochure (page*)>
    <!ELEMENT page (header?, footer?, courseinfo+) >
    <!ELEMENT header (#PCDATA) >
    <!ELEMENT footer (#PCDATA) >
    <!ELEMENT courseinfo (#PCDATA) >

Assessable Tasks:

Exercises 1, 2 and 3.

Internal students should demonstrate to your tutor by executing your scripts with your tutor present. This must be done in week 7, 8 or 9. No marks will be awarded if the work is demonstrated after that - your tutor have no discretionary power on this deadline.

External students should submit the scripts and sample runs during the deadline as indicated in the Study Schedule.

H.L. Hiew
Unit Coordinator


Document author: H.L. Hiew, Unit Coordinator
Last Modified: Wednesday, 31-Mar-2004 00:55:52 MST
Disclaimer & Copyright Notice © 2004 Murdoch University
This document is relevant for semester 1, 2004 only