Root element, case sensitivity

Every XML document must have exactly one root element that wraps all the others, and XML is case-sensitive, so <Event> and <event> are different tags and a start tag must be closed by an end tag of the identical case.

8 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Two errors that look like nothing

Two of the easiest ways to break an XML file leave it looking almost right. First, having two elements sitting at the top with no single wrapper. Second, closing a <name> tag with </Name>.

Both pass a quick glance and both make the document not well-formed, so Android refuses the layout. They come down to two rules: exactly one root element, and XML is case-sensitive. Small rules, frequent marks.

Theory

One trunk, exact names

A family tree has one trunk at the top that everything branches from; you cannot have two separate trunks in a single tree. XML is the same: one root element holds all the rest. And the labels are like passwords: case matters. Aditi and aditi are different logins; <Event> and <event> are different tags. Close a tag with the wrong case and XML says 'that is not the same name'.

Theory

The two rules

  • One root element: an XML document must have exactly one outermost element that wraps everything else. Two elements at the top level is invalid.
  • Case sensitivity: tag and attribute names are case-sensitive. <Note>, <note>, and <NOTE> are three different tags. A start tag must be closed by an end tag of the identical case: <name>...</name>, never </Name>.

In Android this bites on attributes too: it is android:layout_width, not android:Layout_Width.

Practical

Right and wrong

<!-- WRONG: two elements at the top, no single root -->
<name>Aditi</name>
<year>2</year>

<!-- RIGHT: one root <registration> wraps both -->
<registration>
  <name>Aditi</name>
  <year>2</year>
</registration>

<!-- WRONG: case mismatch on open/close -->
<Event>Robotics</event>    <!-- <Event> vs </event> -->

<!-- RIGHT: identical case -->
<event>Robotics</event>

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Why does an Android layout always start with one tag?

Open any FestConnect layout and the whole thing is inside a single <LinearLayout>...</LinearLayout> (or one ConstraintLayout). Connect that to the root-element rule. Could a layout have a TextView and a Button both at the very top level?

Show the answer

No. Because XML needs exactly one root element, a layout file must have a single outermost container, so LinearLayout (or ConstraintLayout) wraps everything. A TextView and a Button cannot both sit at the top level; they must live inside that one root container. That is why every Android layout opens and closes with a single layout tag: it is the XML root, obeying the one-root rule.

Quiz

Which XML fragment is well-formed?

  1. <data><a>1</a><b>2</b></data>
  2. <a>1</a><b>2</b>
  3. <Name>Aditi</name>
  4. <data><a>1</a><b>2</data></b>
Show the answer

<data><a>1</a><b>2</b></data>

Option A has a single root <data> wrapping <a> and <b>, correctly nested and closed, so it is well-formed. B has two top-level elements and no root (invalid). C mismatches case: <Name> closed by </name> (invalid). D crosses the nesting: </data> before </b> (invalid). Only A obeys one-root, matched case, and clean nesting at once.

Watch out

Root and case traps

1. Two roots: forgetting the single wrapper element. Always enclose siblings in one root (like <registration> or <LinearLayout>).

2. Case mismatch on close: <name>...</Name> fails. Match the exact case.

3. Wrong-case Android attributes: android:Text will not be recognised; it is android:text. XML case sensitivity applies to attribute names, not just tags.

Theory

Next: the two sections of a document

You now know a document has one root. But there is content before the root too: the declaration and optional comments. The next lesson names the two parts of an XML document, the prolog section (declaration and friends) and the document element section (the root and its contents), tidying up how a complete XML file is organised.

Summary

Key takeaways

  • An XML document must have exactly one root element wrapping all others; two top-level elements is invalid.
  • XML is case-sensitive: <Event> and <event> are different tags.
  • A start tag must be closed by an end tag of the identical case (<name>...</name>, not </Name>).
  • Android layouts have a single root container (LinearLayout/ConstraintLayout) because of the one-root rule.
  • Case sensitivity applies to attribute names too: android:text, not android:Text.
  • Memory hook: one trunk, exact names.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from XML (Extensible Markup Language)

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Root element, case sensitivity · Mobile Application Development - 1 (option B) · Gri-Learn