XML Syntax
Rules for writing valid XML documents
📝 XML Syntax Rules
XML has strict syntax rules that must be followed. All tags must be properly closed, nested correctly, and case-sensitive. Following these rules ensures your XML is valid and parseable.
<?xml version="1.0" encoding="UTF-8"?>
<message>
<text>Proper syntax is important!</text>
</message>
Essential Syntax Rules
Closing Tags
All tags must be closed
<tag>Content</tag>
<empty/>
Case Sensitive
Tags are case-sensitive
<Name>...</Name>
<!-- Not same as -->
<name>...</name>
Proper Nesting
Elements must nest correctly
<outer>
<inner>...</inner>
</outer>
One Root
Must have single root element
<root>
<!-- All content -->
</root>
🔹 Closing Tags Rule
In XML, every opening tag must have a corresponding closing tag. This is stricter than HTML where some tags can be left unclosed.
🔸 Correct Syntax:
<person>
<name>John Doe</name>
<age>30</age>
</person>
🔸 Incorrect Syntax:
<person>
<name>John Doe
<age>30</age>
</person>
<!-- ERROR: name tag not closed -->
🔸 Self-Closing Tags:
<!-- Empty elements can self-close -->
<linebreak/>
<image src="photo.jpg"/>
<empty/>
🔹 Case Sensitivity
XML tags are case-sensitive, meaning <Name> and <name> are treated as completely different elements. Always use consistent casing throughout your document.
🔸 Correct (Matching Case):
<Book>
<Title>XML Guide</Title>
<Author>Jane Smith</Author>
</Book>
🔸 Incorrect (Mismatched Case):
<Book>
<Title>XML Guide</title>
<Author>Jane Smith</AUTHOR>
</book>
<!-- ERROR: Case mismatch in closing tags -->
Best Practice:
- Use lowercase for all tags (most common)
- Or use camelCase consistently
- Never mix different casing styles
🔹 Proper Nesting
XML elements must be properly nested - child elements must be closed before their parent elements. Think of it like properly nested parentheses in mathematics.
🔸 Correct Nesting:
<outer>
<middle>
<inner>Content</inner>
</middle>
</outer>
🔸 Incorrect Nesting:
<outer>
<middle>
<inner>Content</middle>
</inner>
</outer>
<!-- ERROR: Tags overlap incorrectly -->
Think of it like boxes:
✅ Small box inside medium box inside large box
❌ Boxes overlapping or intersecting
🔹 Attribute Syntax
Attributes provide additional information about elements. They must always be enclosed in quotes and appear in the opening tag only.
🔸 Correct Attribute Syntax:
<book isbn="978-0-123456-78-9" language="English">
<title>XML Basics</title>
</book>
<image src="photo.jpg" width="300" height="200"/>
🔸 Incorrect Attribute Syntax:
<book isbn=978-0-123456-78-9>
<!-- ERROR: Missing quotes -->
<book isbn='123' isbn="456">
<!-- ERROR: Duplicate attribute -->
Attribute Rules:
- Always use quotes (single or double)
- No duplicate attributes in same element
- Attributes only in opening tags
- Use either 'single' or "double" quotes consistently
🔹 Special Characters
Some characters have special meaning in XML and must be escaped using entity references to be displayed as literal text.
🔸 Entity References:
<message>
<text>5 < 10 & 10 > 5</text>
<quote>"Hello" 'World'</quote>
</message>
Displays as:
5 < 10 & 10 > 5
"Hello" 'World'
Common Entities:
- < = < (less than)
- > = > (greater than)
- & = & (ampersand)
- " = " (double quote)
- ' = ' (apostrophe)
🔹 Comments in XML
Comments help document your XML code. They are ignored by parsers and can span multiple lines.
<?xml version="1.0"?>
<!-- This is a single-line comment -->
<data>
<!--
This is a multi-line comment
explaining the data structure
-->
<item>Value</item>
</data>
<!-- Comments can appear anywhere except inside tags -->
Comment Rules:
- Start with <!-- and end with -->
- Cannot contain double hyphens (--) inside
- Cannot be placed inside tags
- Can span multiple lines
🔹 Complete Valid XML Example
Here's a complete example demonstrating all syntax rules working together:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Product Catalog -->
<catalog>
<product id="101" available="true">
<name>Laptop</name>
<price currency="USD">899.99</price>
<description>
High-performance laptop with 16GB RAM & 512GB SSD
</description>
<tags>
<tag>electronics</tag>
<tag>computers</tag>
</tags>
<inStock/>
</product>
</catalog>
This XML is valid because:
✅ Has XML declaration
✅ Single root element (catalog)
✅ All tags properly closed
✅ Correct nesting
✅ Attributes in quotes
✅ Special characters escaped