XML - rules of XML
1. Every XML document must have a root element
-
All other elements must be nested within a single root (parent) element.
<bookstore>
<book>...</book>
</bookstore>
2. Tags must be properly closed
-
XML is strict about closing tags. Every opening tag must have a corresponding closing tag, or be self-closing.
<author>John</author>
<line-break /> <!-- Self-closing tag -->
3. Tags are case-sensitive
-
<Book>and<book>are considered different. You must consistently use the same case.
<Title>Correct</Title>
<title>Incorrect if you opened with Title</title>
4. Elements must be properly nested
-
Tags cannot overlap. Inner elements must be completely closed before the outer one.
<!-- Correct -->
<note><to>John</to><from>Jane</from></note>
<!-- Incorrect -->
<note><to>John<from>Jane</to></from></note>
5. Attribute values must be quoted
-
All attribute values must be enclosed in single or double quotes.
<book genre="fiction" year="2023">
...
</book>
6. Use of a prolog (optional but recommended)
-
The XML declaration typically appears at the top of the file.
<?xml version="1.0" encoding="UTF-8"?>
7. Special characters must be escaped
-
Use entity references for characters like
<,>,&,',":-
<→< -
>→> -
&→& -
'→' -
"→"
-
8. Whitespace is preserved
-
XML preserves spaces and line breaks inside elements unless specifically handled otherwise by the application parsing it.
9. Comments follow a strict format
-
XML comments must be enclosed in
<!-- -->and cannot contain--inside.
<!-- This is a valid XML comment -->
10. Custom tags are allowed
-
XML is extensible, meaning you can create your own tags, as long as they follow the rules above.