Andrej
Andrej

Reputation: 1736

Must a new line follow the XML declaration?

Is this one a valid XML instance? It has nothing between the the XML declaration and the root node.

<?xml version="1.0" encoding="UTF-8"?><data></data>

I could not find the right place in the XML specification myself and hope that somebody will help me...

Upvotes: 16

Views: 4172

Answers (2)

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143099

The spec (see 2.8 Prolog and Document Type Declaration) does not require (but does allow) a newline to follow the XML Declaration.

Formally, this is written as:

[16]         PI         ::=       '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
...  
[22]         prolog     ::=        XMLDecl? Misc* (doctypedecl Misc*)?
[23]         XMLDecl    ::=       '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
...  
[27]         Misc       ::=        Comment | PI | S

As you can see in [22] prolog, the XML Declaration is optional (see the question mark after the symbol) and zero, one or more (see the star) Misc can follow which are: Comments, other Processing Instructions (PI) or Whitespace (S). Whitespace includes the newline.

Because Misc is optional here, a newline can, but need not, follow after the declaration.

Upvotes: 16

Thargor
Thargor

Reputation: 1872

New lines or so are not part of the spec. When you, for example, write a XML Document with the XMLOutputter Class (in Java), you get a file without newlines. Newlines are for humans.

Upvotes: 2

Related Questions