CIS336 (also Semester 2 of CIS219, CIS221 and IT226)
Website design, implementation and management
Semester 2, 2006-7

Lectured by David Meredith


Exercises for Workshop 3 (assessed)

The following questions test your understanding of the XPath language. They are taken from pages 90-91 of Møller and Schwartzbach (2006). Answer all the questions and e-mail your answers to David Meredith at <dave@titanmusic.com>.

You must e-mail your answers to David Meredith by 18:00 on Thursday 22 February 2007. If you send your answers after this deadline, you will receive zero for this portion of the first coursework.


Using an XSLT processor to check your answers

An XPath 2.0 expression may be evaluated using an XSLT 2.0 processor such as Saxon-B 8.8.

First create a directory C:\saxon.

Then download the file saxon8-8j.zip and save the file in the directory C:\saxon. Extract all the files from this zip archive into the directory C:\saxon. The C:\saxon directory should now contain a file called saxon8.jar.

Then make an XSL stylesheet called xpath.xsl containing the following text:

<xsl:stylesheet 
	version="2.0"
	xmlns="http://www.brics.dk/ixwt/recipes"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
	xmlns:rcp="http://www.brics.dk/ixwt/recipes"
	xmlns:fn="http://www.w3.org/2005/xpath-functions"
	xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xsl:template match="/">
		<xsl:value-of select='foo'/>
	</xsl:template>
</xsl:stylesheet>

Save xpath.xsl in the C:\saxon directory.

Now download the file http://www.brics.dk/ixwt/examples/recipes.xml and save this file also in the directory C:\saxon.

Now to test that everything is working, replace the word foo in xpath.xsl with the XPath location path //rcp:ingredient/@amount. Then open the MS DOS command line and use the command cd C:\saxon to change the current directory to C:\saxon. Now type the command:

java -jar saxon8.jar -t recipes.xml xpath.xsl > recipes.html

Then open the file recipes.html in TextPad and view the result of the evaluation. For the expression above, the file recipes.html should contain the text

<?xml version="1.0" encoding="UTF-8"?>1.5 1 1 1 0.5 2 1 0.5 12 2 0.25 3 12 2 2 0.25 4 5 1 0.5 4 1 * 16 2 2 0.5 0.25 0.25 1 6 10 6.5 0.25 0.25 2.5 * * 4 2.5 21 0.75 1 * 0.5 * 15 3 1.5 0.75 3 * 1.5 0.5 4 * * 1.5 5 2 1 1 0.5 * 2 0.25 0.25 3 3 * * 0.25 0.25 0.25 0.5 3

The file xpath.xsl must contain a declaration for every namespace that is used in any XPath expression that you are evaluating.


Questions

  1. Consider the XML tree corresponding to the document http://www.brics.dk/ixwt/exercises/axes.xml. Assuming the element named foo is the current node, list, in the correct order, the nodes belonging to each of the following axes: (a) child, (b) descendant, (c) parent, (d) ancestor, (e) following-sibling, (f) preceding-sibling, (g) following, (h) preceding, (i) attribute, (j) self, (k) descendant-or-self and (l) ancestor-or-self.
  2. Write the unabbreviated versions of the following location paths:
    1. .//@foo
    2. /foo/bar/../baz[7]
  3. Using the recipes.xml file, which is available here, write an XPath expression that finds the number of eggs used in each recipe that also uses milk. (When the expression is evaluated, it should return a sequence of numbers.)
  4. Using the recipes.xml file, write an XPath expression that finds the total number of eggs used in all the recipes.
  5. Using the recipes.xml file, which of the following comparison expressions evaluate to true?
    1. (//rcp:ingredient)[40] eq (//rcp:ingredient)[53]
    2. (//rcp:ingredient)[40] = (//rcp:ingredient)[53]
    3. (//rcp:ingredient)[40] is (//rcp:ingredient)[53]
  6. Assume that $x contains a sequence of strings and that $y contains a single string. Using the XPath 2.0 function library, write expressions that compute the following results:
    1. Whether or not $y equals one of the strings in $x.
    2. The length of the largest string occurring in $x.
    3. Whether or not the concatenation of strings in $x forms a palindrome.
    4. How many of the strings occurring in $x appear as attribute values in the document whose URL is $y.