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 />
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 />
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).
The XML declaration specifies the document encoding, which is as important for comments as for structural XML. Therefore, the declaration should go first. I wouldn't be surprised if many XML readers were able to deal with this, but it's a bad idea.
No, this does not comply with XML standards, but comments are good.