Chapters: JavaScript HTML & CSS JSON XML YAML ← Full TOC

← XML Index

📄 XML Syntax — Elements, attributes, namespaces, and well-formed rules

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.

📋 Reference

ItemSyntax / ValueDescription
<?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 elementSingle root wraps all other elementsEvery XML doc has exactly one root
Case-sensitive<Name> ≠ <name>XML tag names are case-sensitive
Nesting<a><b></b></a> — correctTags must be properly nested — no overlapping
Entity refs&lt; &gt; &amp; &quot; &apos;Escape < > & " ' in content/attributes
CDATA<![CDATA[ <unescaped content> ]]>Literal text block — no escaping needed
Comment<!-- comment text -->XML comment — cannot contain --
Namespacexmlns:ns="http://example.com"Avoid name collisions across vocabularies
Namespace prefix<ns:element>Prefix maps to namespace URI
Default namespacexmlns="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-formedAll tags closed, properly nested, one rootMinimum requirement for valid XML
ValidWell-formed + conforms to DTD or XSDValidated against schema
EncodingUTF-8 (default) / UTF-16 / ISO-8859-1Character encoding — use UTF-8
WhitespacePreserved inside elementsWhitespace is significant in XML content
PI<?xml-stylesheet type="text/xsl" href="style.xsl"?>Link XSLT stylesheet

💡 Example

<?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 &amp; Node.js</title>  <!-- & escaped as &amp; -->
          <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>

🏠 Index  |  XPath →