Exercises

1. 

What is the difference between the following two XPath expressions?

   /root/nodeA/nodeB   

and:

   nodeA/nodeB   

image from book

2. 

Which of the following statements is the best answer for the following XPath expression:

   //country/../@name   
  1. Find all countries whose name node is not empty.

  2. Find parent nodes for all countries.

  3. Find all parents of all countries where the country node has a name attribute.

  4. Find nothing because no root node is specified.

  5. None of the above.

image from book

3. 

What is the best answer for what this XPath expression will find?

   demographics/region/country/city[last()-1]"/>   
  1. The first city for every country

  2. The last city for every country

  3. The second city for every country

  4. The second last city for every country

  5. None of the above

image from book

4. 

What will this XPath expression find?

   demographics/region/country[@*]   
  1. The entire contents of an XML document

  2. Only regions with a region id attribute of between 1 and 10

  3. Countries with a name beginning with the letter A

  4. All countries with at least 1 attribute

  5. None of the above.

image from book

5. 

Which of the following are possible answers, with respect to this XPath expression?

   //child::collectionofthings/../parentofcollectionofthings/bigthings/attribute::*   
  1. Get all attributes of a node called bigthings.

  2. Get all attributes of the parent node, of a node called collectionofthings.

  3. Get the node parentofcollectionofthings.

  4. Get the parent node of parentofcollectionofthings.

  5. Get zip!

image from book

Answers

1.  

Exercise 1 solution

The first expression is an absolute path and the second is a relative path. An absolute path scans from the specified node, generally the root. A relative path scans from the current path. The current path is the current point to where any processing has scanned to, within an XML document.

2.  

Exercise 2 solution

e. None of the above. The expression //country/../@name searches for country nodes anywhere in the document. Then the two dots move upward to the parent node (that could be a region node), where the parent node has an attribute called name .

3.  

Exercise 3 solution

d. The second last city for every country is the best answer because we are not specifically sure of the structure of the XML document. Assuming the demographics node is the root node, the XPath expression finds all cities, within countries, within regions. The predicate applied to the city finds the last city in the collection of cities, less 1 (the seconds last city).

4.  

Exercise 4 solution

d. All countries with at least 1 attribute. The entire contents will not be found because [@*] is a predicate, and any data outside of the path /demographics/region/country will be missed. Countries beginning with the letter A is completely nonsensical because nothing in the expression gets anywhere near specifying that particular restriction. The predicate [@*] implies all attributes within the country element, and thus at least 1 applies in this case.

5.  

Exercise 5 solution

a. Get all attributes of a node called bigthings is a good answer because the expression includes bigthings/attribute::* . Answer b is incorrect because parentofcollectionofthings is actually a sibling of collectionofthings (child finds the child of the former) there is no parent node in this picture at all. Answer c is incorrect because it contradicts the first two, and its simply wrong. The same applies to d. Answer e (Get zip!) implies get nothing. Actually this could be a good answer as well because you dont know the content of the XML document anyway.



Beginning XML Databases
Beginning XML Databases (Wrox Beginning Guides)
ISBN: 0471791202
EAN: 2147483647
Year: 2006
Pages: 183
Authors: Gavin Powell

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net