| Previous | Next
ConstraintsIn addition to defining the basic structures used in documents and DTDs, XML 1.0 defines a list of rules regarding their usage. These constraints put limits on various aspects of XML usage, and documents cannot in fact be considered to be "XML" unless they meet all of the well-formedness constraints. Parsers are required to report violations of these constraints, though only well-formedness constraint violations require that processing of the document halt completely. Namespace constraints are defined in Namespaces in XML, not XML 1.0. Well-Formedness ConstraintsWell-formedness refers to an XML document's physical organization. Certain lexical rules must be obeyed before an XML parser can consider a document well-formed. These rules should not be confused with validity constraints, which determine whether a particular document is valid when parsed using the document structure rules contained in its DTD. The Backus-Naur Form (BNF) grammar rules must also be satisfied. The following sections contain all well-formedness constraints recognized by XML Version 1.0 parsers, including actual text from the 1.0 specification.
Text from specificationIn the internal DTD subset, parameter entity references can occur only where markup declarations can occur, not within markup declarations. (This does not apply to references that occur in external parameter entities or to the external subset.) ExplanationIt is only legal to use parameter entity references to build markup declarations within the external DTD subset. In other words, within the internal subset, parameter entities may only be used to include complete markup declarations.
Text from specificationThe external subset, if any, must match production for ExplanationThe
Text from specificationThe replacement text of a parameter entity reference in a ExplanationThe replacement text of parameter entities may contain declarations that might not be allowed if the replacement text appeared directly. Parameter entity references in the internal subset cannot appear within declarations, but this rule does not apply to declarations that have been included via parameter entities.
Text from specificationThe ExplanationProper element nesting is strictly enforced, and every open tag must be matched by a corresponding close tag. Of course empty elements do not require and may not have a close tag.
Text from specificationNo attribute name may appear more than once in the same start-tag or empty- element tag. ExplanationAttribute names must be unique within a given element.
Text from specificationAttribute values cannot contain direct or indirect entity references to external entities. ExplanationXML parsers report an error when asked to replace references to external parsed entities within attribute values.
Text from specificationThe replacement text of any entity referred to directly or indirectly in an attribute value (other than " ExplanationThis restriction is meant to simplify the task of parsing XML data. Since attribute values can't even appear to contain element data, simple parsers need not track literal strings. Just by recognizing
Text from specificationCharacters referred to using character references must match the production for ExplanationAny characters that the XML parser generates must be real characters. A few character values in Unicode are not valid standalone characters.
Text from specificationIn a document without any DTD, a document with only an internal DTD subset which contains no parameter entity references, or a document with ExplanationThis long constraint lists the only situations in which an entity reference may appear without a corresponding entity declaration. Since a nonvalidating parser is not obliged to read and parse the external subset, the parser must give the document the benefit of the doubt, if an entity could possibly have been declared.
Text from specificationAn entity reference must not contain the name of an unparsed entity. Unparsed entities may be referred to only in attribute values declared to be of type ExplanationSince unparsed entities can't be parsed, don't try to force the parser to parse them.
Text from specificationA parsed entity must not contain a recursive reference to itself, either directly or indirectly. ExplanationBe careful how you structure your entities; make sure you don't inadvertently create a circular reference: <!ENTITY a "&b;"> <!ENTITY b "&c;"> <!ENTITY c "&a;"> <!--wrong!-->
Text from specificationParameter entity references may only appear in the DTD. ExplanationThis constraint is self evident because the <ok>%noproblem;</ok> The text Validity ConstraintsThe following sections contain all validity constraints that are enforced by a validating parser. Each includes actual text from the XML 1.0 specification and a short explanation of what the constraint actually means.
Text from specificationThe ExplanationThe name provided in the
Text from specificationParameter 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 is contained in the replacement text for a parameter entity reference, both must be contained in the same replacement text. ExplanationThis constraint means you can't create a parameter entity that completes one DTD declaration and begins another; the following XML fragment would violate this constraint: <!ENTITY % finish_it ">"> <!ENTITY % bad "won't work" %finish_it; <!--wrong!-->
Text from specificationThe standalone document declaration must have the value " ExplanationThis laundry list of potential standalone flag violations can be read to mean, "If you have an external subset in your DTD, ensure that your document doesn't depend on anything in it if you say
Text from specificationAn element is valid if there is a declaration matching ExplanationIf a document includes a DTD with element declarations, make sure the actual elements in the document match the rules set down in the DTD.
Text from specificationThe attribute must have been declared; the value must be of the type declared for it. ExplanationAll attributes used on elements in valid XML documents must have been declared in the DTD, including the
Text from specificationNo element type may be declared more than once. ExplanationUnlike entity and attribute declarations, only one declaration may exist for a particular element type.
Text from specificationParameter entity replacement text must be properly nested with parenthesized groups. That is to say, if either of the opening or closing parentheses in a For interoperability, if a parameter entity reference appears in a ExplanationThis constraint restricts the way parameter entities can be used to construct element declarations. It is similar to the Proper Declaration/PE Nesting constraint in that parameter entities may not be used to complete or open new parenthesized expressions. It prevents the XML author from hiding significant syntax elements inside parameter entities.
Text from specificationThe same name must not appear more than once in a single mixed-content declaration. ExplanationDon't list the same element type name more than once in the same mixed-content declaration.
Text from specificationValues of type ExplanationNo two attribute values for attributes declared as type
Text from specificationNo element type may have more than one ExplanationEach element can have at most one
Text from specificationAn ExplanationTo avoid potential duplication, you can't declare an
Text from specificationValues of type Explanation
Text from specificationValues of type ExplanationAttributes declared to contain entity references must contain references to unparsed entities declared in the DTD.
Text from specificationValues of type ExplanationIf an attribute is declared to contain a name or list of names, the values must be legal XML name tokens.
Text from specificationValues of this type must match one of the notation names included in the declaration; all notation names in the declaration must be declared. ExplanationAttributes that must contain notation names must contain names that reference notations declared in the DTD.
Text from specificationNo element type may have more than one ExplanationA given element can have only one attribute declared with the
Text from specification For compatibility, an attribute of type ExplanationEmpty elements cannot have
Text from specificationValues of this type must match one of the ExplanationAssigning a value to an enumerated type attribute that isn't listed in the enumeration is illegal in the DTD.
Text from specificationIf the default declaration is the keyword ExplanationRequired attributes must appear in the document and have a value assigned to them if they are declared as
Text from specificationThe declared default value must meet the lexical constraints of the declared attribute type. ExplanationIf you provide a default attribute value, it must obey the same rules that apply to a normal attribute value within the document.
Text from specificationIf an attribute has a default value declared with the ExplanationIf you choose to provide an explicit value for a
Text from specificationIf any of the " ExplanationIf you use a parameter entity to contain the beginning of a conditional section, the parameter entity must also contain the end of the section.
Text from specificationIn a document with an external subset or external parameter entities with " ExplanationParameter and general entity declarations must precede any references to these entities. All entity references must refer to previously declared entities. The specification also states that declaring the five predefined general entities (
Text from specificationThe Name must match the declared name of a notation. ExplanationExternal unparsed entities must use a notation that is declared in the document.
Text from specificationOnly one notation declaration can declare a given ExplanationDeclaring two notations with the same name is illegal. Namespace ConstraintsThe following list contains all constraints defined by the namespaces specification. Each includes actual text from the Namespaces in XML specification and a short explanation of what the constraint actually means.
Text from specificationPrefixes beginning with the three-letter sequence x, m, l, in any case combination, are reserved for use by XML and XML-related specifications. ExplanationJust like most other names in XML, namespace prefixes names can't begin with
Text from specificationThe namespace prefix, unless it is ExplanationYou have to declare all namespaces before you can use them. The prefixes have no meaning without the declarations, so using a prefix without a declaration context is an error. The namespace with the prefix |