Stylus Studio XML Editor

Table of contents

Appendices

2.8 Prolog and Document Type Declaration

Prolog and Document Type Declaration

XML 1.1 documents MUST begin with an XML declaration which specifies the version of XML being used. For example, the following is a complete XML 1.1 document, Well-Formed but not Validity:

<?xml version="1.1"?>
<greeting>Hello, world!</greeting> 

but the following is an XML 1.0 document because it does not have an XML declaration:

<greeting>Hello, world!</greeting>

The function of the markup in an XML document is to describe its storage and logical structure and to associate attribute name-value pairs with its logical structures. XML provides a mechanism, the Document Type Declaration, to define constraints on the logical structure and to support the use of predefined storage units. An XML document is valid if it has an associated document type declaration and if the document complies with the constraints expressed in it.

The document type declaration MUST appear before the first Element in the document.

Prolog
2.8    prolog   ::=   XMLDecl Misc* (doctypedecl Misc*)?
2.8    XMLDecl   ::=   '<?xml' VersionInfo EncodingDecl? SDDecl? S?'?>'
2.8    VersionInfo   ::=   S 'version' Eq ("'" VersionNum "'" | '"' VersionNum '"')
2.8    Eq   ::=   S? '=' S?
2.8    VersionNum   ::=   '1.1'
2.8    Misc   ::=   Comment | PI | S

The XML document type declaration contains or points to markup declaration that provide a grammar for a class of documents. This grammar is known as a document type definition, or DTD. The document type declaration can point to an external subset (a special kind of External Entity) containing markup declarations, or can contain the markup declarations directly in an internal subset, or can do both. The DTD for a document consists of both subsets taken together.

A markup declaration is an Element Type declaration, an Attribute-List Declaration, an entity declaration, or a Notation Declaration. These declarations MAY be contained in whole or in part within Parameter entity, as described in the well-formedness and validity constraints below. For further information, see [Physical Structures].

Document Type Definition
2.8    doctypedecl   ::=   '<!DOCTYPE' S Name (S ExternalID)? S? ('[' intSubset ']' S?)? '>'[VC: Root Element Type]
2.8    DeclSep   ::=   PEReference | S
2.8    intSubset   ::=   (markupdecl | DeclSep)*
2.8    markupdecl   ::=   elementdecl | AttlistDecl | EntityDecl | NotationDecl | PI | Comment[VC: Proper Declaration/PE Nesting]

Note that it is possible to construct a well-formed document containing a doctypedecl that neither points to an external subset nor contains an internal subset.

The markup declarations MAY be made up in whole or in part of the Replacement Text of Parameter entity. The productions later in this specification for individual nonterminals (elementdecl, AttlistDecl, and so on) describe the declarations after all the parameter entities have been Include.

Parameter entity references are recognized anywhere in the DTD (internal and external subsets and external parameter entities), except in literals, processing instructions, comments, and the contents of ignored conditional sections (see [Conditional Sections]). They are also recognized in entity value literals. The use of parameter entities in the internal subset is restricted as described below.

Validity Constraint: Root Element Type

Root Element Type

The Name in the document type declaration MUST match the element type of the Root Element.

Validity Constraint: Proper Declaration/PE Nesting

Proper Declaration/PE Nesting

Parameter-entity Replacement Text MUST be properly nested with markup declarations. That is to say, if either the first character or the last character of a markup declaration (markupdecl above) is contained in the replacement text for a Parameter-entity reference, both MUST be contained in the same replacement text.

Well Formedness Constraint: PEs in Internal Subset

PEs in Internal Subset

In the internal DTD subset, Parameter-entity reference MUST NOT occur within markup declarations; they MAY occur where markup declarations can occur. (This does not apply to references that occur in external parameter entities or to the external subset.)

Well Formedness Constraint: External Subset

External Subset

The external subset, if any, MUST match the production for extSubset.

Well Formedness Constraint: PE Between Declarations

PE Between Declarations

The replacement text of a parameter entity reference in a DeclSep MUST match the production extSubsetDecl.

Like the internal subset, the external subset and any external parameter entities referenced in a DeclSep MUST consist of a series of complete markup declarations of the types allowed by the non-terminal symbol markupdecl, interspersed with white space or Parameter-entity reference. However, portions of the contents of the external subset or of these external parameter entities MAY conditionally be ignored by using the conditional section construct; this is not allowed in the internal subset but is allowed in external parameter entities referenced in the internal subset.

External Subset
2.8    extSubset   ::=   TextDecl? extSubsetDecl
2.8    extSubsetDecl   ::=   ( markupdecl | conditionalSect | DeclSep)*

The external subset and external parameter entities also differ from the internal subset in that in them, Parameter-entity reference are permitted within markup declarations, not only between markup declarations.

An example of an XML document with a document type declaration:

<?xml version="1.1"?>
<!DOCTYPE greeting SYSTEM "hello.dtd">
<greeting>Hello, world!</greeting> 

The System Identifier "hello.dtd" gives the address (a URI reference) of a DTD for the document.

The declarations can also be given locally, as in this example:

<?xml version="1.1" encoding="UTF-8" ?>
<!DOCTYPE greeting [
<!ELEMENT greeting (#PCDATA)>
]>
<greeting>Hello, world!</greeting>

If both the external and internal subsets are used, the internal subset MUST be considered to occur before the external subset. This has the effect that entity and attribute-list declarations in the internal subset take precedence over those in the external subset.

addXML 1.1 processors SHOULD accept XML 1.0 documents as well. If a document is well-formed or valid XML 1.0, and provided it does not contain any control characters in the range [#x7F-#x9F] other than as character escapes, it may be made well-formed or valid XML 1.1 respectively simply by changing the version number.