XML Document

XML > Structure > XML Document

An XML document consists of the following parts

XML Declaration

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

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="/Content/Glossary/main.xsl"?> 

Document Type Declaration (DTD)

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

Comments can be added AFTER the XML Declaration, and before the Root XML Element.

Comments are optional.

Root XML Element

The XML Document MUST contain a single XML Element, which contains the documents data.

EBNF Syntax

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.

See Also

Try Liquid XML Free and see how we can help you today Free Trial