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 |
---|---|---|
comment() |
True for a comment node. | following::comment() selects all the comment nodes that appear after the context node. |
node() |
True for a node of any type. | preceding::node() selects all the nodes that appear before the context node. |
processing-instruction() |
True for a processing instruction node. | self::processing instruction() selects all the processing instruction nodes within the context node. |
text() |
True for a text node. | child::text() selects the text nodes that are children of the context 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.
Node Tests | Name Tests | Targeted Processing Instruction Tests