Node Type Tests
To select nodes types other than, or in addition to, elements nodes, use a node type test. The effect of using a node-type test is to override the principal node type for the given axis. For example, descendant::text()
locates all text nodes descended from the context node, even though the principal node type for the descendant axis is element.
There are four node type tests available, as shown in the following table.
Node type | Returns | Example |
---|---|---|
|
True for a comment node. |
|
|
True for a node of any type. |
|
|
True for a processing instruction node. |
|
|
True for a text node. |
|
Note
The processing-instruction()
test may have an argument that is literal; in this case, it is True for any processing instruction that has a name equal to the value of the literal. For more information, see Targeted Processing Instruction Tests.
The parentheses, ()
, at the end of the node type test are essential. Their presence eliminates any problems that may arise if the source document has nodes whose names are comment, text, processing-instruction, or node. When an XPath application encounters the double parentheses, it knows that this is a node type test, not a name test for nodes with those names. Similarly, node type tests are not XPath functions, references to which also contain parentheses, empty or otherwise.