Chapter 5: XML Syntax

XML Syntax Rules – The Absolute Must-Know List

XML is extremely strict about syntax. If even one rule is broken → the document is not well-formed → most parsers will refuse to read it at all.

Think of XML as a very strict librarian who will not accept a book if even one label is wrong.

Here are all the important syntax rules in detail:

Rule 1 – Exactly One Root Element (Top-Level Element)

There must be exactly one element that contains everything else.

Correct:

XML

Wrong (most common beginner mistake):

XML

Real consequence: Most XML parsers throw error immediately: “The markup in the document following the root element must be well-formed.”

Tip: If you have many similar items → wrap them in a plural/root name:

XML

Rule 2 – Every Opening Tag Must Have a Matching Closing Tag

XML is not like HTML (where some tags can be left open).

Correct:

XML

Wrong:

XML

Two correct ways to write empty elements:

XML

Common mistake:

XML

Rule 3 – Tags Must Be Properly Nested (No Crossing)

Correct nesting = like properly nested folders or brackets.

Correct:

XML

Wrong (crossing):

XML

Visual mnemonic:

Think of tags like parentheses:

Correct: ({[ ]}) Wrong: ({[ )}] ← broken

Rule 4 – Tag Names Are Case-Sensitive

<Person> is not the same as <person> or <PERSON>.

Correct:

XML

Wrong:

XML

Very common mistake in real projects:

XML

Recommendation: Choose one naming style and stick to it:

  • CamelCase → <ProductDetails>
  • snake_case → <product_details>
  • kebab-case → <product-details> (less common in XML)

Rule 5 – Attribute Values Must Always Be Quoted

Always use ” or ‘ — never leave them without quotes.

Correct:

XML

Wrong:

XML

Also correct (single quotes):

XML

Tip: Use double quotes “ by default — it’s the most common convention.

Rule 6 – Special Characters in Text Must Be Escaped

These characters have special meaning in XML → you must escape them when they are normal text:

Character Must write as Meaning / Purpose
< &lt; Starts a tag
> &gt; Ends a tag
& &amp; Starts an entity (escape sequence)
&quot; Used for attribute values (if inside “)
&apos; Used for attribute values (if inside ‘)

Correct:

XML

Wrong:

XML

Rule 7 – Attribute Names Must Be Unique Within the Same Element

You cannot have two attributes with the same name on one element.

Wrong:

XML

Correct:

XML

Rule 8 – The XML Declaration (optional but strongly recommended)

Correct placement: must be the very first thing in the file (no spaces, no comments before it)

Correct:

XML

Wrong:

XML

Most common form in 2025–2026:

XML

Full Realistic Example – All Rules Applied

XML

All rules followed:

  • One root: <invoice>
  • All tags closed
  • Proper nesting
  • Case consistent
  • Attributes quoted
  • No unescaped special characters
  • No duplicate attributes
  • Declaration at the top

Quick Summary Table – XML Syntax Rules at a Glance

# Rule Must Do Common Mistake
1 One root element Exactly one top element Multiple top-level tags
2 Every tag closed <tag>…</tag> or <tag/> Missing closing tag
3 Proper nesting Inner tags close before outer Crossing tags: <b><i></b></i>
4 Case-sensitive names <Name> ≠ <name> Mixing cases
5 Attributes in quotes id=”101″ id=101
6 Escape special chars < → &lt;, & → &amp; Writing <, & directly in text
7 No duplicate attributes One name per attribute per element id=”a” id=”b”
8 XML declaration first <?xml … ?> at very beginning Comment or space before declaration

Would you like to go deeper into any of these next?

  • More examples of real-world escaping (prices, descriptions, code snippets)
  • What happens when you break each rule (exact error messages)
  • Well-formed vs valid XML (big difference)
  • How to check if your XML is well-formed quickly
  • Common copy-paste mistakes from HTML/JSON
  • Next topic: Attributes vs Child Elements in detail

Just tell me what you want to practice or clarify more! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *