Java Reference
In-Depth Information
Discussion
This discussion offers some further characterization of XPath for readers who may be unfa-
miliar with it, and it gives more detail on how to create advanced XPath expressions.
In the solution example, you were primarily interested in elements and attribute values. In ad-
dition to these, XPath is capable of addressing the following document nodes:
▪ Root
▪ Text
▪ Comment
▪ Processing instruction
▪ Namespace
The name XPath is appropriate for what XPath does: it defines an expression language that
allows you to create a path toward any set of nodes within the tree structure of an XML docu-
ment. Using an XPath expression, you can yield the following types of results:
▪ Node set (an unordered set of distinct nodes that match the expression)
▪ String
▪ Boolean
▪ Floating-point number
XPath defines some basic navigational building blocks that you can combine to create address
expressions. Table 3-1 illuminates the most basic selectors.
Table3-1.XPath selectors
/
When this is the first character in an expression, it selects the root node, which is the parent of the docu-
ment element. In subsequent placements, it acts as an element separator.
.
Selects the current node.
..
Selects the parent of the current node.
@ Selects one or more attributes.
// Selects all following elements, regardless of their location in the document hierarchy. When used within
a path, indicates elements that are direct or indirect descendants of the previously specified element.
XPath also provides a collection of operators that act just as you might hope they would
( Table 3-2 ).
Search WWH ::




Custom Search