What Is node() vs text() in XSLT Pattern Matching?

In XSLT and XPath, node() and text() are both node test functions used to select or match components of an XML document tree, but they target different structural scopes. While text() matches only textual character data within elements, node() matches any node type including elements, text nodes, comments, and processing instructions, excluding only attributes and the root document node itself unless explicitly targeted.

The Scope of node()

The node() test is a broad selector that matches any child node of the current context regardless of its specific type. In an XML tree structure, child nodes include:

When used in template matching, <xsl:template match="node()"> applies to every child node type encountered during traversal. It is frequently employed in identity transforms to match and copy all structural elements and their contents. Notably, node() does not match attribute nodes (@*) or namespace nodes, as attributes are not considered child nodes in the XPath data model.

The Scope of text()

The text() test is a specialized node test designed exclusively to match text nodes. Text nodes represent the character data enclosed by elements, including meaningful string content, whitespace, newlines, and entity references evaluated into plain text.

When written as <xsl:template match="text()">, the template triggers only when processing raw character data. It ignores surrounding wrapper elements, child elements, comments, and processing instructions.

Key Behavioral Differences

The primary distinction between node() and text() lies in specificity and structural granularity:

Choosing between node() and text() depends on whether the transformation requires preserving structural XML markup and metadata or extracting and modifying raw text values alone.