XML Syntax and Rules
Estimated study time: 15 minutes.
XML is strict about its syntax. A document that breaks these rules is considered not well-formed, and most parsers will refuse to process it. Here are the rules every XML document must follow.
1. One Root Element
Every document must have exactly one top-level element that wraps everything else.
2. Every Tag Must Be Closed
<!-- Correct -->
<title>Data Analytics</title>
<!-- Incorrect -->
<title>Data Analytics
3. Tags Are Case-Sensitive
<Title>...</title> <!-- invalid: Title and title don't match -->
4. Proper Nesting
<!-- Correct -->
<a><b>text</b></a>
<!-- Incorrect -->
<a><b>text</a></b>
5. Attribute Values Must Be Quoted
<student id="102">...</student>
6. Special Characters Must Be Escaped
<for<>for>&for&"for"'for'
7. Whitespace Is Preserved
Unlike HTML, XML parsers preserve whitespace exactly as written inside elements, which matters for text-heavy content.
💡 Tip: Run your XML through a validator before relying on it in a pipeline — a single unclosed tag can break an entire downstream process.