The intent of XML namespaces is to eliminate naming conflicts in XML documents that contain element types and attributes from multiple XML languages. Namespaces can prove useful in situations in which documents combine components from several fields that have the same name but are used in a different context. For example, the bird classification document shown previously in Example 3-6 uses the element <name> to identify a particular species of heron: <name language="latin">Ardea herodias</name> <name language="english">Great Blue Heron</name> The <name> element we saw in Example 3-8 identifies the contributing authors of an article: <name>George W. Archibald</name> <name>James C. Lewis</name> Each of the name elements has a different content model. In this case, the document that contains names of birds can use "name" qualified by one Uniform Resource Identifier (URI; see Chapter 7) [RFC 2396]. Figure 3-6 shows the parts of a typical HTTP URI as used in a namespace. A document that contains names of authors can use "name" qualified by another URI. The URI does not have to point to an actual file or other resource. Figure 3-6. Parts of a namespace as shown in the form of a URI
XML namespaces are collections of names identified by a URI. You use an XML namespace in XML documents to qualify element and attribute names. A unique two-part name identifies the element and attribute names in the namespace. The XML Namespaces Recommendation [Names] defines a mapping based on the idea of a prefix. When an element type name or attribute name contains a colon, then the mapping treats the part of the name before the colon as a namespace prefix. The part following the colon gives the local name.
The prefix maps to a URI reference and selects a namespace. The combination of the universally managed URI namespace and the document's own namespace produces universally unique identifiers. The namespace prefix serves as a proxy for a URI reference.
3.5.1 Namespace DeclarationsTo declare a namespace, you use an attribute, either "xmlns" as the attribute name or "xmlns:" as a prefix to the attribute name. The attribute value is a URI and identifies the namespace (see Example 3-10). Chapter 5 also provides more information about namespace declarations. Example 3-10 Associating a namespace prefix with the namespace name<?xml version="1.0"?> <name xmlns:family='http://myhomepage.com/classification'> <!-- the "family" prefix is bound to http://myhomepage.com/classification for the "name" element and contents -->
The prefix provides the namespace prefix part of the qualified name and must have been associated with a namespace URI reference by a namespace declaration. The portion following the prefix provides the local part of the qualified name. The prefix functions as a placeholder for the namespace name.
3.5.2 Using Qualified NamesElement names are given as qualified names. Attributes may be namespace declarations, or their names may be given as qualified names. The following two examples come from the Namespaces in XML Recommendation [Names]. Here the namespace declaration applies to the element where it is specified and to all elements within the content of that element. This relationship holds true unless it is overridden by another namespace declaration with the same prefix: <?xml version="1.0"?> <!-- all elements here are explicitly in the HTML namespace --> <html:html xmlns:html='http://www.w3.org/TR/REC-html40'> <html:head> <html:title>Frobnostication</html:title> </html:head> <html:body><html:p>Moved to <html:a href='http://frob.com'>here.</html:a></html:p> </html:body> </html:html> You can declare multiple namespace prefixes as attributes of a single element: <?xml version="1.0"?> <!-- both the bk and isbn namespace prefixes are available throughout --> <bk:book xmlns:bk='urn:loc.gov:books' xmlns:isbn='urn:ISBN:0-395-36341-6'> <bk:title>Cheaper by the Dozen</bk:title> <isbn:number>1568491379</isbn:number> </bk:book> 3.5.3 Namespace GuidelinesKeep in mind the following guidelines when using namespaces:
|