XML technologies / XML / XML in a Nutshell / Namespaces / Namespaces and DTDs
(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)
Namespaces are completely independent of DTDs and can be used in valid or invalid documents. A document may have a DTD without namespaces, namespaces without a DTD, both, or neither. Namespaces change neither DTD syntax nor the definition of validity. For example, a valid document using an element named dc:title must have an ELEMENT declaration correctly specifying the content of dc:title:
<!ELEMENT dc:title (#PCDATA)> The element name in the document, including its prefix, must exactly match its name in the DTD. The DTD cannot omit the prefix and simply declare an element title. The same applies to prefixed attributes. If an element uses the attributes xlink:type and xlink:href, the DTD must also declare xlink:type and xlink:href and cannot simply use type and href.
Conversely, if an element uses an xmlns attribute to set the default namespace without assigning element prefixes, element names in the DTD must be unprefixed. The validator neither knows nor cares about namespaces. It merely sees colons in some element and attribute names, which are valid as long as they are declared.
Parameter entity references for namespace prefixes
Having to declare prefixed names in DTDs rather than qualified names or combinations of local names and namespace URIs makes changing prefixes harder. Every declaration using a prefix must change too. With some planning, parameter entity references can simplify this.
The trick is to define both the namespace prefix and its separating colon as parameter entities, for example:
<!ENTITY % dc-prefix "dc">
<!ENTITY % dc-colon ":">Next, define qualified names as further parameter entity references:
<!ENTITY % dc-title "%dc-prefix;%dc-colon;title">
<!ENTITY % dc-creator "%dc-prefix;%dc-colon;creator">
<!ENTITY % dc-description "%dc-prefix;%dc-colon;description">
<!ENTITY % dc-date "%dc-prefix;%dc-colon;date">Warning! Do not skip this step or try to use the parameter entities dc-prefix and dc-colon directly in ELEMENT and ATTLIST declarations. This fails because XML parser insert extra spaces around entity replacement text when used outside another entity’s replacement text.
Then use the qualified-name entity references in every declaration:
<!ELEMENT %dc-title; (#PCDATA)>
<!ELEMENT %dc-creator; (#PCDATA)>
<!ELEMENT %dc-description; (#PCDATA)>
<!ELEMENT %dc-date; (#PCDATA)>
<!ELEMENT rdf:Description ((%dc-title; | %dc-creator; | %dc-description; | %dc-date;)*)>A document needing a different prefix now changes only the parameter entity definitions, either by editing the DTD directly or overriding them in its internal subset. For example, to change the prefix from dc to dublin, insert this entity definition before the normal definition in the DTD :
<!ENTITY % dc-prefix "dublin"> To use the default namespace instead of explicit prefixes, define the entities dc-prefix and dc-colon as empty strings:
<!ENTITY % dc-prefix "">
<!ENTITY % dc-colon "">
Copyright © 2005 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "XML in a Nutshell" is subject to the same conditions as the printed edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de