Home > Online Product Documentation > Table of Contents > Finding a Particular Node
To find a specific node within a set of nodes, enclose an integer within brackets ( [ ] ). The integer indicates the position of the node relative to its parent. This section discusses the following topics:
See also Obtaining the Current Node for the Current XSLT Template.
The node positions for a node set start with
1. Evaluation of the position number is always based on document order. For example, the following query returns the first
author element in the current context:
The next query finds the
author elements (in the current context) that contain a
first-name element. The query returns the third such
author element.
When you specify an integer in brackets, it is equivalent to calling the
position() function. For example, the following queries both return the third
y child element of each
x child element in the current context:
Tip: If you do not know the position of the node you want, a call to the
position() function might help you. See
Determining the Position Number of a Node.
The return value of the
position() function depends on the specified axis. For example, suppose the axis is one of the reverse axes, such as
preceding,
ancestor, or
preceding-sibling. The
position() function returns the nth one in reverse document order that falls in the specified axis.
The
position() function returns an integer that indicates the position of the node within the parent. Positions start with
1; a node with a position of
1 is always the first node in the collection.
For example, the following query returns the first three
degree elements in the document:
The next query finds the first two book children in the current context:
The XPath processor executes the
position() function with respect to the parent node. Consider the following data:
The following expression returns the first
y element contained in each
x element:
For more information, see also Finding an Element with a Particular ID.
Positions are relative to the parent. Consider the following data, which has line numbers on the left for explanation only.
The following query returns the first
y element contained in each
x element. It returns the elements on lines 6 and 10. The XPath processor finds all
x elements. For each
x element, the XPath processor then returns the first
y element it finds.
The next query returns the first
y element that is contained in an
x element that is in the context node set. It returns the element on line 6. The XPath processor finds all
y elements inside
x elements. The XPath processor then returns the first element in that set.
The next query returns the empty set. The XPath processor finds the first
x element. It then searches that first
x element for the first
y. Because the first
x element does not contain a
y element, this query returns the empty set.
To obtain nodes relative to the last node in the set, use the
position()
and
last()
functions with arithmetic. For example, the following queries both obtain the last
author element in the current context:
The following queries both return the next-to-last
author element:
For information about
position(), see
Determining the Position Number of a Node. For information about
last(), see
Determining the Context Size.
To obtain several nodes in one operation, use the
and or the
or operator with the
position() and
last() functions. For example, the following query returns the first and the last
author nodes in the current context:
You can also specify a range of nodes. For example, the next query returns the second, third, and fourth
author elements:
To obtain a range of nodes, m to n, relative to the last node, use the following format:
For example, the following query obtains the last five nodes in the current context:
The following query finds the first and fourth
author elements:
The next query finds the first, the third through the fifth, and the last
author elements:
The XPath processor removes duplicate values. For the previous query, if there are only five elements in the collection, the query returns only one copy of the fifth element.
The next example finds all
author elements in which the first
degree is a Ph.D.:
Suppose you want to obtain from a collection the first node that meets a certain condition. For example, you want the first book whose author's last name is Bob. You can specify the following query:
When the XPath processor evaluates this expression, it creates a collection of
book elements where the author's last name is Bob. The XPath processor then returns the first node in this collection.
The following two expressions appear to also return the first
book whose author's last name is Bob, but they do not. Instead, these queries both return a book whose author's last name is Bob only if that book is the first book in the document.
To obtain the element that has a particular identifier (ID), the DTD must specify an attribute for that element. The type of this attribute must be
ID. The name of the attribute is not significant, though it is typically
id. If there is such an attribute, you can call the
id() function to obtain the element with a particular ID. The format is
The
id() function evaluates to a set. It ignores the context node set except to evaluate the function's argument. The result set contains an element node that has an attribute of type
ID whose value is identical to the string the object argument evaluates to. The element node can appear anywhere in the document that is being queried.
For example:
This query searches for an element that has an attribute whose
Details about working with IDs are in the following topics:
When the
id() function's argument is of type
node-set, the result is the union of the results of applying
id() to the string value of each of the nodes in the argument node set.
When the argument of
id() is any other type, the XPath processor converts the argument to a string as if by a call to the
string() function. The XPath processor splits the string into a white-space-separated list of tokens. The result is a node set that contains the elements in the same document as the context node that have a unique ID equal to any of the tokens in the list.
An element node can have a unique ID. This is the value of the attribute that is declared in the DTD as type
ID. No two elements in a document can have the same unique ID. If an XML processor reports two elements in a document as having the same unique ID (which is possible only if the document is invalid), the second element is treated as not having a unique ID.
If a document does not have a DTD, the
id() function always returns an empty node list.
The node tests allow you to obtain nodes according to their type. Node test is an XPath term. Although a node test looks like a function, it is not a function.
You can use node tests with filters and position specifiers. They resolve to the set of children of the context node that meets the restrictions you specify.
Node tests for XPath 2.0 add to the set of node tests supported for XPath 1.0. Node tests common to both XPath 1.0 and XPath 2.0 are shown in Table 65. Node tests unique to XPath 2.0 are shown in Table 65.
|
Node Test
|
Node Type Returned
|
|---|---|
|
Matches any attribute node.
|
|
|
Matches any document node.
|
|
|
Matches any element node.
|
|
|
Matches any single item.
|
For each
p element in the current context, the following query returns its second
text child node:
Following is a query that finds the third
comment child in each
foo element anywhere in the document:
In the Document Object Model (DOM), a document contains comments, processing instructions, and declarations, as well as the document element. As in XPath, the root node is the root of the DOM tree, and the root node is not the same as the document element. This allows comments, declarations, and processing instructions at the document entity level.
For example, the following query finds all comments at the document entity level. In other words, it finds all comments that are immediate children of the root node.
This query returns the comment at the beginning of the
bookstore.xml file:
A query like the following returns all the comments in a document:
The following query returns the third comment in the document.