Can XML comments go anywhere?

According to the XML specification, a well-formed XML document is:

document ::= prolog element Misc*

where prolog is

prolog ::= XMLDecl? Misc* (doctypedecl Misc*)?

and Misc is

Misc ::= Comment | PI | S

and

XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'

which means that, if you want to have comments at the top, you cannot have an XML type declaration.

You can, however, have comments after the declaration and outside the document element, either at the top or the bottom of the document, because Misc* can contain comments.

The specification agrees with Wikipedia on comments:

2.5 Comments

[Definition: Comments may appear anywhere in a document outside other markup; in addition, they may appear within the document type declaration at places allowed by the grammar. They are not part of the document's character data; an XML processor MAY, but need not, make it possible for an application to retrieve the text of comments. For compatibility, the string "--" (double-hyphen) MUST NOT occur within comments.] Parameter entity references MUST NOT be recognized within comments.

All of this together means that you can put comments anywhere that's not inside other markup, except that you cannot have an XML declaration if you lead with a comment.

However, while in theory theory agrees with practice, in practice it doesn't, so I'd be curious to see how your experiment works out.


The first example is not valid XML, the declaration has to be the first thing in a XML document.

But besides that, comments can go anywhere else.

Correcting your first example:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Queries used: ... -->
<dataset>
</dataset>

Tags:

Xml

Comments