What is the purpose of XPath in XML?
C
To navigate and query XML nodes
D
To convert XML into JSON
Analysis & Theory
**XPath** is used to **navigate** through elements and attributes in an XML document.
Which of the following XPath expressions selects all `<book>` elements in an XML document?
Analysis & Theory
`//book` selects **all `<book>` elements** anywhere in the XML document.
What does the XPath expression `/library/book[1]` select?
A
All books in the library
B
The first `<book>` element inside `<library>`
D
The second book in the document
Analysis & Theory
`/library/book[1]` selects the **first `<book>` child** of `<library>`.
Which XPath expression selects all `<title>` nodes under `<book>` elements?
Analysis & Theory
`//book/title` selects **all `<title>` nodes** that are direct children of `<book>` elements.
What does `@` represent in XPath?
Analysis & Theory
`@` is used to **select attributes**, e.g., `@id` selects the `id` attribute.
What will the XPath `//book[@category='fiction']` select?
A
All books with any attribute
B
All books under the 'fiction' node
C
All `<book>` elements with `category='fiction'`
D
All elements with fiction text
Analysis & Theory
This selects all `<book>` elements where the `category` attribute is equal to `'fiction'`.
Which XPath selects the root element of an XML document?
Analysis & Theory
`/` refers to the **root** of the XML document.
What does the expression `//book/title/text()` return?
B
Text content of all `<title>` elements inside `<book>`
C
All book titles as elements
Analysis & Theory
`text()` gets the **text content** of the selected element.
Which XPath axis is used to select all ancestors of a node?
Analysis & Theory
`ancestor::` selects **all ancestors** (parents, grandparents, etc.) of a node.
What does `//book[position()<3]` select?
A
Books with ID less than 3
B
All books except the third one
C
The first two `<book>` elements
Analysis & Theory
`position()<3` selects **the first two** `<book>` elements in the matched list.