XML technologies / XML / XML in a Nutshell / XML reference / Constraints

(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)

I.

Well-formedness constraints

II.

Validity constraints

III.

Namespace constraints

  

XML 1.0 defines basic document and DTD structures plus rules governing their use. These concern different aspects of XML. Documents cannot properly be called XML unless they satisfy every well-formedness constraint. Parsers should report violations, but must stop all further processing only for well-formedness violations. Constraints for namespaces are defined in Namespaces in XML rather than XML 1.0.

  

 

Well-formedness constraints

Well-formedness concerns lexical document structure. If lexical rules are satisfied, an XML parser accepts the document as well-formed. This differs from validity: well-formedness can be assessed without a DTD, whereas validity requires one. BNF grammar rules must also be met. The sections below give all XML 1.0 well-formedness constraints checked by parsers and the corresponding specification text.

 

Parameter entities in the internal DTD subset

Specification excerpt

In the internal DTD subset, parameter entities are allowed where markup declarations may occur, but not within them. This does not apply to references inside external parameter entities or the external subset.

Description

Parameter entities may form markup declarations only in the external subset. Internally, they may be replaced only by complete declarations.

 

External DTD subset

Specification excerpt

An external subset must match production extSubset.

Description

Production extSubset specifies permitted declarations. Generally, the external subset may contain only complete declarations or parameter entity references. For details of production extSubset, see the EBNF grammar in Grammar of an XML 1.0 document.

 

Parameter entities between declarations

Specification excerpt

If production DeclSep is used and the matching text contains a parameter entity, its replacement text must match production extSubsetDecl.

Description

Replacement text may contain declarations that would be invalid if inserted literally. Parameter entities are forbidden within internal-subset declarations, except when those declarations themselves occur within parameter entities.

 

Element type match

Specification excerpt

The Name in an end-tag must match the start-tag’s name.

Description

Elements must nest correctly; every start-tag needs a corresponding end-tag.

 

Unique attribute specifications

Specification excerpt

No attribute name may appear twice in a start-tag, including empty-element tags.

Description

Attribute names must be unique within an element.

 

No external entity references

Specification excerpt

Attribute values must not directly or indirectly reference external entities.

Description

Parsers report an error for external replacement-entity references within attribute values.

 

No < in attribute values

Specification excerpt

Within an attribute value, entity replacement text must not contain <, directly or indirectly. The only exception is &lt;.

Description

This simplifies XML parsing. Attribute values cannot look like element data, so simple parsers need not search literal strings. Looking for < and > suffices to check markup structure and nesting.

 

Legal characters

Specification excerpt

Characters referenced by character references must match production Char.

Description

Every character produced by an XML parser must be legal. A few Unicode characters are not valid XML characters.

 

Entity declared

Specification excerpt

In documents without a DTD, or without an external subset and without parameter entity references, or documents with standalone='yes', an entity reference outside the external subset or any parameter entity reference must name a declared Name in a declaration outside external subsets and parameter entities. These names need no declaration in a well-formed document: amp, lt, gt, apos, quot. Parameter entity declarations must precede references. General entity declarations must precede references in attribute-list defaults. Non-validating processors need not read or process declarations in external subsets or external parameter entities. Such documents are exempt from the well-formedness requirement that an entity be declared, except when standalone='yes'.

Description

This long constraint identifies the only cases allowing undeclared references. Because non-validating parsers need not read external subsets, they should give documents the benefit of the doubt and assume the entity was declared.

 

Parsed entity

Specification excerpt

An entity reference must name a parsed entity. Unparsed entities may occur only in attribute values declared as ENTITY or ENTITIES.

Description

A parser can substitute only entities it has read; do not try to trick it.

 

No recursion

Specification excerpt

A parsed entity must not reference itself directly or indirectly.

Description

Structure entities carefully to avoid unintended circular references:

<!ENTITY a "&b;">
<!ENTITY b "&c;">
<!ENTITY c "&a;"> <!--falsch, c referenziert sich indirekt selbst!-->

 

