|
Home > Online Product Documentation > Table of Contents > Finding a Particular Node Finding a Particular NodeTo 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. About Node Positions
The node positions for a node set start with
The next query finds the
When you specify an integer in brackets, it is equivalent to calling the
Tip: If you do not know the position of the node you want, a call to the
The return value of the
Determining the Position Number of a Node
The
For example, the following query returns the first three
The next query finds the first two book children in the current context:
The XPath processor executes the
The following expression returns the first
For more information, see also Finding an Element with a Particular ID. Positions in Relation to Parent NodesPositions 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
The next query returns the first
The next query returns the empty set. The XPath processor finds the first
Finding Nodes Relative to the Last Node in a Set
To obtain nodes relative to the last node in the set, use the
The following queries both return the next-to-last
For information about
Finding Multiple Nodes
To obtain several nodes in one operation, use the
You can also specify a range of nodes. For example, the next query returns the second, third, and fourth
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:
Examples of Specifying Positions
The following query finds the first and fourth
The next query finds the first, the third through the fifth, and the last
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
Finding the First Node That Meets a ConditionSuppose 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
The following two expressions appear to also return the first
Finding an Element with a Particular ID
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
The
For example:
This query searches for an element that has an attribute whose Details about working with IDs are in the following topics: The id() Function's Argument
When the
When the argument of
Unique IDs
An element node can have a unique ID. This is the value of the attribute that is declared in the DTD as type
If a document does not have a DTD, the
Obtaining Particular Types of Nodes By Using Node TestsThe 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.
For each
Following is a query that finds the third
About the Document ObjectIn 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
Getting Nodes of a Particular TypeA query like the following returns all the comments in a document:
The following query returns the third comment in the document.
|

Cart

