0

What is the motivation behind the decision to have a single root element in an XML document?

Could someone explain why the XML design team made this decision?

kjhughes
  • 106,133
  • 27
  • 181
  • 240

3 Answers3

5

You're asking a design motivation question, which is ordinarily challenging to answer after-the-fact without merely speculating.

Fortunately, Tim Bray captured and recorded some of the design history and motivations behind XML, and says this regarding exactly one element, called the root, or document element:

Roots and Nests

This paragraph (which, strictly speaking is unnecessary - it is merely amplifying the consequences of the grammar) describes the essence of well-formedness. Simply stated, there has to be one element that contains everything else, and all the elements have to nest nicely within each other - no overlapping! All this "root" and "nest" terminology suggests trees, which is just fine.

The fact that XML requires a single root element is more important than you might think; given that we expect to be transmitting these documents over network links which, we all know, are sometimes slow, flaky, and unreliable, it's a really good idea if the beginning and (especially) the end of every document is clearly marked, so that even if the guy on the other end is slow in closing down the link, you know when you've got the whole message.

See also: What is the difference between root node, root element and document element in XML?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • While interesting, since XML is by definition a subset of SGML, Tim Bray wasn't at liberty to change SGML's assumption to have a single document element. Actually, SGML has CONCUR which allows interleaved/overlapping tags from multiple DOCTYPEs in a single document instance to form multiple "views" (each having their own unique single document element) over an instance. – imhotap Jun 20 '19 at 11:00
1

According to the Wikipedia definition of XML

Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. The W3C's XML 1.0 Specification and several other related specifications—all of them free open standards—define XML.

Basically XML is a set of rules to make sure every document has the same standard. If you have a couple of hours to kill and wants to get bored to dead, you can read the specification here.


Back to your question, it was a design decision to have only one root element in a document.

One of the main advantage is to allows to build a tree structure. It's easier to parse a logical tree with a predefined structure then a collection of node of different types floating around.

With a predefined structure, it's also easier to traverse the document and find the information you're looking for.

Justin Lessard
  • 10,804
  • 5
  • 49
  • 61
0

As stated in the XML specification:

Definition: There is exactly one element, called the root, or document element, no part of which appears in the content of any other element. [...] The elements, delimited by start- and end-tags, nest properly within each other.

choroba
  • 231,213
  • 25
  • 204
  • 289