TExcelTAD
Home › XML › XML Syntax

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
<?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/>.

xml · try it yourself
<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:

xml · try it yourself
<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:

xml · try it yourself
<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:

xml · try it yourself
<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?
< &lt; Always, in text and attribute values
& &amp; Always
> &gt; Optional (required only in the sequence ]]>)
" &quot; Inside a "-quoted attribute
' &apos; Inside a '-quoted attribute
xml · try it yourself
<rule>if price &lt; 10 &amp;&amp; stock &gt; 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

  1. One root element.
  2. Every start tag has an end tag (or is self-closing).
  3. Tags nest without overlapping.
  4. Names match in case.
  5. Attribute values are quoted, and no attribute is repeated on the same element.
  6. < and & in text are escaped.
  7. 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> containing cheap. Checking content rules is called validation and is covered in the DTD and Schema lessons.

Test yourself

Which document is well-formed?

  1. <a><b></a></b>
  2. <a><b></b></a>
  3. <a><b></B></a>
  4. <a x=1></a>

How must a literal & be written inside XML text?

  1. &
  2. &and;
  3. &amp;
  4. \&

Where may the XML declaration <?xml version="1.0"?> appear?

  1. Anywhere in the prolog
  2. Only at the very start of the document
  3. Right after the root element
  4. At the end of the file