Theory
A file in two halves
Look again at a full FestConnect XML file. The top has a line about the file itself (<?xml ...?>), maybe a comment. Below it sits the real data, wrapped in one root element.
XML formally names these two halves: the prolog section (the header material) and the document element section (the data). Knowing which is which, and that the prolog comes first, is a small but reliable exam question and clarifies how every XML file is laid out.
Theory
A letter's letterhead and its body
A formal letter has a letterhead at the top: the date, reference number, maybe a note about the paper. Then comes the body, the actual message. The letterhead describes and precedes; the body carries the content. An XML document is the same: the prolog is the letterhead (declaration, comments, schema), and the document element is the body (the root and its data).
Theory
The two sections
- Prolog section (optional, at the top): the XML declaration (
<?xml version="1.0" encoding="UTF-8"?>), plus optional comments, processing instructions, and a DTD/schema reference (DOCTYPE). It describes the document. - Document element section (the body): the single root element and all its nested children and content. This is the actual data.
Order matters: the prolog comes first; then exactly one root. If the declaration is present, it must be the very first line.
Practical
Prolog and body, labelled
<!-- ===== PROLOG SECTION ===== -->
<?xml version="1.0" encoding="UTF-8"?>
<!-- a fest registration record -->
<!-- ===== DOCUMENT ELEMENT SECTION ===== -->
<registration>
<name>Aditi Shah</name>
<year>2</year>
<event>Robotics</event>
</registration>At a glance
The two sections at a glance
| Section | Contains | Required? |
|---|---|---|
| Prolog | declaration, comments, DTD/schema | optional |
| Document element | the single root + its data | required |
Think first
Which part is the data?
In the snippet, which section holds the actual registration values, and which merely describes the file? And if you deleted the whole prolog, would the file still hold its data?
Show the answer
The document element section (<registration>...</registration>) holds the actual data: the name, year, and event. The prolog (the <?xml ...?> line and comment) only describes the file, it carries no registration data. If you deleted the entire prolog, the file would still contain its data and could still be well-formed, because the prolog is optional. The one thing you cannot drop is the single root element, that is the required body.
Quiz
Where in an XML document does the prolog appear, and what must be true if an XML declaration is present?
- The prolog is at the top, before the root; the declaration must be the very first line
- The prolog is at the bottom, after the root element
- The prolog replaces the root element entirely
- The declaration can appear anywhere inside the root
Show the answer
The prolog is at the top, before the root; the declaration must be the very first line
The prolog is the header section at the top of the file, before the document element (root), and if an XML declaration is included it must be the very first line, with nothing before it (A). The prolog is never at the bottom (B), and it does not replace the root (C), which is the required data body. The declaration cannot float inside the root (D). Prolog first, then one root.
Watch out
Document-structure traps
1. Content before the declaration: even a blank line or comment before <?xml ...?> makes it invalid. The declaration, if present, is line one.
2. Calling the prolog 'the data': the prolog only describes the file; the document element holds the data.
3. Two roots in the body: the document element section still obeys the one-root rule from the previous lesson.
Theory
Next: the declaration line up close
The prolog's most important piece is the XML declaration. The final XML lesson zooms into it: what version and encoding mean, which parts are required, the order of its attributes, and why it must be lowercase and first. Then you return to building FestConnect's screen with real Android widgets in Unit 4.
Summary
Key takeaways
- An XML document has two sections: the prolog (top) and the document element (body).
- Prolog (optional): the XML declaration, comments, processing instructions, and any DTD/schema.
- Document element (required): the single root element and all its nested data.
- The prolog comes first; if a declaration is present it must be the very first line, with nothing before it.
- Deleting the prolog can still leave a valid file; deleting the root cannot.
- Memory hook: a letter's letterhead (prolog) and its body (document element).