Laboratory Week 7
Learning Objective:
- Learn how to use XSLT to transform an XML document.
- Learn some example XML transformations.
Required Reading:
Access to Software:
All required software is available in lab SC2.31.
External students may download the required software and install it on your own machine. Internal students may do the same, but are encouraged to still attend labs for supervision when doing the exercises below.
In a few of of the following exercises, we will transform XML documents with style sheets using James Clark's XT program, a standalone Win32 program implemented in Java. This is actually part of a full distribution containing an XSLT API which can be used in writing XML Java programs. See the downloads page for the link to the XT distribution.
Instructions and Exercises:
Copy all the example XML adn XSL files from the examples directory into your local or network drive. Read the files using a text editor and understand what each of the XSL style sheet does:
- default.xsl: default transformation.
- author1.xsl: extracting all poem authors' names.
- author2.xsl: extracting all author names - a much better example.
- anotherxml.xsl: transforming to another XML format.
- tohtml.xsl: transforming to HTML.
nodes.xsl: Printing the name of every node in the source tree.
For each of the XSL style sheets, see the result of the transformation by using the XT program. Do this by starting a MS-DOS prompt and copying the xt.exe program to the same directory as where your files are (eg. C:\TEMP). Then execute the transformation by typing "xt xml_file xsl_file". Egs.
C:\TEMP> xt poetry.xml author1.xsl
C:\TEMP> xt poetry.xml default.xsl | more
Try writing your own transformation style sheets. When looking at the results of your transformations, do not worry about about inexplicable indentations. XT will generate such indentations for "text" output. For "xml" output, you can turn these indentation on or off - see example in anotherxml.xsl.
- Exporting to MS-Office documents: Write XSLT style sheet that extracts the title and author information to .CSV (comma separated file). You can import that file into MS-Office and any other applications which accepts .CSV files. This is an example of converting from XML to a specific format acceptable by a particular application.
A CSV file is a text file with fields in each row separated by commas. Your result from the transformation should look like this:
The SICK ROSE, William Blake
Song of the Open Road, Ogden Nash
Try redirecting your output to a file (eg result.csv below) and opening the file using EXCEL.
C:\TEMP> xt poetry.xml xml2csv.xsl > result.csv
- Inappropriate document structure: Write XSLT style sheet that extracts the first line of every poem (NOT the first line of every stanza). Does the structure of the document make it obvious where this piece of information is? Would it be appropriate to change the DTD of poetry.xml to support this operation? If yes, make the relevant changes and implement a new style sheet?
- Write XSLT style sheet that displays the number of poems in the document (Hint 1: read the lecture notes on XPath functions, and look up the the available functions in the unit reader. The function sum() is NOT the answer).
- The following is an example DTD for a new XML application (ie. a new language). Write an XSLT style sheet that transform from the poetry DTD to this new DTD.
<!DOCTYPE book [
<!ELEMENT book (page*)>
<!ELEMENT page (header?,footer?,content+)>
<!ELEMENT header (#PCDATA) >
<!ELEMENT footer (#PCDATA) >
<!ELEMENT content (#PCDATA) >
]>
Document author: H.L.
Hiew, Unit Coordinator
Last Modified: Monday, 09-Apr-2001 09:14:09 MST
Disclaimer & Copyright Notice © 2001 Murdoch
University