XML — Syntax
XML Declaration
<?xml version="1.0" encoding="UTF-8"?>
Root Element
Every XML document must have exactly one root element:
<root>
<child>Content</child>
</root>
Closing Tags
All tags must be closed:
<paragraph>Text</paragraph>
<empty/>
Case Sensitivity
Tags are case-sensitive:
<p>Lowercase</p>
<P>Uppercase</P>
Nesting
Elements must be properly nested:
<!-- Correct -->
<p><b>Bold text</b></p>
<!-- Incorrect -->
<p><b>Text</p></b>
Attributes
<book genre="fiction" pages="200">
Special Characters
| Character | Entity |
|---|---|
| < | < |
| > | > |
| & | & |
| " | " |
| ' | ' |
<note>Price: 5 < 10 & 10 > 5</note>
Whitespace
XML preserves whitespace:
<name>
John
Doe
</name>
Rules Summary
- Must have a root element
- Tags must be closed
- Tags are case-sensitive
- Must be properly nested
- Attribute values must be quoted
- Special characters must be escaped
Mini Practice
- Create a well-formed XML document
- Use proper nesting
- Escape special characters
- Add attributes to elements
Up Next
Continue with Elements — creating XML elements.
Related Topics
Frequently Asked Questions about Syntax
What is Syntax in XML?
Syntax is a fundamental concept in XML. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Syntax?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Syntax.
Why is Syntax important in XML?
Syntax is essential for XML development. Understanding this concept will help you write better code and solve real-world problems more effectively.