- Why Namespaces Are Needed: Resolving Name Conflicts
- Qualified Names, Prefixes, Local Names, and Other Terminology
- Declaring Namespaces in XML Documents
- Default Namespace
- Handling Namespaces in a DTD or XML Schema
- Validating Documents with Namespaces
- What Does a Namespace Point To?
- Namespace Support and Use
- Special Attributes: xmlns, xml:space, xml:lang, and xml:base
- Common Namespaces
- Summary
- For Further Exploration
Handling Namespaces in a DTD or XML Schema
Remember how I emphasized that it's the URI, not the prefix, that matters? Well, I told a little lie. When it comes to DTDs, the reverse is true: the prefix matters. Why? Because DTDs declare element names, content models, and attributes, all of which require fully qualified names that must reflect the same prefix used by all instance documents. That implies that if we decide to change the prefix in the XML documents for any reason, we must also change the prefix in the DTD.
Since XML DTD syntax predates XML namespaces, qualified names such as Disc:price and xlink:href are really nothing special; they are simply cases of legal XML Names that happen to contain colons. From the perspective of the DTD, there is no such thing as a namespace declaration or namespace use. In fact, a "prefix" isn't meaningful either; the letters and semicolon that follow are just part of the element or attribute name. This is not just a matter of semantics, as we'll see when we discuss validity with respect to namespaces in the next section.
That doesn't mean that prefixes can be used in DTDs without some definition. They need to be declared in an attribute-list declaration for the elements with which they are to be associated. The general syntax for this attribute-list declaration is:
<!ATTLIST prefix:ElementName xmlns:prefix CDATA #FIXED "URI">
The variable portions are in italics. For example, the attribute-list declaration for the earlier discount namespace attached to the root element DiscountCatalog is:
<!ATTLIST Disc:DiscountCatalog xmlns:Disc CDATA #FIXED "http://www.HouseOfDiscounts.com/namespaces/Discounts">
Notice that this is just an attribute declaration involving the xmlns attribute, rather than a namespace declaration. DTDs have no syntax for expressing a "namespace declaration" because they were developed long before XML namespaces. This implies that although Disc:DiscountCatalog looks exactly like the universal name in our XML document namespace example, in the DTD, it is merely an element name. It has nothing at all to do with namespaces! And even though I referred to the part before the colon as a "prefix," from the perspective of DTD syntax, Disc:DiscountCatalog just happens to be an element name that contains a colon. As far as the DTD is concerned, there is no separate notion of a prefix!
We'll defer a detailed discussion of how namespaces are handled in XML Schema until chapter 6. For now, we'll simply observe that since XML Schemas are written in XML syntax, designation of namespaces is similar to the way they are specified in an XML instance. Namespaces typically are declared on the root element, which in this case is xsd:schema. For example, one possibility is:
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.YOUR.com" xmlns="http://www.YOUR.com" elementFormDefault="qualified"> <xsd:element name="YourElement" /> </xsd:schema>
In contrast to DTDs, XML Schema redefines validity for universal names because, as we'll see, a local name in an element or attribute declaration can be associated with an XML namespace.