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

  • &lt; for <
  • &gt; for >
  • &amp; for &
  • &quot; for "
  • &apos; 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.

Ready to go beyond the basics?

Get hands-on training, live mentorship, and placement support with the Data Analytics Course at Uncodemy.

Explore XML Course →