XML Reference
Complete guide to XML syntax and structure
📄 What is XML?
XML (eXtensible Markup Language) is a markup language for storing and transporting data. It's both human-readable and machine-readable, making it perfect for data exchange between systems.
<?xml version="1.0" encoding="UTF-8"?>
<message>
<text>Hello XML!</text>
</message>
XML Syntax Rules
XML follows strict syntax rules that ensure documents are well-formed and valid. Understanding these fundamental rules is essential for creating proper XML documents that can be parsed correctly by any XML processor.
Tags Must Close
Every opening tag needs a closing tag
<book>Content</book>
Case Sensitive
Tags are case-sensitive
<Book> ≠ <book>
Proper Nesting
Elements must be properly nested
<a><b>Text</b></a>
Root Element
One root element required
<root>...</root>
🔹 Basic XML Document
A complete XML document with declaration and structure:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="fiction">
<title>The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
<year>1925</year>
<price>10.99</price>
</book>
</bookstore>
Structure:
Declaration: <?xml version="1.0"?>
Root: bookstore
Child: book (with attribute)
Data: title, author, year, price
🔹 XML Elements
Elements are the building blocks of XML documents:
<!-- Element with content -->
<name>John Doe</name>
<!-- Empty element (two ways) -->
<linebreak></linebreak>
<linebreak />
<!-- Nested elements -->
<person>
<firstname>John</firstname>
<lastname>Doe</lastname>
</person>
🔹 XML Attributes
Attributes provide additional information about elements:
<!-- Single attribute -->
<book category="fiction">Title</book>
<!-- Multiple attributes -->
<img src="photo.jpg" alt="Photo" width="500" />
<!-- Attributes vs Elements -->
<person gender="female">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
Attribute Rules:
- Must be quoted (single or double quotes)
- Cannot contain multiple values
- Cannot contain tree structures
- Not easily expandable
🔹 XML Namespaces
Namespaces prevent element name conflicts:
<?xml version="1.0"?>
<root xmlns:h="http://www.w3.org/HTML"
xmlns:f="http://www.example.com/furniture">
<h:table>
<h:tr>
<h:td>Cell</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>Coffee Table</f:name>
<f:width>80</f:width>
</f:table>
</root>
🔹 XML Comments
Add comments to document your XML:
<?xml version="1.0"?>
<!-- This is a comment -->
<data>
<!-- Multi-line comment
can span multiple lines
for detailed explanations -->
<item>Value</item>
</data>
🔹 Special Characters
Escape special characters using entities:
<message>
<text>Price: 5 < 10</text>
<quote>"Hello"</quote>
<company>AT&T</company>
</message>
Entity References:
- < = < (less than)
- > = > (greater than)
- & = & (ampersand)
- " = " (quote)
- ' = ' (apostrophe)
🔹 CDATA Sections
Use CDATA for text containing special characters:
<script>
<![CDATA[
function compare(a, b) {
if (a < b) {
return -1;
}
return 1;
}
]]>
</script>