Navigation, Predicates and Functions in XPath

<strong>Write the following XPath queries for the document finance.xml: </strong> • head of all articles between the 4th and the 9th of May • The first and the third article of Alliance & Leicester • The depth of the XML document (Hint: XPath2 usage) • Explain why the query //head[2] returns a different result to descendant::head[2], and how the results look like for this particular example file </br> <p> </strong>Get the maximum and minimum of the values in values.xml </strong> • Try first to do it in XPath1 (trick: use negation of existential comparison) • Write an XPath2 query that iterates over the numbers 1 to 12 and returns the square product of each number • Return the concatenated textual value of all the child elements of the element data in text.xml. Further, remove all non-breakable spaces that are used in the concatenated text. And normalize/trim the white-spaces finally . </p> The files <cite>finance,values</cite> can be found in the attachment.
1 answer

Using XPath 1.0, XPath 2.0, and XSLT

• 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)