Lesson 2 of 6

Syntax & Structure

Think of an XML document like a set of labelled boxes, nested inside each other. Every box has a name on the outside (an opening tag) and a matching label confirming where it ends (a closing tag), and boxes can hold other boxes — but never in a way that lets one box poke halfway out of its parent. Get that nesting discipline right, and you have what XML calls a well-formed document.

💡 The Bottom Line

An XML document is built from elements — an opening tag, optional content, and a matching closing tag — arranged in a single nested tree with exactly one root element. Every tag that opens must close, tags must nest properly without overlapping, and there must be exactly one outermost element wrapping everything else. A document that follows all three rules is called well-formed.

Elements: the building block

An element is everything from an opening tag to its matching closing tag:

<title>The Pragmatic Programmer</title>

Here <title> is the opening tag, </title> is the closing tag (note the forward slash), and The Pragmatic Programmer is the element's text content. Elements can also contain other elements instead of plain text — that's how you build structure:

<book>
  <title>The Pragmatic Programmer</title>
  <author>Andy Hunt</author>
  <year>1999</year>
</book>

Here, <book> is the parent element, and <title>, <author>, and <year> are its children — each one a complete little element in its own right, nested inside.

Exactly one root element

Every well-formed XML document must have a single outermost element that contains everything else — the root element. You can't have two top-level elements side by side:

<!-- NOT well-formed — two elements at the top level -->
<book>...</book>
<book>...</book>

<!-- Well-formed — one root wrapping both -->
<library>
  <book>...</book>
  <book>...</book>
</library>

⚠️ Gotcha: proper nesting, not just closing

Tags must close in the reverse order they opened — like parentheses. Closing every tag isn't enough if they overlap:

<!-- NOT well-formed — overlapping tags -->
<book><author>Andy Hunt</book></author>

<!-- Well-formed — properly nested -->
<book><author>Andy Hunt</author></book>

Self-closing tags

Some elements never have content — they just mark a point in the document, like a line break. Writing <br></br> works, but XML also allows a shorthand: a single tag that opens and closes itself, marked with a trailing slash before the final >:

<br/>
<line-break/>
<book id="123"/>

<br/> is exactly equivalent to <br></br> — it's purely a convenience so you don't have to write empty closing tags for every content-free element.

🙋 There Are No Dumb Questions

Q: Does "well-formed" mean my XML is correct?
A: It means your XML follows the basic syntax rules — closed tags, proper nesting, one root. It does not mean the document matches whatever structure a particular application expects (the right elements, in the right order, with the right names). That second, stricter check is called being valid against a schema — we'll come back to this distinction in Lesson 6, because mixing the two up is one of the most common XML mistakes.

🔧 Try It Yourself

Spot the problem in this XML fragment before reading on: <movie><title>Up</movie></title>. (Hint: trace the nesting — which tag closes first, and which one opened first?)

✅ Quick Check

Which of these is required for an XML document to be "well-formed"?


Next up: attributes — a second way to attach data to an element — and namespaces, which keep tag names from colliding when you combine XML from different sources.

← Back to
Next →