Skip to main content

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

XPath operator

text() node test

Selects text-node children, including indentation whitespace as well as the words a reader sees.

text() selects text nodes, not an element's recursively collected string-value. Direct character data and indentation whitespace are separate child nodes; text inside a nested element belongs to that nested element.

Use string(.) or normalize-space(.) when you want the combined textual value of an element. Use text() when the distinction between direct text and nested markup matters.

Syntax
child::text() — usually text()
Returns
node-set
Kind
Operator
Version
XPath 1.0 (libxml2)

Worked examples

Each result below is what the evaluator returned, asserted on every test run. An empty result means an empty node-set — which is an answer, not an error.

  • count(/order/text())on order.xmlRun
    6
  • normalize-space(/order/customer/text())on order.xmlRun
    Ada Lovelace
  • count(//line/text())on order.xmlRun
    0

    Each line contains a <price> child but no direct text of its own.

Run your own expression

What catches people out

  • Pretty-printing creates whitespace text nodes, so text()[1] may be a newline rather than visible content.
  • text() selects direct children only. Descendant text needs .//text() or the element's string-value.
  • An attribute has a string-value but no text-node child, so @id/text() selects nothing.

The sample documents

Every example on this page runs against this document. The sitemap one is namespaced on purpose — almost every real XML document is, and that changes which expressions match.

order.xml
<?xml version="1.0" encoding="UTF-8"?>
<order id="A-1001" tier="gold">
  <customer country="NO">Ada Lovelace</customer>
  <!-- priority customer -->
  <?audit retained?>
  <lines>
    <line sku="WIDGET-1" qty="2"><price>14.50</price></line>
    <line sku="WIDGET-2" qty="1"><price>39.00</price></line>
    <line sku="GIFT-WRAP" qty="1"><price>0.00</price></line>
  </lines>
  <note>  Deliver after 09:00  </note>
</order>

Related

All 40 XPath entries

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.