Processing Order

It is important to understand exactly how the DTD will be processed, especially if it includes internal and external entities. We are most interested in the processing order of the different types of entities because the processing order will affect the final result of the DTD and the XML document if the DTD and the XML document include entities and the entities are substituted. Before we examine processing order, let's look at the rules for processing a document:

  • If a document contains more than one entity declaration using the same entity name, the first entity declaration that is encountered will be used. All subsequent declarations with the same name will be ignored.

  • If a document contains more than one attribute declaration using the same attribute name, the first attribute declaration that is encountered will be used. All subsequent declarations with the same name will be ignored.

  • If more than one element declaration has the same element name, a fatal error will be raised by the processor.

Now that you know the rules for processing an XML document, let's look at the processing order that the processor follows:

  1. The internal subset of the DTD is read before everything else. This guarantees that any attribute or entity definitions listed in the internal subset will override any definitions in an externally referenced DTD. Developers can still use external DTDs, but they can override the declarations in the external DTDs.

  2. If the internal DTD contains external parameter entities, these entities will be replaced when the processor reaches them in the DTD. The internal DTD will be expanded to include the replacement text, and the replacement text will be processed. Once this is done, the rest of the internal DTD is processed. If the internal DTD contains additional external parameter entities, they will be replaced in the same manner when the processor reaches them. All general entities will be ignored at this step.

  3. Once the entire internal DTD is processed, any external DTDs referenced in the DOCTYPE declaration using the PUBLIC or SYSTEM keyword will be processed.

  4. Once the internal and external DTDs are processed and validated, the processor will replace general entities in the document body when they are referenced in the document.

Thus, you can create general external DTD documents containing declarations that apply to a large set of applications. You can override entities and attributes in these external DTDs in an internal DTD because the internal DTD will be processed first. Notice that the DTD is validated before the general entities are replaced. This order explains why general entities can never be used in any part of your declarations that are being validated, such as the enumerated values for an attribute.