XML Root Element
Estimated study time: 7 minutes.
The root element is the single top-level element that wraps every other element in an XML document. Every well-formed XML document must have exactly one.
Example
<?xml version="1.0" encoding="UTF-8"?>
<course>
<title>Data Analytics</title>
<duration>4 Months</duration>
</course>
Here, course is the root element. Everything else in the document — title, duration — lives inside it.
Rules for the Root Element
- There can be only one root element per document.
- It must fully enclose every other element — nothing can exist outside it.
- It comes immediately after the XML declaration and prolog.
What Happens Without One?
<!-- Invalid: two top-level elements, no single root -->
<title>Data Analytics</title>
<duration>4 Months</duration>
A parser will reject this document as not well-formed, because there's no single element containing both pieces of data.
<catalog> or <data> rather than leaving multiple top-level elements.What Can Appear Outside the Root Element
The rule "nothing outside the root" really means no elements or text outside it. A few other items are allowed before or after the root:
- The XML declaration, which must be the very first thing in the file, with nothing before it, not even a blank line or space.
- Comments, such as
<!-- generated by export tool -->. - Processing instructions, such as a stylesheet link
<?xml-stylesheet ...?>. - A DOCTYPE declaration, placed before the root, which names the root element and can point to a DTD.
- Whitespace between these items.
<?xml version="1.0" encoding="UTF-8"?>
<!-- Course export -->
<!DOCTYPE catalog>
<catalog>
<course id="101"><title>Data Analytics</title></course>
</catalog>
<!-- end of file -->
This document is well-formed. If you add the word done after the closing </catalog> tag, it is not, because stray text is outside the root.
Reading the Root Element in Code
Every XML parser gives you the root as the entry point to the whole document. In Python's built-in ElementTree:
import xml.etree.ElementTree as ET
xml_text = """<catalog>
<course id="101"><title>Data Analytics</title></course>
<course id="102"><title>Python</title></course>
</catalog>"""
root = ET.fromstring(xml_text)
print(root.tag) # catalog
for course in root:
print(course.get("id"), course.find("title").text)
In browser JavaScript, the same element is available as documentElement:
const doc = new DOMParser().parseFromString(xmlText, "application/xml");
console.log(doc.documentElement.nodeName); // catalog
If parsing fails because the root is missing, Python raises a ParseError, and the browser returns a document containing a parsererror element, so always check for that before reading data.
Namespaces and Schemas on the Root
The root is the usual place to declare namespaces, so every child element can use them:
<catalog xmlns="https://example.com/catalog"
xmlns:p="https://example.com/pricing">
<course><title>Python</title><p:fee>0</p:fee></course>
</catalog>
An XML Schema (XSD) or DTD also decides which element is allowed to be the root. A document that is well-formed can still fail validation if its root name does not match what the schema expects. With a default namespace, remember that ElementTree reports the root tag as the namespace URI in curly braces followed by catalog, not just catalog, which often confuses beginners.
Naming the Root Well, and Errors to Avoid
- Use a plural or collection name, such as
ordersoremployees, when the document holds many records of one kind. - Keep the name stable; other systems often rely on it to recognise the file type.
- Do not concatenate two XML files directly. The result has two roots and fails to parse; wrap the contents in a new root instead.
- Watch for invisible characters or spaces before the declaration when files are generated by scripts.
- When an API returns several fragments, wrap them before passing them to a parser.