Skip to main content

XML error · prolog

Malformed declaration expecting version

The document opens with <?xml but no version attribute. Version is the one part of the declaration that is not optional, and it must come first.

Malformed declaration expecting version (line 1)

What it means

If a document has an XML declaration at all, that declaration must begin with version. Encoding and standalone are optional and must follow it; a declaration that leads with encoding is not a declaration the parser will accept.

The declaration itself is optional — a document with no <?xml line is perfectly legal and assumed to be version 1.0 in UTF-8. The failure is starting one and then leaving out its only mandatory part.

What usually causes it

  • Writing <?xml encoding="UTF-8"?> in the belief that the declaration is only about encoding.
  • Assembling the declaration from parts where version is added conditionally.
  • Copying a processing instruction and editing it into a declaration.
  • Stripping attributes from the first line with a regex that was too greedy.

How to fix it

  • Put version first: <?xml version="1.0" encoding="UTF-8"?>.
  • Or delete the declaration entirely — UTF-8 and 1.0 are the defaults, so a document without one is fine.
  • Never hand-build the first line; every serializer emits a correct declaration.

The same error elsewhere

Different parsers, same defect. If you arrived with one of these messages, you are in the right place.

  • Xerces (Java)

    The "version" pseudo attribute is required and must appear first in the XML declaration.
  • expat (Python)

    XML declaration not well-formed: line 1, column 6
  • .NET System.Xml

    Version number must start with '1.'

Before and after

Both snippets are re-checked by the test suite against the real parser: the first is confirmed to produce the exact error above, the second to parse cleanly.

Fails
<?xml encoding="UTF-8"?>
<root/>
Parses
<?xml version="1.0" encoding="UTF-8"?>
<root/>

Where this bites most

Get started

Bring order to the XML your team can't afford to ignore.

Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.