Skip to main content
XMLDir

Search 309 pages — tools, formats, elements, namespaces, directory, comparisons, XPath, datatypes, glossary, parse errors, schema errors, use cases, blog and product.

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

    2
  • A 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.

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.