Well-Formedness Rules

A "well-formed" XML document is one that follows XML's syntax rules correctly. This is different from being "valid" (which means it matches a specific structure defined by a DTD or schema, covered next lesson) — well-formedness is the more basic bar every XML document has to clear just to be parsed at all.

Every tag needs a matching close

Unlike HTML, where a browser will forgive a missing closing tag, XML requires one for every element — or a self-closing tag for an element with no content:

XML both are well-formed
<price>19.99</price>
<empty-note />

The second form, <empty-note />, is shorthand for <empty-note></empty-note> — a self-closing tag for an element that has no content or children.

Proper nesting

Tags have to close in the reverse order they opened — they can't overlap:

XML well-formed
<book><title>Dune</title></book>
XML NOT well-formed — overlapping tags
<book><title>Dune</book></title>

In the second example, <title> opens inside <book> but closes after </book> already closed it — the tags cross over each other instead of nesting cleanly, and a parser rejects this outright.

Quoted attribute values

Attribute values must always be wrapped in quotes — single or double, but always something:

XML well-formed
<book id="42"></book>
XML NOT well-formed — unquoted value
<book id=42></book>

Case sensitivity

XML tag names are case-sensitive, meaning <Item>, <item>, and <ITEM> are three completely different tag names as far as a parser is concerned:

XML NOT well-formed — mismatched case
<Item>Widget</item>
Parsing this document
error: Opening and ending tag mismatch: Item line 1 and item

Even though Item and item look like the same word to a person, a parser treats the closing </item> as closing a tag that was never opened, since the actual open tag was <Item>, capital I.

Note: this is the biggest practical difference from HTML habits — a browser silently repairs broken HTML (missing tags, mismatched case, unquoted attributes) and just renders whatever it can figure out. An XML parser does none of that. One well-formedness violation, anywhere in the document, and parsing fails completely — there's no partial result to fall back on.