Location paths
A location path is a formulation which allows the access and selection of certain nodes or node sets within a XML document. Location paths
- can have a short or a detailed notation
- can be relative or absolute.
Short and detailed syntax
A path expression in XPath can have a detailed or a short notation. Whereas the detailed syntax is used for nodes and axes being rarely addressed, there is a short notation for often selected nodes and axes. The name "child::" for the child axis can in principle be omitted since it is considered as standard axis.
An example for the detailed notation:
/child::UNIVERSITY/child::PHILOSOPHY/child::STUDENT-NUMBER/attribute::UNIT/
The short notation:
/UNIVERSITY/PHILOSOPHY/STUDENT-NUMBER/@UNIT
When using the short notation, the name of the child axis is omitted and the attribute note is abbreviated by the @ character.
Comparison of the detailed and the short syntax:
axis/node | detailed notation | short notation |
node itself | self::node() | . |
children | child:: | standard axis (default), the indication can be left out |
descendants | descendant-or-self::node()/ | // |
parents | parent::node() | .. |
attributes | attribute:: | @ |
Absolute and relative paths
A path in XPath may be relative or absolute. Whereas relative paths start from the (current) context node, an absolute location path origins from the root node. A location path consists of several location steps which are separated by slashes. A location step contains the indication of an axis, the indication of a node test and one (or several) predicate(s), whereby the indication of predicates is optional.
/child::UNIVERSITY/child::POLITICS/child::DEAN
The first slash marks the root node. From there, the <UNIVERSITY> root element is selected which must have a <POLITICS> child element which, in turn, must have a <DEAN> child element in order to get a match. The axes are indicated along with their names followed by two colons.
In short notation, the path is as follows:
/UNIVERSITY/POLITICS/DEAN
Relative paths begin with a double slash and make a search in the entire document possible:
//child::POLITICS/child::DEAN
Now, the entire document is searched for the <POLITICS> element and its <DEAN> child element. In case this combination can be found in the document, a match occurs.
<< back | next >> |