XML (eXtensible Markup Language) is a strict markup language for structured data. Unlike HTML, XML tags are user-defined. Every XML document must be well-formed: properly nested, closed tags, quoted attributes, and a single root element.
| Item | Syntax / Value | Description |
|---|---|---|
| <?xml?> | <?xml version="1.0" encoding="UTF-8"?> | XML declaration — recommended first line |
| Element | <tagName>content</tagName> | Basic building block — must be properly closed |
| Empty element | <br/> or <br></br> | Self-closing element |
| Attribute | <element attr="value"> | Attribute values MUST be quoted |
| Root element | Single root wraps all other elements | Every XML doc has exactly one root |
| Case-sensitive | <Name> ≠ <name> | XML tag names are case-sensitive |
| Nesting | <a><b></b></a> — correct | Tags must be properly nested — no overlapping |
| Entity refs | < > & " ' | Escape < > & " ' in content/attributes |
| CDATA | <![CDATA[ <unescaped content> ]]> | Literal text block — no escaping needed |
| Comment | <!-- comment text --> | XML comment — cannot contain -- |
| Namespace | xmlns:ns="http://example.com" | Avoid name collisions across vocabularies |
| Namespace prefix | <ns:element> | Prefix maps to namespace URI |
| Default namespace | xmlns="http://example.com" | Default namespace for unprefixed elements |
| Processing instruction | <?pi-target data?> | Instruction to the XML processor |
| DOCTYPE | <!DOCTYPE root SYSTEM "file.dtd"> | Link to Document Type Definition |
| Well-formed | All tags closed, properly nested, one root | Minimum requirement for valid XML |
| Valid | Well-formed + conforms to DTD or XSD | Validated against schema |
| Encoding | UTF-8 (default) / UTF-16 / ISO-8859-1 | Character encoding — use UTF-8 |
| Whitespace | Preserved inside elements | Whitespace is significant in XML content |
| PI | <?xml-stylesheet type="text/xsl" href="style.xsl"?> | Link XSLT stylesheet |
<?xml version="1.0" encoding="UTF-8"?>
<!-- MyWebUniversity Student Database -->
<university xmlns="https://www.mywebuniversity.com/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.mywebuniversity.com/schema students.xsd"
version="2.0"
created="2026-06-25">
<department name="Computer Science" code="CS">
<student id="1001" status="active">
<name>
<first>Alice</first>
<last>Smith</last>
</name>
<email>alice@mywebuniversity.com</email>
<gpa>3.95</gpa>
<enrolled>2024-09-01</enrolled>
<graduation/> <!-- empty element — date not yet set -->
<courses>
<course code="CS101" credits="3" grade="A">
<title>Introduction to Python</title>
<instructor>Dr. Brown</instructor>
</course>
<course code="CS201" credits="3" grade="A-">
<title>JavaScript & Node.js</title> <!-- & escaped as & -->
<instructor>Dr. Green</instructor>
</course>
<course code="CS301" credits="4" grade="A+">
<title>Data Science with R</title>
<notes><![CDATA[Student excelled at <ggplot2> and dplyr::filter()]]></notes>
</course>
</courses>
<tags>
<tag>dean-list</tag>
<tag>scholarship</tag>
<tag>remote</tag>
</tags>
</student>
<student id="1002" status="active">
<name><first>Bob</first><last>Jones</last></name>
<email>bob@mywebuniversity.com</email>
<gpa>3.72</gpa>
<enrolled>2024-09-01</enrolled>
<graduation/>
<courses/>
<tags/>
</student>
</department>
</university>