Downloading exercise files
Download the source code of the .
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 thetransforming process in each example (Any doubts? Ask questions!)
- java -jar saxon9he.jaredmond.xml edmond.xsl > out.html
- java -jar saxon9he.jar books.xml BIBstyle.xsl> out.html (Note: BIBstyle uses for-each element)
- java -jar saxon9he.jar books.xml BIBstyle2.xsl> out.html (Note: BIBstyle2 uses apply-templates element)
- java -jar saxon9he.jar gem.xml MyFriends.xsl> out.html
- java -jar saxon9he.jar gem.xml GEMholiday.xsl> out.html
- java -jar saxon9he.jar scorers.xml gtimes.xsl> out.html
- java -jar saxon9he.jar scorers.xml gcount.xsl> out.html
- java -jar saxon9he.jar Phonebook.xml p2p.xsl> out.xml
- java -jar saxon9he.jar sdb.xml q1.xsl> out.xml
- java -jar saxon9he.jar sdb.xml q1a.xsl> out.xml (Note: the one with weight more than 25)
- java -jar saxon9he.jar sdb.xml q2.xsl> out.xml
- java -jar saxon9he.jar sdb.xml q3.xsl> out.xml
- java -jar saxon9he.jar sdb.xml q4.xsl> out.xml
- java -jar saxon9he.jar sdb.xml q5.xsl> out.xml
- java -jar saxon9he.jar sdb.xml q6.xsl> out.xml
Developing your own XSLT program
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:
Sorting XSLT output
In this section, we will examine the ability to sort the output produced aspart of an XSL transformation.
- 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.
- Try aussort1.xsl via:
java -jar saxon9he.jar aus.xml aussort1.xsl > out.html - Examine the output file. You should see a list of all divisions in descending order of area (in square kilometres).
- 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')
- In a new browser window, open this . You should then scroll down to xsl:sort.Read up about the sort options in XSLT.
- 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 resultfrom 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.
- Try the following:
java -jar saxon9he.jar aus.xml ausparam1.xsl short=QLD >out.html - Examine the output file. You should see information specific to Queensland.
- Open the file ausparam1.xsl file. You should make sure you understand the following features of the program:
- The declaration of the parameter "short".
- The use of the parameter in the form $short in the <H2> element.
- The use of the expression division/name[@abbr=$short] to ensure thatthe 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.
- The consequent appearance of match="name" to catch these elements.
- The use of select="." to display the name element.
- 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.
- Try the following:
java -jar saxon9he.jar aus.xml auskey1.xsl short=QLD >out.html - Examine the output file. You should see information specific to Queensland, just as we didwith the previous program.
- Open the file auskey1.xsl file. You should make sure you understand the following features of the program:
- 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 nameelements by means of this structure; and (3) the use="@abbr", which indicatesthat we want to access these elements by means of their abbreviation.
- 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.
Saxon - XQuery Processor
- Create a directory called saxon-xq.
- Here is a local copy of the of the SAXON XQuery processor with some XQuery sample queries and XML documents.
- Unzip the file into the 'saxon-xq' directory you have created.
- Test the examples and examine the output files using the following command
java -cp saxon9he.jar net.sf.saxon.Query -q:$1 -o:$2
where variable 1 represents the query file name ("sample files with extension .xq") and variable 2 represent the output file name.