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:
|
0 1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0"?> <library> <book>...</book> <book>...</book> </library> |
Wrong (most common beginner mistake):
|
0 1 2 3 4 5 6 7 8 9 |
<?xml version="1.0"?> <book>...</book> <book>...</book> <!-- ERROR: two root elements --> |
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:
|
0 1 2 3 4 5 6 7 8 9 |
<students> <!-- good root --> <student>...</student> <student>...</student> </students> |
Rule 2 – Every Opening Tag Must Have a Matching Closing Tag
XML is not like HTML (where some tags can be left open).
Correct:
|
0 1 2 3 4 5 6 7 8 |
<name>Riya Sharma</name> <age>19</age> <city>Delhi</city> |
Wrong:
|
0 1 2 3 4 5 6 7 8 |
<name>Riya Sharma <age>19</age> <!-- ERROR: <name> was never closed --> |
Two correct ways to write empty elements:
|
0 1 2 3 4 5 6 7 8 9 10 |
<!-- Style 1 – short / self-closing (most popular today) --> <photo src="profile.jpg"/> <!-- Style 2 – long form (also correct) --> <photo src="profile.jpg"></photo> |
Common mistake:
|
0 1 2 3 4 5 6 7 8 |
<br> <!-- wrong in XML --> <br/> <!-- correct --> <br /> <!-- also correct (space before / is allowed) --> |
Rule 3 – Tags Must Be Properly Nested (No Crossing)
Correct nesting = like properly nested folders or brackets.
Correct:
|
0 1 2 3 4 5 6 7 8 9 |
<book> <title>Atomic Habits</title> <author>James Clear</author> </book> |
Wrong (crossing):
|
0 1 2 3 4 5 6 7 8 9 10 |
<book> <title>Atomic Habits <author>James Clear</title> </author> <!-- ERROR: tags crossed – invalid nesting --> |
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:
|
0 1 2 3 4 5 6 7 8 |
<Person> <FirstName>Akash</FirstName> </Person> |
Wrong:
|
0 1 2 3 4 5 6 7 8 9 |
<Person> <firstname>Akash</firstname> <!-- ERROR: case does not match --> </Person> |
Very common mistake in real projects:
|
0 1 2 3 4 5 6 7 8 |
<!-- Many people coming from HTML do this --> <ItemCode>ABC123</ItemCode> <itemcode>ABC123</itemcode> <!-- different tag! --> |
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:
|
0 1 2 3 4 5 6 |
<book id="B784" language="English" inStock="true"> |
Wrong:
|
0 1 2 3 4 5 6 7 |
<book id=B784 language=English inStock=true> <!-- ERROR: missing quotes --> |
Also correct (single quotes):
|
0 1 2 3 4 5 6 |
<book id='B784' language='English'> |
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 |
|---|---|---|
| < | < | Starts a tag |
| > | > | Ends a tag |
| & | & | Starts an entity (escape sequence) |
| “ | " | Used for attribute values (if inside “) |
| ‘ | ' | Used for attribute values (if inside ‘) |
Correct:
|
0 1 2 3 4 5 6 7 8 9 |
<description> Price is &lt; 500 &amp; discount &gt; 20% He said: &quot;Best book ever!&quot; </description> |
Wrong:
|
0 1 2 3 4 5 6 7 8 9 |
<description> Price is < 500 & discount > 20% <!-- Parser thinks < and > start new tags → ERROR --> </description> |
Rule 7 – Attribute Names Must Be Unique Within the Same Element
You cannot have two attributes with the same name on one element.
Wrong:
|
0 1 2 3 4 5 6 7 8 |
<product id="P101" id="P102"> <!-- ERROR: duplicate attribute name --> </product> |
Correct:
|
0 1 2 3 4 5 6 |
<product id="P101" code="P102"> |
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:
|
0 1 2 3 4 5 6 7 |
<?xml version="1.0" encoding="UTF-8"?> <root>...</root> |
Wrong:
|
0 1 2 3 4 5 6 7 8 |
<!-- comment before declaration --> <?xml version="1.0"?> <!-- ERROR in many strict parsers --> |
Most common form in 2025–2026:
|
0 1 2 3 4 5 6 |
<?xml version="1.0" encoding="UTF-8"?> |
Full Realistic Example – All Rules Applied
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?xml version="1.0" encoding="UTF-8"?> <invoice number="INV/2025/0789" date="2025-06-22"> <seller> <name>TechTrend Innovations Pvt Ltd</name> <gstin>36AAECT4567P1Z2</gstin> </seller> <buyer> <name>Creative Minds Academy</name> <address> <city>Bengaluru</city> <state>Karnataka</state> <pin>560055</pin> </address> </buyer> <items> <item id="ITM-001"> <description>Online Training License – 1 year</description> <quantity>1</quantity> <unitPrice>185000.00</unitPrice> </item> </items> <totals> <grandTotal currency="INR">218300.00</grandTotal> <note>Payment due within 15 days</note> </totals> <legal>Goods once sold will not be taken back.</legal> </invoice> |
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 | < → <, & → & | 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! 😊
