• Head of all articles between the 4th and the 9th of May
//article[date[day >=4 and day <=9][month=5]]/head
• The first and the third article of Alliance & Leicester
//article[source="Alliance & Leicester"][position()=1 or position()=3]
• The depth of the XML document (Hint: XPath2 usage)
Note: Since the definition for the depth of XML document was not explicitly given, I
assume the depth is counted by maximum of depth of the element nodes.
max(//*/count(ancestor::*))
• Explain why the query //head[2] returns a different result to
descendant::head[2]
//head[2] returns a set of element, in which each element is a second head element
in a particular descendant set.
descendant::head[2] returns a second head element of a set which contains all
head element in the document.
• The results look like for this particular example file
//head[2] returns no result
descendant::head[2] returns:
ISA Savers Could Lose Tax Relief
Get the maximum and minimum of the values in values.xml, using XPath1
(trick: use negation of existential comparison)
Get maximum value:
/doc/value[not(. < //value)]
Get minimum value:
/doc/value[not(. > //value)]
• Get the maximum and minimum of the values in values.xml, using XPath2
Get maximum value:
/doc/max(value)
Get minimum value:
/doc/min(value)
• Write an XPath2 query that iterates over the numbers 1 to 12 and returns
the square product of each number
for $x in 1 to 12 return $x*$x
• Return the concatenated textual value of all the child elements of the element data
in text.xml
string(/root/data)