A name test is the most common form of a node test since it allows you to explicitly specify the name of the nodes that you want to select. However, just specifying a node's name does not automatically result in the selection of the appropriate node since a document tree may have different node types with the same name. To select the appropriate node, a name test selects only those nodes whose name matches the specified name and whose node type matches the principal node type of the specified axis.
The following table lists the principal node type for the various axes.
Axis | Principal node type |
---|---|
Any axis other than an attribute:: axis or the namespace:: axis (that is, an axis that can contain elements) |
element |
attribute | attribute |
namespace | namespace |
The name specified in a name test may be one of three types: an asterisk (*), a QName, or the expression, NCName:*. The following table shows examples of how each of these types of names works in conjunction with the specified axis to locate a particular node-set.
Name | Returns | Example |
---|---|---|
* (asterisk) | True for any node of the principal node type. | ancestor::* selects all ancestors of the context node.
|
QName | True for any node of the principal node type whose expanded name equals the expanded name specified by the QName. | child::para selects all the <para> elements nodes that are children of the context node. If the context node has no <para> children, selects an empty set of nodes. |
NCName:* | True for any node of the principal type whose expanded name has the namespace URI to which the NCName expands, regardless of the local name. |
When the node test is a QName, XPath must first expand the QName based upon the context namespace declarations in the XML file. This expansion process is identical to the expansion process for element names in start and end tags, except that it does not use the default namespace declared with xmlns
. If the QName does not have a prefix, XPath looks for those nodes whose local names match the given QName and whose namespace URI is null. A QName causes an error if the QName's prefix does not correspond to any of the context namespace declarations in the file.
When the node test is NCName:*, XPath expands the NCName in the same way as it would expand the prefix of a QName. Similarly, an NCName:* causes an error if the NCName part does not correspond to any of prefixes contained in the context namespace declarations in the file.
Node Tests | Node Type Tests | Targeted Processing Instruction Tests