Skip to content

XML Syntax

XML (Extensible Markup Language) has a set of straightforward syntax rules. These rules ensure that XML documents are structured consistently, making them easy for both humans and software to read and process.

Unlike HTML, where some tags can be left unclosed (like <p> or <li> in older versions, or void elements like <br>, <img>), XML requires every element to have a corresponding closing tag. Elements that don’t contain content are called empty elements and can use a self-closing tag.

Correct XML:

<message>
<text>This is a paragraph.</text>
<line_break/> <!-- Self-closing tag for an empty element -->
</message>

Incorrect XML (missing closing tag for text):

<message>
<text>This is a paragraph.
</message>

Note: The XML declaration (e.g., <?xml version="1.0" encoding="UTF-8"?>) often seen at the start of an XML file is a processing instruction, not an element. It does not have a closing tag and is not considered part of the document’s element tree.

XML tags distinguish between uppercase and lowercase letters. The start tag <Message> is different from <message>. The start tag and end tag must use the exact same case.

Incorrect (mismatched case):

<Message>This is incorrect</message>

Correct:

<message>This is correct</message>

Tip: A common convention is to use lowercase for element and attribute names, but consistency within your project or standard is key.

Elements must be nested in the correct order. If an element starts inside another element, it must also end inside that element. Think of them like properly matched parentheses or Russian nesting dolls.

Incorrect Nesting (** closes after opens, but before closes):

<outer>**<i>This text is bold and italic**</i></outer>

Correct Nesting:

<outer>**<i>This text is bold and italic</i>**</outer>

In the correct example, the <i> element is opened and closed entirely within the boundaries of the ** element.

Every valid XML document must contain exactly one top-level element, known as the root element. All other elements must be descendants of this single root element. It acts as the container for the entire document content.

Example Structure:

<root_element>
<child_element>
<subchild_element>...</subchild_element>
</child_element>
<another_child/>
</root_element>

Attributes provide additional information about elements and appear within the start tag. Their values must always be enclosed in either single (’) or double (”) quotes.

Incorrect (attribute value important is not quoted):

<note priority=important>
<to>Alice</to>
<from>Bob</from>
</note>

Correct (using double quotes):

<note priority="important">
<to>Alice</to>
<from>Bob</from>
</note>

Correct (using single quotes):

<note priority='important'>
<to>Alice</to>
<from>Bob</from>
</note>

Tip: Choose one quote style (single or double) and use it consistently for better readability.

Some characters have predefined meanings in XML. For example, the less than symbol < signifies the start of a tag.

If you need to include these special characters as literal text content or within attribute values, you must use their corresponding entity references.

This will cause an XML parsing error:

<condition>if x < 5 then proceed</condition>

To fix this, replace < with its entity reference &lt;:

<condition>if x &lt; 5 then proceed</condition>

The five pre-defined entity references in XML are:

EntityCharacterDescription
<<Less than
>>Greater than
&&Ampersand
''Apostrophe (single quote)
""Quotation mark (double quote)

Note: While only < and & are strictly required to be escaped in element content, it’s good practice to also escape > (&gt;). Within attribute values enclosed in double quotes, " must be escaped as &quot;. Within attribute values enclosed in single quotes, ' must be escaped as &apos;. Using entities consistently improves clarity and prevents potential issues.

You can add comments to your XML documents for documentation or clarification. The syntax is the same as in HTML:

<!-- This is a comment. Comments are ignored by XML parsers. -->

Important: Comments cannot appear inside tags, and the sequence -- (double hyphen) is not allowed inside a comment itself.

By default, XML parsers preserve white space (spaces, tabs, newlines) found within element content. This differs from HTML browsers, which often collapse multiple white space characters into a single space.

Example:

<message>Hello World</message>

In the above, the multiple spaces between “Hello” and “World” are considered significant.

You can control white space handling more explicitly using the xml:space attribute (e.g., xml:space="preserve" or xml:space="default").

Different operating systems use different characters to represent line endings (e.g., Windows uses CR+LF, Unix/Linux/macOS use LF). XML parsers are required to normalize all line endings to a single LF character (Line Feed, \n) before processing.

This ensures consistent handling of text content regardless of the platform where the XML document was created.

An XML document that adheres to all the syntax rules described above is called a “Well-Formed” document. Being well-formed is the minimum requirement for an XML document to be successfully parsed and processed by XML tools.

Think of it like grammatical correctness in a sentence; if it’s not well-formed, the meaning (structure) cannot be understood.