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

Node Test
Node Type Returned
comment()
               

            
Comment nodes.
node()
               

            
All nonattribute nodes.
processing-
instruction("name") 
               

            
Processing instruction nodes. The processing-instruction() node test selects all processing instructions. When this node test specifies a literal argument, it selects any processing instruction that has a name equal to the value of the argument. If there is no argument, this node test selects all processing instructions.
text()
               

            
Text nodes and CDATA nodes.
Table 58. Node Test Return Values Common to XPath 1.0 and XPath 2.0

Node Test
Node Type Returned
attribute()
               

            
Matches any attribute node.
document-node()
               

            
Matches any document node.
element()
               

            
Matches any element node.
item()
               

            
Matches any single item.
Table 59. Node Test Return Values Unique to XPath 2.0

For each p element in the current context, the following query returns its second text child node:

p/text()[2] 
               

            

Following is a query that finds the third comment child in each foo element anywhere in the document:

//foo/comment()[3]
               

            

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.

/comment() 
               

            

This query returns the comment at the beginning of the bookstore.xml file:

"This file represents a fragment of a book store inventory database."

Getting Nodes of a Particular Type

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

//comment()
               

            

The following query returns the third comment in the document.

(//comment()) [3] 
               

            

OASIS Catalogs for Fun and Profit

This tutorial about, Understanding Catalogs, A Use-Case for Catalogs, Registering Catalogs in a Project, Using the DOCTYPE Syntax Sense:X Aid, Navigating Catalogs, Building Composite Catalogs, Converting TR9401 Catalogs to OASIS XML Catalogs and more

Validating EDIFACT Documents with XML Schemas

Either before or after transforming EDIFACT content, generated XML Schemas will make sure that we have all of the necessary components, our codes are all according to the standard codelists, and every little delimiter is in the right place.

DTD Generator

Stylus Studio's DTD Generator supports the automatic generation and association of both internal and external DTDs based on XML instance documents.

XML Spell Checker

The Stylus Studio Spell Checker is integrated with our intuitive XML Text Editor. It highlights typographical errors as you type and lets you easily search, replace, and build custom dictionaries.

Stylus Most Wanted

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.