Aral Code

Getting Started Lesson 2 of 9

XML Syntax Rules — Well-Formed Documents

The rules every XML document must follow — the XML declaration, one root element, closing every tag, proper nesting, case sensitivity, quoted attributes, entities and comments.

XML is strict. A document that follows all the syntax rules is called well-formed. If even one rule is broken, XML programs refuse to read the document at all, unlike HTML, where browsers quietly fix mistakes.

The XML declaration

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

This optional first line says the XML version and the character encoding. If you include it, it must be the very first thing in the file, with nothing before it, not even a space.

Rule 1: exactly one root element

<!-- Wrong: two top-level elements -->
<student>Ana</student>
<student>Ben</student>

<!-- Right: one root wraps everything -->
<students>
  <student>Ana</student>
  <student>Ben</student>
</students>

Rule 2: every element must be closed

<name>Ana</name>      <!-- closing tag -->
<photo/>               <!-- self-closing empty element -->

HTML lets you write <br> without closing it; XML doesn't.

Rule 3: proper nesting

<!-- Wrong -->
<b><i>Mabuhay</b></i>
<!-- Right -->
<b><i>Mabuhay</i></b>

The last element opened must be the first one closed, like stacked plates.

Rule 4: tags are case-sensitive

<Name>Ana</name> is an error: the opening and closing tags must match exactly.

Rule 5: attribute values must be quoted

<student lrn="136728140001" grade='10'>Ana</student>

Single or double quotes are fine, but they're required.

Rule 6: special characters need entities

< and & can't appear directly in text, because they start tags and entities. Use these five predefined entities:

CharacterEntity
<&lt;
>&gt;
&&amp;
"&quot;
'&apos;
<store>Kape &amp; Tinapay</store>
<rule>Grade &lt; 75 means remediation</rule>

For longer text with many special characters, like code, use a CDATA section, where everything is taken literally:

0

Comments and whitespace

<!-- This is a comment. It can't contain two dashes in a row. -->

Whitespace inside text is kept, unlike in HTML, though most programs ignore the indentation between elements.

Element naming rules

  • Names can contain letters, digits, hyphens, underscores and periods.
  • They must start with a letter or underscore, not a digit: <1stPlace> is invalid.
  • No spaces: <first name> is invalid; use <first_name> or <firstName>.
  • Names starting with "xml" are reserved.

Check it yourself

This example uses the browser's XML parser to test whether text is well-formed. Edit the XML and run it again:

Example A well-formedness checker
<textarea id="x" rows="8" cols="44"><students>
  <student lrn="136728140001">
    <name>Ana Reyes</name>
    <grade>10</grade>
  </student>
</students></textarea>
<br><button onclick="check()">Check</button>
<p id="out"></p>
<script>
  function check() {
    const doc = new DOMParser().parseFromString(document.getElementById("x").value, "application/xml");
    const err = doc.querySelector("parsererror");
    document.getElementById("out").textContent = err
      ? "❌ Not well-formed: " + err.textContent.split("\n")[0]
      : "✅ Well-formed! Root element: <" + doc.documentElement.nodeName + ">";
  }
  check();
</script>

Try removing </grade>, changing </name> to </Name>, or deleting the quotes around the LRN.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. How many root elements can an XML document have?
2. Which is a correctly closed empty element?
3. How do you write "&" in XML text?
4. Which element name is valid?

By Sir Reyel · Last updated 2026-12-04