Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

up vote 12 down vote accepted

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.

share|improve this answer
    
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. –  Jeffrey Sweeney Apr 7 '12 at 16:02
3  
@JeffreySweeney then the "XML" gets rejected by any compliant XML parser. –  user1249 Apr 7 '12 at 18:09
    
@ThorbjørnRavnAndersen sounds fair to me. Accepting this answer. –  Jeffrey Sweeney Apr 7 '12 at 18:10
    
@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 '12 at 8:11
    
@ThorbjørnRavnAndersen: I don't have IE available, and it has a pretty bad reputation among those I socialize with. –  tdammers Jun 1 '12 at 10:05
show 6 more comments

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">
]>

<root>
&about;
</root>

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.

share|improve this answer
    
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 ;) –  Jeffrey Sweeney Apr 7 '12 at 18:18
    
You can read it. Thought that was your concern. –  user1249 Apr 7 '12 at 19:27
add comment

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.

share|improve this answer
    
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! –  Jeffrey Sweeney Jun 1 '12 at 14:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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