XPath text(), comment(), and processing-instruction() Guide

In XML XPath, node-test functions are specialized selectors used within location steps to target non-element and non-attribute nodes directly. This article explains how the text(), comment(), and processing-instruction() node-test functions operate, detailing their syntax, matching mechanics, and practical applications for extracting specific nodes from an XML document.

The text() Node Test

The text() node test matches any text node child of the context node. In XML, plain character data contained inside elements is represented as distinct text nodes rather than element nodes.

The comment() Node Test

The comment() node test matches XML comment nodes within the document hierarchy. XML comments are structured as <!-- comment content -->.

The processing-instruction() Node Test

The processing-instruction() node test matches processing instruction (PI) nodes in the XML document. Processing instructions take the format <?target data?> and provide instructions to downstream processing applications.

Summary of Behavioral Differences