XML Syntax

XML enforces a small, strict set of syntax rules that keep every document parseable no matter which program reads it.

Why Strict Syntax Matters

HTML browsers are famously forgiving — they'll often render a page even with mismatched tags or missing quotes. XML parsers are not forgiving at all. A document that breaks any syntax rule isn't merely sloppy, it's invalid XML, and a compliant parser is required to refuse it. This strictness is deliberate: it guarantees that any XML parser, on any platform, interprets a document exactly the same way.

Every Element Needs a Closing Tag

In HTML, an element can sometimes be left open and the browser will still guess what you meant. In XML, every opening tag must have a matching closing tag, with no exceptions.

Closing Tags Are Required

<!DOCTYPE html>
<html>
<body>

<pre>
&lt;!-- Wrong --&gt;
&lt;message&gt;Hello world

&lt;!-- Correct --&gt;
&lt;message&gt;Hello world&lt;/message&gt;
</pre>

</body>
</html>

XML Is Case Sensitive

The tags <Message> and <message> are treated as two completely different elements. An opening tag and its closing tag must match in case exactly, or the document is invalid.

Matching Case Exactly

<!DOCTYPE html>
<html>
<body>

<pre>
&lt;!-- Wrong: opening and closing tags differ in case --&gt;
&lt;Message&gt;Hello&lt;/message&gt;

&lt;!-- Correct --&gt;
&lt;Message&gt;Hello&lt;/Message&gt;
</pre>

</body>
</html>

Quoting Attributes and Proper Nesting

Attribute values must always be wrapped in quotation marks — single or double, used consistently — and never left bare. Elements must also be properly nested, meaning a child element has to close before its parent does, the same way nested parentheses have to close in reverse order of how they opened.

Quoting and Nesting Correctly

<!DOCTYPE html>
<html>
<body>

<pre>
&lt;!-- Wrong: unquoted attribute and a child that outlives its parent --&gt;
&lt;book category=fiction&gt;&lt;title&gt;XML Basics&lt;/book&gt;&lt;/title&gt;

&lt;!-- Correct --&gt;
&lt;book category="fiction"&gt;&lt;title&gt;XML Basics&lt;/title&gt;&lt;/book&gt;
</pre>

</body>
</html>
  • Every element needs a matching closing tag.
  • Tags are case sensitive — <Item> and <item> are different elements.
  • Elements must be properly nested — a child must close before its parent does.
  • Attribute values must always be quoted.
  • A document must have exactly one root element.
  • Whitespace inside text content is preserved, unlike in HTML.

One Root Element

A valid XML document must have exactly one top-level element containing everything else. If two elements sit side by side at the very top with nothing wrapping them, the document is invalid — wrap them in a single shared root.

Note: Improper nesting is one of the most common XML mistakes beginners make. Tags must close in the reverse order they were opened — like nested parentheses.
Note: A document that follows every one of these rules is called well-formed. If you're unsure whether yours qualifies, run it through an XML validator rather than guessing.
RuleHTMLXML
Closing tagsOften optionalAlways required
Case sensitivityNot case sensitiveCase sensitive
Attribute quotesSometimes optionalAlways required
Root elementsNo restrictionExactly one

Exercise: XML Syntax

How does XML handle tag name casing compared to HTML?