XPath Data Model: Elements, Attributes, and Text Nodes
The XPath data model conceptualizes an XML document as a hierarchical tree of nodes rather than raw text. Within this tree, XML structures are mapped to specific node types, each possessing distinct properties, parent-child relationships, and value-extraction rules. This article examines how the XPath data model specifically represents the three most common node types: element nodes, attribute nodes, and text nodes.
The XPath Node Tree Structure
Before examining specific nodes, it is essential to understand that the XPath data model represents an XML document as a connected tree starting from a root node (also known as the document node). Every component of the XML document—including tags, attributes, and text content—is mapped into an abstract node object with defined properties such as node-name, parent, children, and string-value.
Element Nodes
Element nodes represent the primary structural tags within an XML
document. Every XML tag pair (e.g.,
<item>...</item>) or self-closing tag (e.g.,
<item/>) becomes an element node in the XPath
tree.
- Identity and Naming: An element node has an expanded name consisting of a local name and an optional namespace URI.
- Hierarchy and Relationships:
- Parent: An element node’s parent is either the document root node or another element node.
- Children: An element node can have zero or more child nodes, which may include other element nodes, text nodes, comment nodes, and processing instruction nodes.
- Attribute Association: While an element node has
associated attribute nodes, attributes are not considered child
nodes of the element. Instead, the element is the parent of its
attributes, but attributes exist on a separate
attribute::axis. - String Value: The string-value of an element node is the direct concatenation of all character data contained within its descendant text nodes, in document order.
Attribute Nodes
Attribute nodes represent the name-value pairs defined inside an
element’s opening tag (e.g., <item id="101">).
- Identity and Naming: Like element nodes, attribute nodes possess an expanded name (namespace URI and local name). Default attributes declared in a DTD or XML Schema are also instantiated as attribute nodes.
- Hierarchy and Relationships:
- Parent: Each attribute node has a parent element node (the element on which it is declared).
- No Children: Attribute nodes are leaf nodes and cannot have child nodes.
- Axis Separation: Because attributes are not
children, selecting children using XPath expressions like
/item/*will never return attribute nodes; they must be queried using the attribute axis (e.g.,/item/@idor/item/attribute::id).
- String Value: The string-value of an attribute node is the normalized text value assigned to that attribute.
Text Nodes
Text nodes encapsulate the character data (PCDATA) found between element tags.
- Identity and Naming: Text nodes do not have an expanded name; their node-name property is empty.
- Hierarchy and Relationships:
- Parent: A text node always has an element node as its parent.
- No Children: Text nodes are leaf nodes and cannot contain child nodes or attributes.
- Normalization and Merging: In the XPath data model, text nodes are normalized. Adjacent character data is merged into a single text node, meaning an element node will never have two consecutive text node children. Empty text nodes (zero-length strings) are not created in the data model.
- String Value: The string-value of a text node is the character string itself.
Node Characteristics Comparison
| Node Type | Has Expanded Name? | Can Have Children? | Considered a Child of Element? | String Value Calculation |
|---|---|---|---|---|
| Element | Yes | Yes (elements, text, comments, PIs) | Yes | Concatenation of all descendant text nodes |
| Attribute | Yes | No | No (accessed via attribute::
axis) |
The normalized attribute value string |
| Text | No | No | Yes | The character data itself |