Document structure
Text node
A run of character data in the tree, as a node in its own right — the model a query language sees rather than the markup you wrote.
Text nodes hold the characters between tags. How the characters were written — literally, as entity references, or in a CDATA section — makes no difference to the node that results, which is why a diff of two documents can report them identical when their bytes differ.
What does split text into separate nodes is anything that interrupts it: a child element, a comment, a processing instruction. That is why an element's text is not always one node, and why taking the first text node is a common bug.
- Defined in
- XPath 1.0 §5.7
- Category
- Document structure
- Also written
- character data node
Shown, not asserted
All 2 claims are executed on every test run, against the same engines the tools use. If the behaviour changes, the test fails rather than this page quietly becoming wrong.
A child element splits the text around it into two nodes.
document.xml <p>a<b/>c</p>expression count(/p/text())What the engine returns
2A CDATA section produces one ordinary text node, not a special one.
document.xml <x><![CDATA[hi]]></x>expression count(/x/text())What the engine returns
1
What catches people out
- text() selects the first-level text nodes only. For all the text beneath an element, use string() instead.
- A comment splits text just as an element does, even though it contributes nothing to the string value.
- Whitespace-only text between elements is still a text node unless the parser was asked to drop it.
Where it bites
A definition on its own is trivia. These are the pages where this term decides whether a document works.
- XPathstring()Concatenates all descendant text, which is usually what you meant by text().
- XPathnormalize-space()Collapses the whitespace those nodes so often carry.
- ToolsXPath testerRun either against your own document and compare.
Related terms
- Mixed contentAn element containing both text and child elements, interleaved — the model prose uses and data rarely does.
- PCDATAParsed character data: text content that the parser scans for markup, so < and & in it have meaning and must be escaped.
- CDATA sectionA <![CDATA[ … ]]> block whose contents are treated as literal characters, so < and & inside it start nothing.
- Significant whitespaceWhitespace that is part of a document's content, as opposed to indentation a processor may drop.
Get started
Bring order to the XML your team can't afford to ignore.
Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.