Liquid Technologies - XML Glossary
XML / Structure / XML Declaration
In This Topic
    XML Declaration
    In This Topic

    The XML Declaration provides basic information about the format for the rest of the XML document.

    It takes the form of a Processing Instruction and can have the attributes version, encoding and standalone.

    <?xml version="1.0" encoding="utf-8"?>

    version 1.0 The XML Declaration provides the XML Parser with version of the XML specification to use (currently only 1.0 is supported)
    encoding UTF-8, UTF-16, ISO-8859-1 etc

    The character encoding that should be used to decode the rest of the file.

    standalone yes, no The standalone declaration is a way of telling the parser to ignore any markup declarations in the DTD. The DTD is thereafter used for validation only.

    The XML Declaration MUST be the first thing in the XML file, and first means first, no comments, no whitespace, nothing is allowed before it. The only exception to this is a Byte Order Marker (BOM).

    Many XML Parsers raise an error if there is whitespace before the XML Declaration.

    This is a fairly common problem that if you've not see it before can leave you scratching your head or throwing things at the screen.

    The EBNF above is taken from the XML 1.1 Spec. If you were to look at the XML 1.0 spec you would notice a small but significant difference.

    [22]    prolog    ::=    XMLDecl? Misc* (doctypedecl Misc*)?

    Notice that the Xml Declaration is optional. Regardless of the version of the XML spec you are using, its always good practice to include an XML Declaration within your XML Documents.

    EBNF Syntax

    The syntax for an XML Declaration described by the W3C as using EBNF as follows.

    [22]    prolog        ::=    XMLDecl Misc* (doctypedecl Misc*)?
    [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
    [24] VersionInfo ::= S 'version' Eq ("'" VersionNum "'" | '"' VersionNum '"')
    [25] Eq ::= S? '=' S?
    [26] VersionNum ::= '1.' [0-9]+
    [27] Misc ::= Comment | PI | S
    [32] SDDecl ::= S 'standalone' Eq (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no') '"'))
    See Also