SyntaxStudy
Sign Up
XML Beginner 8 min read

XML Syntax Rules

XML has strict syntax rules that must be followed to create well-formed XML documents. Unlike HTML, XML is case-sensitive and every opening tag must have a closing tag.

Example
<?xml version="1.0" encoding="UTF-8"?>

<!-- XML Syntax Rules -->

<!-- 1. Must have a root element -->
<root>

  <!-- 2. Tags are case-sensitive: <Name> != <name> -->
  <Name>Alice</Name>
  <name>Bob</name>

  <!-- 3. All tags must be closed -->
  <self-closing-tag />   <!-- self-closing -->
  <normal>content</normal>

  <!-- 4. Attributes must be quoted -->
  <item id="1" color="red">Value</item>

  <!-- 5. Proper nesting required -->
  <!-- WRONG: <a><b></a></b> -->
  <!-- RIGHT: -->
  <a><b></b></a>

  <!-- 6. Special characters must be escaped -->
  <comparison>5 &lt; 10 &amp;&amp; 10 &gt; 5</comparison>
  <!-- &lt; = <  &gt; = >  &amp; = &  &quot; = "  &apos; = ' -->

  <!-- 7. Comments -->
  <!-- This is a comment -->

</root>