XML Well-Formedness

The 5 rules every XML document must follow — with common errors and fixes.

Validate your XML instantly with the free XML Formatter — paste and click Validate.

What Is XML Well-Formedness?

A well-formed XML document follows the syntactic rules defined by the W3C XML specification. Parsers reject any document that violates these rules with a fatal error — there is no error-recovery in XML, unlike HTML. Every XML parser enforces well-formedness strictly.

Well-formedness is not the same as validity. A valid XML document is well-formed AND conforms to a schema (XSD, DTD, or RELAX NG). Well-formedness is the minimum baseline all XML documents must satisfy, regardless of whether a schema exists.

Rule 1 — Every Opening Tag Must Have a Matching Closing Tag

Every element must be explicitly closed. Void elements that HTML permits as bare tags (<br>, <img>) must use self-closing syntax in XML:

<!-- Invalid: missing closing tag -->
<note>
  <body>Hello!
</note>

<!-- Valid -->
<note>
  <body>Hello!</body>
</note>

<!-- Valid self-closing for empty elements -->
<br/>
<img src="photo.jpg"/>

Rule 2 — Tags Must Be Properly Nested

Overlapping tags are illegal. Every child element must be fully contained within its parent before the parent closes:

<!-- Invalid: overlapping tags -->
<b><i>bold italic</b></i>

<!-- Valid: proper nesting -->
<b><i>bold italic</i></b>

Rule 3 — Attribute Values Must Be Quoted

Every attribute value must be enclosed in single or double quotes. Unquoted attribute values are not permitted in XML:

<!-- Invalid: unquoted attribute -->
<img width=100 height=200/>

<!-- Valid -->
<img width="100" height="200"/>

Rule 4 — Exactly One Root Element

An XML document must have exactly one root element that wraps all other content. Multiple top-level elements make a document invalid:

<!-- Invalid: two root elements -->
<item>first</item>
<item>second</item>

<!-- Valid: single root wraps everything -->
<items>
  <item>first</item>
  <item>second</item>
</items>

Rule 5 — Special Characters Must Be Escaped

Five characters have special meaning in XML markup and must be escaped in element content and attribute values:

&amp;   →  &   (ampersand)
&lt;    →  <   (less-than sign)
&gt;    →  >   (greater-than sign)
&quot;  →  "   (double quote — in attribute values)
&apos;  →  '   (apostrophe — in attribute values)

Alternatively, raw < and & characters can appear inside a CDATA section without escaping.

Tag Names Are Case-Sensitive

<Note>, <note>, and <NOTE> are three distinct elements. A closing tag must exactly match the case of its opening tag:

<!-- Invalid: case mismatch -->
<Note>Hello</note>

<!-- Valid -->
<Note>Hello</Note>

Common Validation Errors and Fixes

"Unclosed tag" / "Missing end tag"
An opening tag has no matching closing tag. Add the missing </tagname>, or convert to a self-closing empty element: <tagname/>.
"Unexpected end of file"
The parser reached the end of the document while still inside an element. Usually a closing tag is missing somewhere before the end of the file.
"Attribute without a value"
HTML allows bare boolean attributes like disabled or checked — XML does not. Use disabled="disabled" or checked="true".
"Multiple root elements"
Wrap all top-level elements inside a single container: <root>...</root>.
"Illegal character" / "Invalid character reference"
Control characters (ASCII 0–8, 11–12, 14–31) are not allowed in XML text. Strip them, or replace with numeric character references where the spec allows them (e.g., tab: &#9;).

Well-Formedness vs Schema Validation

The XML Formatter's Validate button checks well-formedness only — it confirms the document follows the five structural rules above. It does not validate element sequences, required attributes, or data types against an XSD or DTD schema.

Schema validation catches business-rule errors ("this element requires a type attribute", "the value must be a positive integer"). Well-formedness validation catches the structural errors that prevent any parser from reading the document at all. Fixing well-formedness is always the first step.