XML Tree

Every XML document is built as a tree of elements, starting from a single root and branching down into parents and children.

The Tree Structure

XML documents form a hierarchy called an XML tree. The tree starts at a single root element, and every other element is nested inside it, branching downward the way a family tree branches from an ancestor to its descendants.

A Bookstore as a Tree

<!DOCTYPE html>
<html>
<body>

<pre>
&lt;bookstore&gt;
  &lt;book category="fiction"&gt;
    &lt;title&gt;Harry Potter&lt;/title&gt;
    &lt;author&gt;J K. Rowling&lt;/author&gt;
    &lt;year&gt;2005&lt;/year&gt;
  &lt;/book&gt;
  &lt;book category="reference"&gt;
    &lt;title&gt;Everyday Italian&lt;/title&gt;
    &lt;author&gt;Giada De Laurentiis&lt;/author&gt;
    &lt;year&gt;2005&lt;/year&gt;
  &lt;/book&gt;
&lt;/bookstore&gt;
</pre>

</body>
</html>

Root, Parent, and Child

In the example above, <bookstore> is the root element — the single top-level element every other element lives inside. Each <book> is a child of <bookstore> and, at the same time, a parent to its own <title>, <author>, and <year> elements. Elements that share the same parent, like the two <book> elements, are called siblings.

  • Root — the one element that contains every other element in the document; a document can have only one.
  • Parent — any element that contains other elements.
  • Child — an element contained within another element.
  • Sibling — elements that share the same parent.
  • Descendant — any element nested anywhere below another, at any depth.
  • Ancestor — any element above another in the tree, at any depth.

Family Terms in a Deeper Tree

<!DOCTYPE html>
<html>
<body>

<pre>
&lt;family&gt;
  &lt;parent&gt;
    &lt;child&gt;
      &lt;grandchild&gt;Leaf node&lt;/grandchild&gt;
    &lt;/child&gt;
  &lt;/parent&gt;
&lt;/family&gt;
</pre>

</body>
</html>
Note: The words parent, child, sibling, and root come from the same tree analogy used across computer science generally — the same vocabulary shows up when people talk about file systems, HTML's DOM, or organizational charts.

Why the Tree Matters

Software that reads XML — parsers, XPath queries, XSLT transformations — all navigate the document by walking this tree: starting at the root, stepping down into children, and stepping back up to parents. Understanding the tree shape is what makes those tools' syntax make sense later on.

Navigating Down From the Root

<!DOCTYPE html>
<html>
<body>

<pre>
&lt;catalog&gt;
  &lt;cd&gt;
    &lt;title&gt;Empire Burlesque&lt;/title&gt;
    &lt;artist&gt;Bob Dylan&lt;/artist&gt;
  &lt;/cd&gt;
&lt;/catalog&gt;
&lt;!-- catalog is the root; cd is its only child;
     title and artist are cd's children --&gt;
</pre>

</body>
</html>
TermMeaningExample from bookstore
RootTop-level element, exactly one per document<bookstore>
ParentElement containing other elements<book>
ChildElement contained by another<title>
SiblingElements sharing a parentthe two <book> elements
Note: A document with more than one top-level element is not valid XML. If you find yourself writing two elements side by side at the very top, wrap them both in a single root element.

Exercise: XML Tree

In the XML tree structure, what must every well-formed document contain?