XML Namespaces
Earlier in the chapter, we introduced XML namespaces. This W3C XML standard introduces the following terms with regard to XML namespaces:Local name Represents the name of the element or attribute without the prefix. In the previous example, book, title, author, ISBN, and so forth are considered local names. These are used whenever there is no concern over duplicate tag or attribute names. Local name is also used to refer to the name part of a qualified name.
Qualified name Represents the fully prefixed name. For example, as a continuation of the previous examples, bk:title, bk:book, and so forth are considered qualified names. Qualified names are being used more often because XML Schemas are defining standard types, such as address, customer, purchase order, and so on, and there is a need to differentiate semantics.
Namespace prefix Represents the namespace prefix declared using the special prefix, xmlns. The previous example defined one namespace prefix: bk. Prefixes are scoped and thus must be unique within the children of the parent element that declared the namespace, but prefixes may be overridden by a new declaration on a descendent element or attribute.
Expanded name Represents the result of applying the namespace defined by the namespace prefix to the qualified name. For example, bk:booklist could be expanded to http://www.mypublishsite.com/books:booklist. The expanded name is never seen in the XML document itself, but is conceptually important.
Two kinds of namespace attributes exist: prefixed and default. A prefixed namespace attribute is of the form nsprefix:attr, where nsprefix is the namespace prefix defined previously. Once a prefix has been declared, it can be used to specify a namespace for any elements or attributes in the scope of the element where it was declared. You would, therefore, need to declare global prefixes—that is, prefixes you want to use everywhere in your document—as attributes of the root element.The default namespace attribute is xmlns. xmlns has the effect of specifying a default namespace for the entire scope of an element (including the element itself). This default does not apply to the attributes in the subtree, however. For example, consider the following example:
<booklist xmlns="http://www.osborne.com/books>
<book isbn="1234-5678-1234">
<title>Oracle XML Handbook</title>
<author>Oracle XML Team</author>
</book>
<book isbn="24345-564478-1344234">
<title>The C programming language</title>
<author>Kernighan and Ritchie</title>
</book>
</booklist>
This root element declaration has the effect of specifying that all the elements under booklist (book, title, author) are in the http://www.osborne.com/books namespace. The attribute isbn, however, is not. Default namespaces can be specified at any level of the document and have the effect of overriding previous declarations. Setting xmlns=““ has the effect of removing the default namespace declaration for a particular document subtree.Namespaces complicate the determination of attribute uniqueness. For example, consider the following example:
<booklist xmlns:dollars="USA" xmlns:pounds="Britain">
<book dollars:price="7.99" pounds:price="3.99">
<title>The Code of the Woosters</title>
<author>P.G. Wodehouse </author>
</book>
</booklist>
The two price attributes should be considered different, even though they have the same local name, because their expanded names are different. The following document would not be considered well-formed, however:
<booklist xmlns:dollars="USA" xmlns:currency="USA">
<book dollars:price="7.99" currency:price="3.99">
<title>The Code of the Woosters</title>
<author>P.G. Wodehouse </author>
</book>
</booklist>
Here, even though dollars:price and currency:price have different qualified names, they have the same expanded name, which means they are, in fact, the same attribute declared twice on the book element. For a similar reason, only one default namespace is allowed per document.