Text Nodes

Text nodes represent the character data content within elements. Every adjacent string of characters within element content makes up a single text node. Text nodes can be both queried and constructed in XQuery, although these expressions have limited usefulness.

Text Nodes and the Data Model

A text node does not have any children, and its parent is an element. In Example 21-8, the desc element has three children:

  • A text node whose content is Our (ending with a space)

  • A child element i

  • A text node whose content is shirt! (starting with a space)

The i element itself has one child: a text node whose content is favorite.

Example 21-8. Text nodes in XML (desc.xml)

<desc>Our <i>favorite</i> shirt!</desc>

The string value of a text node is its content, as an instance of xs:string. Its typed value is the same as the string value, except that it is of type xs:untypedAtomic rather than xs:string.

Text nodes do not have names, so calling any of the name-related functions with a text node will result in the empty sequence or a zero-length string, depending on the function.

If your document has no DTD or schema, any whitespace appearing between the tags in your source XML will be translated into text nodes. This is true even if it is just there to indent the document. For example, the following b:header element node:

<b:header>
   <b:date>2006-10-15</b:date>
</b:header>

has three children. The first and third children are text nodes that contain only whitespace, and the second child is, of course, ...

Get XQuery now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.