XML Syntax Rules
XML's rules are few, simple — and strictly enforced. A document that obeys them is called well-formed. A parser that finds even one violation must stop and report an error; it is not allowed to guess what you meant the way an HTML browser does.
The prolog
A document may start with an XML declaration:
<?xml version="1.0" encoding="UTF-8"?>It is optional, but if present it must be the very first thing in the file — not even a space or blank line may come before it. UTF-8 is the default encoding and the right choice for almost everything.
Rule 1 — every element must be closed
Each start tag needs a matching end tag. Elements with no content can use the short self-closing form <tag/>.
<order>
<item>Notebook</item>
<item>Pencil</item>
<giftWrap/>
</order>Now remove </item> from the second item and run it: the parser reports where it got confused.
Rule 2 — tags are case-sensitive
<Item> and <item> are two different names, so they can't open and close the same element:
<note>
<Title>Shopping</title>
</note>Rule 3 — elements must nest properly
An element that starts inside another must also end inside it. Overlapping tags are not allowed:
<p>This is <b><i>wrong</b></i></p>Fix it by closing in reverse order: <b><i>right</i></b>.
Rule 4 — exactly one root element
Everything must live inside a single top-level element (see the XML Tree lesson).
Rule 5 — attribute values must be quoted
Use double or single quotes, but always use one of them:
<book pages=320 lang="en">Dune</book>Change pages=320 to pages="320" and the document becomes well-formed.
Rule 6 — escape special characters
The < character always starts a tag, and & always starts an entity reference, so neither may appear raw in text. Use the predefined entities:
| Character | Entity | Needed? |
|---|---|---|
< |
< |
Always, in text and attribute values |
& |
& |
Always |
> |
> |
Optional (required only in the sequence ]]>) |
" |
" |
Inside a "-quoted attribute |
' |
' |
Inside a '-quoted attribute |
<rule>if price < 10 && stock > 0 then "show" it</rule>Comments
Comments look like HTML comments: <!-- like this -->. They must not contain -- inside, and can't appear inside a tag.
White space is preserved
Unlike HTML, which collapses runs of spaces into one, XML hands every space, tab and newline in your text to the application. Whether that white space matters is up to the program reading it.
Line endings
Whatever your editor saves (CRLF on Windows, LF on Linux/macOS), XML parsers normalise line endings to a single line feed, so documents behave the same on every system.
A well-formed checklist
- One root element.
- Every start tag has an end tag (or is self-closing).
- Tags nest without overlapping.
- Names match in case.
- Attribute values are quoted, and no attribute is repeated on the same element.
<and&in text are escaped.- The XML declaration, if any, is the first thing in the file.
Tip: Well-formed is the minimum bar. A document can be well-formed but still wrong for its purpose — for example, a
<price>containingcheap. Checking content rules is called validation and is covered in the DTD and Schema lessons.
Test yourself
Which document is well-formed?
<a><b></a></b><a><b></b></a><a><b></B></a><a x=1></a>
How must a literal & be written inside XML text?
&∧&\&
Where may the XML declaration <?xml version="1.0"?> appear?
- Anywhere in the prolog
- Only at the very start of the document
- Right after the root element
- At the end of the file