The most basic of XPath patterns is the pattern that references the
current node, which consists of a period sign:
.
A period will obtain the current node.when you�re traversing a document tree.
Thus current node pattern is therefore a relative pattern because it makes sense
only in the context of a tree of data. As a contrast to the current pattern,
which is relative, consider the pattern that is used to select the root node of
a document. This pattern is called as root pattern and consists of a single
forward slash:
/
If you are using a single forward slash in an expression for the training
log sample document, it would refer to the trainlog element
because this element is the root element of the document. Because the root
pattern directly references a specific location in a document (the root node),
it is also considered an absolute pattern. The root pattern is extremely important to
XPath because it represents the starting point of the document�s node tree.
If there are child nodes there must also be parent nodes. To access a parent
node, you must use two periods:
..
As an example, if the current context is one of the distance
elements then in the training log document, the .. parent
pattern will reference the parent of the node, which is a session
element. You can put patterns together to get more interesting
results. e.g. to address a sibling node, you must first go to the parent
and then references the sibling as a child. In other words, you use the parent
pattern (..) followed by a forward slash (/) followed by the sibling name,
like this:
../duration
This pattern assumes that the context is one of the child elements of the
session element (other than duration ). Assuming this
context, the ../duration pattern will reference the duration
element as a sibling node.
Thus far I�ve focused on referencing individual nodes. However, it�s also
possible to select multiple nodes. For example, you can select all of the child
nodes (descendants) of a given node using the double slash pattern:
//
As an example, if the context is one of the session elements in
the training log document.e.g. you can select all of its
child nodes by using double slashes. This results in the duration .
, distance , location , and
comments elements being selected.
Another way to select multiple nodes is to use the wildcard pattern, which is
an asterisk:
*
The wildcard pattern selects all of the nodes in a given context. So, if the
context was a session element and you used the pattern
*/distance , all of the distance elements in the document
would be selected. This occurs because the wildcard pattern first results in all
of the sibling session elements being selected, after which the
selection is limited to the child distance elements.
To summarize, following are the primary building blocks used to reference
nodes in XPath:
- Current node�
.
- Root node�
/
- Parent node�
..
- Child node�
Child
- Sibling node�
/Sibling
- All child nodes�
//
- All nodes�
*
|