I have another one of those "is it done in XML" questions (my last one about xml comments hasn't been answered if anyone has a good explanation)

I was just wondering if anyone, anywhere would:

  1. Use multiple root elements in an XML document
  2. Put text content outside of a root element

W3C discourages these practices, Javascript's DOMParser doesn't even allow these cases, and I can't think of one sane reason to do either of these things. However, I know how bizarre some implementations of XML have been, so I wouldn't be surprised.

Does anyone have any real world examples where this would be done? I will also accept an answer that specifies if other mainstream parsers allow doing either of these.

3 Answers 3


As per the standard, an XML document must have exactly one root element in order to be well formed XML. Any parser that accepts otherwise is wrong, and any XML document that is structured any different is not in fact an XML document.

It is not merely discouraged by W3C, but simply against the standard.

  • I've seen plenty of XML that doesn't follow the standard, from not using quotes correctly to closing tags inaccurately. Ticks me off too ;) I just need to know if you have personally ever seen code that goes against these two particular standards, and what it was for. Apr 7, 2012 at 16:02
  • 3
    @JeffreySweeney then the "XML" gets rejected by any compliant XML parser.
    – user1249
    Apr 7, 2012 at 18:09
  • @JeffreySweeney a very simple test is to open the XML "document" in Internet Explorer. If it complains, it is an invalid XML document. This also has the benefit of being available to everyone and having a good reputation.
    – user1249
    Jun 1, 2012 at 8:11
  • 1
    @ThorbjørnRavnAndersen: I don't have IE available, and it has a pretty bad reputation among those I socialize with.
    – tdammers
    Jun 1, 2012 at 10:05
  • 1
    Not everyone is a Windows user.
    – tdammers
    Jun 1, 2012 at 10:50

As a "real world" example of multiple roots and intervening text code, consider the XML produced by "mysql -xml".

A compound query (or stored procedure) can return multiple result sets, each starting with the root ... . If one of the subqueries has an error, then mysql produces a single non-XML line "ERROR xxxx - message" instead of XML.

Parsing mysql's XML output involves both multiple roots as well as textual messages.

I'd like to say mysql is messed up and should be fixed, but the reality is, I need to parse mysql output as it currently exists.

  • That's very good to know. I can only hope that that's a fringe case... clients wouldn't likely need to edit MySQL XML output I don't think. Thank you very much! Jun 1, 2012 at 14:48

This can be handled by specifying the snippet in question as an external system entity.

<xml version="1.0">

<!DOCTYPE root [
<!ENTITY about SYSTEM "path/to/about.xml">


The W3C recommendation use the term "Parameter-entity" for this. See http://www.w3.org/TR/2004/REC-xml-20040204/#dt-PERef for the formal definition.

  • So, it is "kind of" allowed in the DOCTYPE assuming it follows the correct syntax. My concern was more for those who hire inexperienced XML coders that write XML their own way. My code does handle a variable DOCTYPE definition too ;) Apr 7, 2012 at 18:18
  • You can read it. Thought that was your concern.
    – user1249
    Apr 7, 2012 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.