- Escaping XML Data
- XML Encoding
- XML 1. 0 EBNF
- XML 1.1 EBNF
- Byte Order Marker
- XML Schemas
- XML Mixed Content
- Describing Complex Structures in XML
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.
 prolog ::= XMLDecl? Misc* (doctypedecl Misc*)?
The syntax for an XML Declaration described by the W3C as using EBNF as follows.
 prolog ::= XMLDecl Misc* (doctypedecl Misc*)?
 XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
 VersionInfo ::= S 'version' Eq ("'" VersionNum "'" | '"' VersionNum '"')
 Eq ::= S? '=' S?
 VersionNum ::= '1.' [0-9]+
 Misc ::= Comment | PI | S
 SDDecl ::= S 'standalone' Eq (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no') '"'))