XML Tree
Every XML document forms a tree. There is exactly one element at the top — the root — and every other element sits inside it, nested as children, grandchildren and so on. Understanding this tree shape is the key to everything that follows: XPath, the DOM and XSLT all work by walking the tree.
A document and its tree
<school>
<class code="7B">
<teacher>Ms Okafor</teacher>
<student>Liam</student>
<student>Noor</student>
</class>
<class code="8A">
<teacher>Mr Brandt</teacher>
<student>Yuki</student>
</class>
</school>Run it: the output is indented exactly like the tree. Drawn as a diagram it looks like this:
school ← root element
├── class (code="7B")
│ ├── teacher "Ms Okafor"
│ ├── student "Liam"
│ └── student "Noor"
└── class (code="8A")
├── teacher "Mr Brandt"
└── student "Yuki"Family vocabulary
XML borrows words from family trees to describe how elements relate:
| Term | Meaning | Example from above |
|---|---|---|
| Root | The single top-level element | <school> |
| Parent | The element directly containing another | <class> is the parent of <teacher> |
| Child | An element directly inside another | <student> is a child of <class> |
| Siblings | Elements with the same parent | the two <student> elements in 7B |
| Ancestor | Any element above, all the way to the root | <school> is an ancestor of every <student> |
| Descendant | Any element below, at any depth | every <student> is a descendant of <school> |
| Leaf | An element with no child elements | <teacher>, <student> |
Exactly one root
A document with two top-level elements is not a tree any more — it is two trees, and the parser rejects it:
<student>Liam</student>
<student>Noor</student>The fix is to wrap them in a single parent such as <students>…</students>.
Order matters
Child elements keep the order they were written in. A program reading the document will see Liam before Noor. That makes XML good for things where order carries meaning, such as the chapters of a book or the steps of a recipe.
<recipe name="Pancakes">
<step n="1">Whisk eggs and milk</step>
<step n="2">Stir in flour</step>
<step n="3">Rest the batter for 10 minutes</step>
<step n="4">Fry in a hot pan</step>
</recipe>Text and attributes are part of the tree too
In the full model (the DOM, which you'll meet later) it isn't only elements that are nodes. The text Ms Okafor is a text node that is a child of <teacher>, and code="7B" is an attribute node attached to its <class>. Even the line breaks and spaces between elements become text nodes — a detail that matters when you walk the tree with JavaScript.
Tip: When you design an XML format, sketch the tree first. If the same information appears in two branches, or a branch grows very deep, the structure probably needs rethinking.
Mixed content
Elements can contain a mix of text and other elements. This is common in document formats:
<para>Water boils at <value unit="C">100</value> degrees at <em>sea level</em>.</para>Here <para> has five children in order: text, <value>, text, <em>, text.
Test yourself
How many root elements can a well-formed XML document have?
- None
- Exactly one
- One per section
- As many as you like
In <a><b/><c/></a>, what is the relationship between b and c?
- Parent and child
- Ancestor and descendant
- Siblings
- Unrelated
Which word describes every element below a given element, at any depth?
- Children
- Descendants
- Siblings
- Ancestors