Node Tests

XPath also has node tests. A node test looks like a function, but is used to match certain types of nodes. A node test works like a predicate in that it returns only nodes that meet certain criteria. XPath 1.0 has four node tests:

node()

Matches all nodes. The test node() is true for every kind of node.

text()

Matches text nodes only.

comment()

Matches comment nodes.

processing-instruction()

Matches processing instruction nodes. If this node test includes a string, it matches processing instruction nodes with that name. For example, processing-instruction('cocoon-process') matches processing instruction nodes that begin with <?cocoon-process>.

*

Matches all the nodes along a particular axis (we’ll cover axes shortly). For example, child::* matches all the element children of a node, whereas attribute::* matches all the attributes of a node.

NCName:*

Matches all the nodes in a particular namespace. In our sonnet, auth:* matches any element that has the namespace URI http://www.authors.com/.

[2.0] New node tests in XPath 2.0

XPath 2.0 uses all of the node tests from XPath 1.0. It also defines several new node tests:

element()

Matches any element. Using this node test without arguments is similar to select="*" in XPath 1.0. The difference is that in XPath 2.0, we can use the element() node test to check the name and datatype of an item. For example, element(author) matches any elements named <author>. The two-argument version of this node test allows us to find elements that match a particular ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.