Re: [xml] single root element

Hi Vojtech,

Although an XML document may only have a single document (root) element, an external entity does not have this restriction.

Take for example two documents, foo.xml and bar.xml:

foo.xml contains:

<!DOCTYPE foo [
  <!ENTITY bar SYSTEM "bar.xml">

and bar.xml contains:


then foo.xml is well-formed. bar.xml, if used as a standalone document, is not well-formed.

I sometimes use this trick for log files. The application writes log entries to the equivalent of bar.xml, one (parent) element per line. When I want to process the log file I use the equivalent of foo.xml.

Steve Ball

On 02/10/2013, at 6:13 PM, Vojtech Fried <vfried opentext com> wrote:

I have a slightly philosophical question. XML specs says that there can be only one root element in a XML document. However, libxml allows you to have any number of element children in a document node. Starting from a valid document, you can add or remove child elements of the document node freely. You get an error when you try to e.g. save such a document.
My view is that it is a “loophole”. Single root element is not enforced, because you could need in some situations to temporarily  have an empty document or a document with multiple root elements. But you should not rely on it and try to have a single root element most of the time.
My coworker views it as a “feature” that allows you to e.g. easily merge two documents into one (they can have different root elements names). And then use XPaths on this merged document in (almost) the same way as you would with the two original documents.
What would you say? Why is there this single root element rule anyway?
The reason I am asking is that our software was written in a way that exploits this “loophole” (uses this “feature”), but now when extending some functionality I get those XML errors. But it is also hard to fix without breaking backward compatibility…
xml mailing list, project page
xml gnome org

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]