An XML document consists of the following parts
An optional xml declaration, this typically looks like this.
Example XML Declaration |
Copy Code
|
---|---|
<?xml version="1.0" encoding="utf-8" ?> |
If an XML Declaration is provided, it 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).
Processing instructions can be added AFTER the XML Declaration, and before the Root XML Element.
These are optional, and typically provide information to external tools, i.e. the XSL transform that should be applied or the schema to be used for validation.
Example Processing Instruction |
Copy Code
|
---|---|
<?xml-stylesheet type="text/xsl" href="main.xsl"?> |
A single Document Type Declaration (DTD) can be added AFTER the XML Declaration, and before the Root XML Element.
The Document Type Declaration is optional, and describes the data contained in the root element
Comments can be added AFTER the XML Declaration, and before the Root XML Element.
Comments are optional.
The XML Document MUST contain a single XML Element, which contains the documents data.
The syntax for an XML Document is described by the W3C using EBNF as follows.
[1] document ::= prolog element Misc* - Char* RestrictedChar Char* [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.1' [27] Misc ::= Comment | PI | S [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? ('[' intSubset ']' S?)? '>'
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.