The XML Declaration

Optional XML declarations provide version and encoding information, as well as information about external markup declarations.

Productions

[23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
[24] VersionInfo ::= S 'version' Eq ("'" VersionNum "'" | '"' VersionNum '"')
[25] Eq ::= S? '=' S?
[26] VersionNum ::= '1.0' 1.0
[26] VersionNum ::= '1.1' 1.1
[32] SDDecl ::= S 'standalone' Eq (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no')
'"'))
[80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'" )
[81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')* /* Encoding name contains only Latin
characters */

Examples

<?xml version="1.0" ?>

<?xml version="1.1" encoding="ISO-8859-1"?>

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>

Description

The XML declaration is recommended by the XML spec but is not mandatory. The XML declaration is a human- and machine-readable flag that states several facts about the content of the document. If present, it must appear on the first line of the document. An XML declaration is not a processing instruction, although it looks like one. In general, it provides three pieces of information about a document: (1) the XML version information; (2) the character encoding used in the document; and (3) the standalone document declaration, which states whether or not the document stands alone—that is, whether or not it relies on markup declarations from an external source (a DTD).

Version information

If you use an XML declaration, ...

Get XML Pocket Reference, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.