In the DTD

Specification excerpt

Parameter entity references may occur only within the DTD.

Description

This is natural because character % is not special outside the DTD. This element is therefore fine:

 <ok>%noproblem;</ok> 

The parser passes text %noproblem; unchanged without error.

 

 

Validity constraints

The following sections give every validity constraint a validating parser must enforce, with the XML 1.0 specification text and a short explanation.

 

Root element type

Specification excerpt

The name specified in the DTD must match the root element’s name.

Description

The declaration !DOCTYPE contains the root name, which must match the document’s root element.

 

Proper declaration/parameter-entity nesting

Specification excerpt

Parameter entity replacement text must nest properly within markup declarations: if it includes the first or last declaration character, it must include both.

Description

A parameter entity cannot finish one DTD declaration and begin another. This fragment would violate the constraint:

<!ENTITY % hoert_hier_auf ">">
<!ENTITY % bad "funktioniert nicht" %hoert_hier_auf; <!--falsch!-->

 

Standalone document declaration

Specification excerpt

The standalone declaration must have value no if an external markup declaration supplies a default for an omitted attribute; if entities other than amp, lt, gt, apos and quot are used; if external declarations cause an attribute value to change upon normalization; or if an element declared with element content contains ignorable whitespace (ignorable whitespace).

Description

In simpler terms: if your DTD has an external subset, ensure the document depends on none of its declarations before writing standalone='yes' in the XML declaration. Even simpler: if an external subset exists, set standalone to no.

 

Element valid

Specification excerpt

An element is valid if a declaration matching production elementdecl exists for its type name and one of these conditions holds:

  • The declaration matches EMPTY and the element has no content.

  • The declaration matches children and the child sequence matches the language of its regular expression. Whitespace is allowed between start-tag and first child, between children, and between last child and end-tag. Whitespace characters match production S. A CDATA section containing only whitespace does not match production S and is therefore forbidden there.

  • The declaration matches Mixed and content consists of characters and children whose types appear in the declaration.

  • The declaration matches ANY and every child type is declared.

Description

When using a DTD with element declarations, ensure every document element follows its rules.

 

Attribute type and value

Specification excerpt

Attributes must be declared and their values match the declared type.

Description

Every attribute on a valid XML document’s elements must be declared in the DTD, including xml:space and xml:lang. Each instance must carry a value matching the declared type. For attribute types, see Attribute-list declaration in Basics of XML syntax.

 

Unique element type declaration

Specification excerpt

No element type may be declared more than once.

Description

Unlike entities and attributes, element types may be declared only once.

 

Proper group/parameter-entity nesting

Specification excerpt

Replacement text must nest properly with parenthesized groups. If it contains either parenthesis of a group matching choice, seq or Mixed, it must contain both.

For simplicity, if a parameter entity reference occurs in a group matching choice, seq or Mixed, replacement text should contain a non-whitespace character, with neither its first nor last non-whitespace character a connector (| or ,).

Description

This restricts parameter entities in element declarations. Like declaration/parameter-entity nesting, entities must not close one parenthesized expression and open another. Authors must not hide significant syntax inside parameter entities.

 

No duplicate types

Specification excerpt

Each name may appear only once in a mixed-content declaration.

Description

An element type name may occur only once in mixed-content declarations.

 

ID

Specification excerpt

Values of type ID must match production Name. No name may occur twice as this type’s value in a document; IDs uniquely identify their containing elements.

Description

No two attribute values of type ID may be equal. This applies globally, not just to a particular element type.

 

One ID per element type

Specification excerpt

No element type may have more than one attribute of type ID.

Description

Every element may have at most one attribute of type ID.

 

ID attribute defaults

Specification excerpt

An attribute of type ID must have #IMPLIED or #REQUIRED as its default.

Description

To avoid duplication, an attribute of type ID cannot be declared #FIXED or assigned a default value.

 

IDREF

Specification excerpt

Attribute values of type IDREF must match production Name, and values of type IDREFS must match production Names. Every Name must match an attribute value of type ID on a document element. In other words, IDREF values must match an attribute of type ID.

