16.8k views
3 votes
Consider the following XML document, loaners.xml:

(a) Write an XPath expression that return all of the name (name elements) in loaners.xml. Emphasis is on if the expression also works on other, similar, XML documents.
(b) Write an XPath expression that return all the names of followers, who have (had) at least one loan, which is to say, where there is a loan element. Emphasis is on if the expression also works on other, similar, XML documents.

User Darscan
by
7.5k points

1 Answer

4 votes

Final answer:

To fetch all name elements in an XML document, use the XPath '//name'. To get names of followers with at least one loan, use '//follower[loan]/name'. Both expressions are versatile for similar document structures.

Step-by-step explanation:

To address your queries about XPath expressions:

a) To return all of the name elements in loaners.xml, the XPath expression you would use is //name. This works for any XML document because the '//' selects nodes in the document from the current node that match the selection no matter where they are.

b) To find the names of all followers who have had at least one loan (implying they have a loan element), the XPath would be //follower[loan]/name. This expression looks for any follower element that contains at least one loan child element and then selects the name child element of those followers. Similar to the above, this XPath should work on any XML document structured in this fashion.

User Molbal
by
7.7k points