Obtaining Particular Types of Nodes By Using Node Tests

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 101. Node tests unique to XPath 2.0 are shown in Table 101.

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:


About the Document Object

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:

Getting Nodes of a Particular Type

A query like the following returns all the comments in a document:


The following query returns the third comment in the document.

(//comment()) [3]  

Free Stylus Studio XML Training: