XML technologies / XPath / XPath 2.0 – Language reference / Path expressions: location steps / Testing node kinds: KindTest

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 4.)

An alternative tests a node by its membership in a node kind. This is called a KindTest. Besides node kind, a KindTest can check type annotations, document-hierarchy position, content model or name, overlapping somewhat with name tests.

A pure KindTest checks only a node’s kind. For unnamed nodes, no other node test can be formulated and the target set cannot be restricted further directly. Such tests therefore always select all matching nodes on the axis.

Looking back at XPath 1.0:
XPath 2.0 substantially extends KindTests compared with XPath 1.0 by adding tests for attribute, document and element nodes. Other KindTests remain largely unchanged.

General kind test: all node kinds

To select all nodes on an axis without distinguishing node kinds, use this general KindTest:

  • node()
    matches every node on the axis regardless of kind. This KindTest is identical in XPath 2.0 and 1.0.

Kind test for text nodes

Text nodes have no names or other features distinguishable by node tests. They can only be selected collectively:

  • text()
    matches all text nodes on the axis. This KindTest is identical in XPath 2.0 and 1.0.

Kind test for comment nodes

Comment nodes also have no names and can only be selected collectively:

  • comment()
    matches all comment nodes on the axis. This KindTest is identical in XPath 2.0 and 1.0.

Kind test for processing-instruction nodes

Processing-instruction nodes can be selected collectively by kind or by their name, interpreted as the target application’s name. The corresponding KindTest therefore has two forms.

  • General form:

    • processing-instruction()
      matches all processing-instruction nodes. Its general form is identical in XPath 2.0 and 1.0.

  • Restricting target names:

    • processing-instruction(NCName)
      matches processing-instruction nodes whose target is identified by the supplied NCName (non colonized name). Passing the NCName directly without string delimiters is new in XPath 2.0.

Looking back at XPath 1.0:
For backward compatibility with XPath 1.0, form processing-instruction('NCName') remains permitted, passing the name as a string with string delimiters.

Kind test for document nodes

XPath 2.0 provides a dedicated document-node test, in a general form or with a content model described by a sequence expression:

  • General form:

    • document-node()
      matches every document node.

  • Restricting the content model:

    • document-node(elementtest)
      A document-node KindTest can contain a sequence expression in the form of an element-node KindTest describing the document node’s content. For example:

    • document-node(element(buch)) 
      matches a document node whose child matches element test element(buch), in other words an XML document whose root element is named <buch>.

Any number of comment and processing-instruction nodes may accompany the root element described by the sequence expression without causing this test to fail.

Looking back at XPath 1.0:
XPath 1.0 has no KindTest for document nodes.

Kind test for element nodes

XPath 2.0 also provides an element-node KindTest. Besides its general form, it can specify a name, a type-annotation name or a type-structure description, sometimes allowing wildcards instead of concrete values.

  • General form:

    • element()
      matches all element nodes on the axis. It corresponds to a NameTest on an element axis with wildcard name *. The element node’s schema type is not considered.

  • Restricting by name:

    • element(bezeichner)
      A QName can be supplied as the name, selecting only element nodes with that QName.

    • element(*)
      With a wildcard instead of a concrete name, the KindTest is equivalent to general form element().

  • Restricting by name and type annotation:

    • element(bezeichner, typangabe)
      A schema-type check can specify a concrete type name that must be valid in the static context.

      In this form, however, the test fails for element nodes with the supplied name and type if the element instance has the null attribute xsi:nil="true".

      The schema declaration must permit this attribute with nillable="true". It is used on an element instance that is empty contrary to its content model, which would otherwise be forbidden.

    • element(bezeichner, typangabe ?)
      To match an element instance even with nilled content, append cardinality indicator ? after the schema-type name. An element then matches even with the nillable attribute set, though its schema declaration must permit nilled content as above.

    • element(*, typangabe)
      A wildcard can replace the name’s QName. The test then matches all elements of the specified schema type, unless nilled.

    • element(*, typangabe ?)
      A wildcard can replace the name’s QName. The test then matches all elements of the specified schema type, even if nilled through xsi:nil="true".

    • element(bezeichner, *)
      This test uses a schema-type wildcard and imposes no further type requirements. It therefore corresponds to element test element(bezeichner). The name must be a QName.

    • element(*,*)
      With wildcards in both positions, as here, this test is equivalent to general form element().

Looking back at XPath 1.0:
XPath 1.0 has no element-node KindTest, always using a NameTest instead. It cannot additionally check element-node type annotations.

Schema test for element nodes: Schema-Element Test

An element node can be tested against a schema declaration. If no matching declaration exists for the node, a static error is reported (err:XPST0008).

  • schema-element(bezeichner)
    The test succeeds only if an in-scope schema has a top-level declaration for the supplied element name, or declares it within a substitution group (substitution group). The node’s type must also match the declared type, and it must not be nilled unless the declaration permits that.

Kind test for attribute nodes

XPath 2.0 also provides an attribute-node KindTest, checking its name and/or type annotation against a schema declaration.

  • General form:

    • attribute()
      matches all attribute nodes on the axis regardless of name.

  • Restricting by name:

    • attribute(@bezeichner)
      A QName can be supplied to the KindTest as the name the tested attribute must match. Note that the QName must be preceded by the attribute axis’s @.

    • attribute(@*)
      Using a wildcard instead of the QName, again preceded by @, makes the test equivalent to the general attribute KindTest attribute().

  • Restricting by name and type annotation:

    • attribute(@bezeichner, typangabe)
      A schema-type check can specify a concrete type name valid in the static context. It must be a simple type (simple type). The QName is preceded by @.

    • attribute(@bezeichner, *)
      This test uses a schema-type wildcard and imposes no further type requirements. It therefore corresponds to attribute test attribute(@bezeichner).

    • attribute(@*, typangabe)
      A wildcard can replace the name while a schema type is supplied. It must again be a simple type (simple type). An attribute node is selected if its type matches the supplied type.

    • attribute(@*,*)
      If both QName and type name are wildcards, the KindTest is equivalent to general form attribute().

Looking back at XPath 1.0:
XPath 1.0 has no attribute-node KindTest, always using a name test on the attribute axis instead.

Schema test for attribute nodes: Schema-Attribute Test

Attribute nodes can also be tested against an in-scope schema using a Schema-Attribute Test. If no corresponding declaration is available, a static error is reported (err:XPST0008).

  • schema-attribute(bezeichner)
    An attribute with the supplied name must be declared in the available schemas, and the tested attribute node’s type must match or derive from the declared type.

   

back to top