Description

An ID reference must point to an actual document ID.

 

Entity name

Specification excerpt

Values of types ENTITY and ENTITIES must match productions Name and Names, respectively. Every Name must name an unparsed entity declared in the DTD.

Description

Entity-reference attributes must reference unparsed entities declared in the DTD.

 

Name token

Specification excerpt

Values of types NMTOKEN and NMTOKENS must match productions Nmtoken and Nmtokens, respectively.

Description

Attributes declared with one or more names must contain XML name tokens.

 

Notation attributes

Specification excerpt

Values must match a notation name in the declaration, and every listed notation must already be declared.

Description

Notation names in attributes must reference notations declared in the DTD.

 

One notation per element type

Specification excerpt

No element type may have more than one attribute NOTATION.

Description

An element may contain only one attribute declared with type NOTATION. This ensures backward compatibility with SGML.

 

No notation on empty elements

Specification excerpt

For compatibility, an attribute of type NOTATION must not occur on an element declared EMPTY.

Description

Empty elements must not have attributes of type NOTATION, for SGML compatibility.

 

Enumerations

Specification excerpt

Values must match one of the Nmtoken listed in the declaration.

Description

An enumerated attribute must hold a value listed in its declaration.

 

Required attributes

Specification excerpt

In a default declaration, keyword #REQUIRED identifies an attribute required in every element of the type.

Description

Required attributes must occur in documents. Attributes declared in the DTD as #REQUIRED must have a value in the XML document.

 

Legal default attribute values

Specification excerpt

A declared attribute default must satisfy the lexical constraints of its attribute type.

Description

Defaults follow the same rules as attribute values in documents.

 

Fixed attribute defaults

Specification excerpt

If an attribute is declared with keyword #FIXED, every instance must have the specified value.

Description

If you declare a value with #FIXED, no other value is allowed.

 

Proper conditional-section/parameter-entity nesting

Specification excerpt

If replacement text contains one conditional-section sequence <![, [ or ]]>, it must contain every sequence forming that section.

Description

Embedding a conditional section’s start in a parameter entity requires embedding the rest too.

 

Declared entities

Specification excerpt

If a non-standalone document (one with standalone='no') has an external subset or external parameter entities, the Name in an entity reference must name a declared entity. For interoperability, valid documents should declare amp, lt, gt, apos and quot if used. Parameter entity declarations must precede references; general entity declarations must precede direct or indirect references in attribute-list defaults.

Description

Both parameter and general entities must be declared before reference. Every reference must point to an already declared entity. The specification even recommends declaring the five predefined entities (amp, lt, gt, apos and quot), though doing so usually only complicates applications unnecessarily.

 

Declared notations

Specification excerpt

A name must match a previously declared notation.

Description

External unparsed entities must use a notation declared earlier in the document.

 

Unique notation

Specification excerpt

The Name in a notation declaration must be unique.

Description

Two notations may not share a name.

 

 

Namespace constraints

The following sections present every namespace constraint from the specification Namespaces in XML, with specification excerpts and short explanations.

 

Leading “XML”

Specification excerpt

Prefixes beginning with any case combination of x, m and l are reserved for XML or related specifications, including “xml”, “XML”, “Xml” and “xmL”.

Description

Namespace names, like most XML names, cannot begin with xml unless defined by the W3C.

 

Prefix declared

Specification excerpt

Every namespace prefix except xml and xmlns must be declared in a namespace declaration attribute on the using element’s start-tag or that of an ancestor containing it. The prefix is used within that scope. Prefix xml is by definition bound to namespace name "http://www.w3.org/XML/1998/namespace". Prefix xmlns is used only for namespace bindings and is not itself bound to a namespace name.

Description

Declare namespaces before use: undeclared prefixes have no meaning and are errors. The namespace with prefix xml is always defined and needs no redeclaration. Prefix xmlns used in declarations is not itself considered a namespace prefix and need not be declared.

  

Back to top

 

 

 

back to top