2. XML Syntax

What you will learn

  • The rules every XML file must follow
  • The XML declaration
  • Root elements, nesting, and case
  • How to include special characters

The rules

  1. There must be one root element that contains everything.
  2. Every opening tag needs a closing tag (or be self-closing).
  3. Elements must be properly nested.
  4. Tag names are case sensitive.
  5. Attribute values must be in quotes.
  6. Special characters must be escaped.

An XML file that follows these rules is called well-formed. A parser refuses a file that is not.

The XML declaration

An XML file may start with a declaration. It is optional, but good practice. If present, it must be the very first thing in the file, with nothing (not even a blank line) before it.

XML code

<?xml version="1.0" encoding="UTF-8"?>
  • version is almost always 1.0.
  • encoding says how characters are stored. UTF-8 supports every language, and you should save your file as UTF-8 too.

One root element

XML code: correct

<?xml version="1.0" encoding="UTF-8"?>
<library>
  <book>Learning XML</book>
  <book>Learning PHP</book>
</library>

XML code: wrong (two roots)

<?xml version="1.0" encoding="UTF-8"?>
<book>Learning XML</book>
<book>Learning PHP</book>

What a parser says

The first file is accepted. The second is rejected with an error such as “extra content at the end of the document”, because there are two top-level elements instead of one.

Closing tags and empty elements

Unlike HTML, every element must be closed. An element with no content may use a self-closing tag.

XML code

<note>
  <to>Ali</to>
  <urgent/>
  <attachment></attachment>
</note>

<urgent/> and <attachment></attachment> mean the same thing: an element with no content.

Proper nesting

Close the inner element before the outer one.

XML code: correct

<p>This is <b>bold</b> text.</p>

XML code: wrong (crossed tags)

<p>This is <b>bold</p></b>

What a parser says

The second line is rejected: the closing tag </p> does not match the open element <b>.

Case sensitivity

XML code: wrong

<Message>Hello</message>

Message and message are different names, so these tags do not match, and the file is not well-formed. A common habit is to use lowercase, or camelCase such as firstName, and to stay consistent.

Quotes around attribute values

Attribute values must always be in single or double quotes. (Attributes get their own lesson.)

XML code

<note date="2026-03-15">Hello</note>

Special characters

Five characters have a special meaning in XML. To use them in text, write an entity reference instead.

Character Write
< &lt;
> &gt;
& &amp;
" &quot;
' &apos;

XML code

<condition>if salary &lt; 1000 &amp; age &gt; 30</condition>

What the text means after parsing

if salary < 1000 & age > 30

The characters < and & are strictly forbidden in ordinary text, so always escape them. The others are only required in some places, but escaping them is harmless.

Unlike HTML, XML does not know names such as &nbsp; or &copy;. Use the actual character (in a UTF-8 file) or a numeric reference such as &#169;.

Whitespace

XML keeps whitespace inside text, unlike HTML, which collapses it. Indentation between elements is ignored by most programs, but spaces inside an element’s text can count as data. Indent with two spaces to keep files readable.

Common mistakes

  • Two root elements, or text outside the root.
  • Forgetting a closing tag, or closing tags in the wrong order.
  • Mixing capital letters between opening and closing tags.
  • Typing a raw & or < in text.
  • Putting a blank line or space before <?xml ... ?>.
  • Saving with an encoding that does not match the declaration.

Exercise

This file has three mistakes. Find and fix them.

<?xml version="1.0" encoding="UTF-8"?>
<shop>
  <Item>Tom & Jerry</item>
  <price currency=USD>5</price>
</shop>
<note>Thanks</note>
Show solution
  1. <Item> and </item> have different capital letters.
  2. The raw & must be written &amp;. The attribute value USD needs quotes.
  3. The <note> element sits outside the root, giving a second root element. Move it inside <shop>.
<?xml version="1.0" encoding="UTF-8"?>
<shop>
  <item>Tom &amp; Jerry</item>
  <price currency="USD">5</price>
  <note>Thanks</note>
</shop>

Knowledge check

1. How many root elements may an XML file have?

Exactly one.

2. Are <Name> and <name> the same tag?

No. XML is case sensitive.

3. How do you write an ampersand in XML text?

&amp;

Summary

  • A well-formed file has one root, closed and properly nested tags, matching case, and quoted attribute values.
  • Start with an optional declaration, and save as UTF-8.
  • Escape <, &, and the other special characters with entity references.
Scroll to Top