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.
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.
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..//@foo
/foo/bar/../baz[7]
(//rcp:ingredient)[40] eq (//rcp:ingredient)[53]
(//rcp:ingredient)[40] = (//rcp:ingredient)[53]
(//rcp:ingredient)[40] is (//rcp:ingredient)[53]