XML declaration allowed only at the start of the document

Something comes before <?xml …?>, or there are two declarations.

The message, as the validator reports it
XML declaration allowed only at the start of the document

Why it happens

  • An empty line or spaces before <?xml, often printed by a PHP file or a template before the output starts.
  • Two documents joined into one file, each with its own declaration.

How to fix it

Make <?xml version="1.0"?> the very first characters of the file, with nothing before it, not even a newline. Remove a second declaration.

Example

Reports the error

<?xml version="1.0"?>
<note/>
Fixed
<?xml version="1.0"?>
<note/>