Concepts / Querying XML with XPath

Querying XML with XPath

XML documents are organized as tree structures with a single root element containing all other elements as nested children.

  • Programming

From Tags to a Tree

A deeply nested XML document can look like a jumble of angle brackets and text when read as plain text. A more useful mental model is a tree. The document has one root element at the top, and every other element is nested inside that root or inside another element below it. XPath works with this organization: it describes a path through the XML tree to select elements.

containscontainscontainscontainspersonroot elementnamechild elementphonechild elementphonechild elementemailchild element
What contains what in an XML document, and how are the root element and nested children arranged?

The root element is the starting point for understanding the document. It contains the rest of the XML as nested descendants.

Elements as Tree Nodes

Each node in an XML tree represents an element. An element can have a tag name, attributes attached to its opening tag, text content between its opening and closing tags, and child elements nested inside it. For XPath navigation, the element's position in the tree is especially important: its position tells you what contains it and what it contains.

containscontainscontainscontainsbookparenttitlechildauthorchildpricechildisbnchild
Which element is the parent of a given element, and which elements are its children or siblings?

Nesting creates parent-child relationships. In the book example, book is the parent of title, author, price, and isbn. Those four elements are siblings because they share book as their parent and occupy the same nesting level. A sibling is not inside another sibling; each one is a separate child of the same parent.

Following an XPath Path

Navigating a multi-level XML tree means starting at the root and following nested parent elements until you reach the element that contains the data you want. This movement through the hierarchy is called a traversal. XPath describes this kind of path so that an XML query tool can select an element within the tree.

down todown todown tocompanyrootdepartmentnested elementteamnested elementmemberAlice
How does a path move from the root through nested levels to locate the member whose text is Alice?

Locating a Team Member

Use the XML tree structure to describe how to locate the member element whose text is Alice.

Begin at company: company is the root element, so it is the starting point for the traversal.

Enter a department: Move from the root to a department child.

Enter a team: Move from that department to a team nested inside it.

Reach member: Move to the member element and identify the one whose text is Alice.

The traversal is company → department → team → member. XPath expresses this path for selecting the desired XML element.

An XPath query is easiest to reason about when you can say its tree path aloud: start at the root, move through each nested parent, and stop at the target element.

Why the Tree View Helps

Tree visualization turns a flat-looking sequence of tags into a hierarchy of containers and contained elements. It makes the root visible, shows which elements are siblings, and exposes the levels that a traversal must cross. This is why the tree model is useful when working with XML parsers, validators, query tools, and transformations: each tool operates in relation to the document's hierarchical organization.

Tree ideaWhat to look for while querying
Root elementThe single top element where navigation begins
ParentAn element that contains another element
ChildAn element nested inside a parent
SiblingElements at the same level that share a parent
TraversalA path followed from the root through nested levels

Tree relationships used when reasoning about XPath navigation

Mistakes in Tree Navigation

  • Treating sibling elements as if one were inside another

    title and author are separate children of book, so they share a parent rather than forming a parent-child pair.

    Fix: Represent both elements at the same level under book.

  • Starting navigation from a nested element instead of the root

    The complete traversal begins at the document's single root element.

    Fix: Trace the path from company through department and team before reaching member.

  • Skipping a nesting level

    The member element is reached through department and team in the described hierarchy.

    Fix: Follow every parent-child step: company → department → team → member.

  • Ignoring element text when several elements have the same name

    The person example contains two phone children at the same level, so the tree shows multiple elements with the same tag name.

    Fix: Use the surrounding tree and the element's content to distinguish the data you are seeking.

Practice the Traversal

EASY

Consider an XML tree whose root is catalog. Inside it is a book element. The book contains title, author, price, and isbn. Identify the root element, the parent of price, and the siblings of title.

Hints
  • The root is the element at the top of the entire tree.
  • The parent of an element is the element that directly contains it.
  • Siblings share the same parent and appear at the same nesting level.

What do you think happens?

What are the answers for the catalog tree?

Reveal answer

Answer: The root element is catalog. The parent of price is book. The siblings of title are author, price, and isbn.

catalog is the top element. price is directly contained by book. title, author, price, and isbn share book as their parent and therefore are siblings at the same level.

XPath Navigation Checklist

  1. Find the single root element at the top of the XML tree.
  2. Identify the parent-child relationships created by nesting.
  3. Separate siblings from descendants: siblings share a parent, while descendants are reached by moving downward through nested levels.
  4. Describe the traversal from the root through each required parent to the target element.
  5. Use that tree path as the mental model for an XPath selection.

XPath becomes easier to understand when XML is viewed as a tree rather than as a flat stream of tags. The root provides the starting point, nesting defines parent-child relationships, siblings occupy the same level, and traversal follows a path down through the hierarchy to the desired element.

Key Takeaways

  • An XML document is organized as a tree with one root element containing all other elements.
  • Nesting creates parent-child relationships, while elements with the same parent are siblings.
  • Each XML node can represent an element with a tag name, attributes, text content, and child elements.
  • A traversal follows a path from the root through nested levels to locate a target element.
  • XPath describes paths through the XML tree so query tools can select elements.