XML Well-Formed Documents

Every XML file must follow a strict set of rules before any parser can read it. When a file follows all these rules, it earns the title well-formed. A well-formed XML document is the minimum requirement for any XML to work. Without being well-formed, the parser rejects the file entirely.

Think of well-formed XML like a properly addressed envelope. The post office delivers a letter only when it has a correct recipient name, street address, city, and postal code. Missing one piece means the letter goes nowhere. XML parsers behave the same way — one rule violation stops everything.

Rules for a Well-Formed XML Document

Rule 1: Every Document Must Have One Root Element

An XML document must have exactly one top-level element that contains all other elements. This is the root element.

Wrong — Two Root Elements
<person><name>Ravi</name></person>
<car><brand>Honda</brand></car>
Correct — One Root Element
<data>
  <person><name>Ravi</name></person>
  <car><brand>Honda</brand></car>
</data>

Rule 2: Every Opening Tag Must Have a Closing Tag

In HTML, you can write <br> without closing it. XML does not allow this. Every opening tag needs a matching closing tag, or the tag must be self-closing.

Wrong
<name>Anita
<city>Mumbai</city>
Correct
<name>Anita</name>
<city>Mumbai</city>
Self-Closing Tag (also correct)
<image src="photo.jpg" />

Rule 3: Tags Are Case-Sensitive

XML treats uppercase and lowercase letters as different characters. <Name> and <name> are two completely different elements.

Wrong
<Name>Sanjay</name>
Correct
<name>Sanjay</name>

Rule 4: Elements Must Be Properly Nested

Nesting means placing elements inside each other. The inner element must close before the outer element closes. Think of nesting like Russian dolls — the smallest doll goes completely inside the next one.

Wrong — Overlapping Tags
<outer><inner></outer></inner>
Correct — Proper Nesting
<outer><inner></inner></outer>

Nesting Diagram

WRONG (overlapping):
  <A> ─────────────────────┐
        <B> ────────┐      │
        </A>        │  ✗   │
              </B> ─┘      │
                            └── A closes before B

CORRECT (properly nested):
  <A> ──────────────────────┐
        <B> ──────┐         │
              </B>┘         │  ✓
  </A> ─────────────────────┘

Rule 5: Attribute Values Must Be in Quotes

In HTML, writing id=main sometimes works. In XML, every attribute value must be wrapped in single or double quotes. No exceptions.

Wrong
<book id=101>
Correct
<book id="101">

Rule 6: An Element Cannot Have Duplicate Attribute Names

Each attribute name must appear only once per element. Repeating the same attribute name in one tag is not allowed.

Wrong
<item color="red" color="blue">
Correct
<item color="red">

Rule 7: Special Characters Must Be Escaped

The characters <, >, and & carry special meaning in XML. Use their escape codes in text content to avoid confusion.

CharacterEscape CodeMeaning
<&lt;Less than
>&gt;Greater than
&&amp;Ampersand
"&quot;Double quote
'&apos;Apostrophe
Wrong
<formula>5 < 10 & 20 > 15</formula>
Correct
<formula>5 &lt; 10 &amp; 20 &gt; 15</formula>

A Complete Well-Formed XML Example

This example shows a product catalog that follows every well-formed rule:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <product id="P001" category="electronics">
    <name>Wireless Headphones</name>
    <price currency="INR">2499</price>
    <description>Battery life &gt; 20 hours</description>
    <image src="headphones.jpg" />
  </product>
  <product id="P002" category="books">
    <name>Learn XML Fast</name>
    <price currency="INR">399</price>
    <description>Great for beginners &amp; experts</description>
  </product>
</catalog>

Well-Formed vs Valid

Well-formed and valid are two different levels of XML correctness. A well-formed document follows the basic XML syntax rules described above. A valid document additionally matches a specific structure defined in a DTD or XML Schema.

PropertyWell-FormedValid
Follows XML syntax rulesYesYes
Matches a DTD or SchemaNot requiredYes
Can be parsedYesYes
Guarantees data structureNoYes

How to Check if Your XML Is Well-Formed

Most text editors and online tools can check XML for well-formedness. Open your XML in a browser — if it displays a tree structure, the document is well-formed. If the browser shows an error message with a line number, find that line and fix the syntax problem.

Free online validators like XMLValidation.com accept XML code and immediately report any well-formedness errors with clear descriptions.

Key Points to Remember

  • A well-formed XML document follows all basic XML syntax rules.
  • One root element must contain all other elements.
  • Every tag must close — either with a closing tag or as a self-closing tag.
  • XML tags are case-sensitive — match opening and closing tags exactly.
  • Elements must nest properly without overlapping.
  • Attribute values always need quotes.
  • Special characters use escape codes: &lt; &gt; &amp;.
  • A well-formed document can be parsed even without a DTD or Schema.