Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OK to put comments before the XML declaration?

Is it OK to put comments before the XML declaration in an XML file?

<!-- Is this bad to do? --> <?xml version="1.0" encoding="utf-8"?> <someElement /> 
like image 592
sourcenouveau Avatar asked Jul 28 '09 20:07

sourcenouveau


People also ask

Can an XML file start with a comment?

Syntax. A comment starts with <! -- and ends with -->. You can add textual notes as comments between the characters.

What are the rules for adding XML comments?

Rules for adding XML commentsDon't use a comment before an XML declaration. You can use a comment anywhere in XML document except within attribute value. Don't nest a comment inside the other comment.

Does XML allow comments?

Allows notes and other human readable comments to be included within an XML file. XML Parsers should ignore XML comments. Some constrains govern where comments may be placed with an XML file.

Which of the following are correct rule for XML declaration?

The correct order is: version, encoding and standalone. Either single or double quotes may be used.


1 Answers

No, it's not OK.

Appendix F of the XML spec says:

Because each XML entity not accompanied by external encoding information and not in UTF-8 or UTF-16 encoding must begin with an XML encoding declaration, in which the first characters must be '< ?xml', any conforming processor can detect, after two to four octets of input, which of the following cases apply.

Ah, but, section F is non-normative, you say.

Well, section 2.1 gives the production for a well-formed XML document, thus:

[1]     document       ::=       prolog element Misc* 

...and in section 2.8 we get the production for "prolog":

[22]    prolog     ::=       XMLDecl? Misc* (doctypedecl Misc*)? [23]    XMLDecl    ::=      '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>' 

So, you can omit the < ?xml declaration, but you can't prefix it with anything.

(Incidentally, "Misc" is the category that comments fall into).

like image 139
Gary McGill Avatar answered Oct 14 '22 22:10

Gary McGill