Introduction to XML




The xmlns defined at the root HTML element is the default namespace applied for all the elements that
don’t have an explicit namespace defined; however the books element defines an explicit namespace
using the prefix blist. Because that prefix is used while declaring the books elements, all of the elements
under books are considered to be using the prefixed namespace.

Role of Namespaces

A namespace is a set of XML elements and their constituent attributes. As you dive deep into XML, such as
creating interactive XML Web pages for the Web and establishing guidelines for transporting data and so
on, you will find that XML namespaces are incredibly important. Here are some of the uses of namespaces.

Reuse

Namespaces can allow any number of XML documents to reference them. This allows namespaces to be
reused as needed, rather than forcing developers to reinvent them for each document they create. For
instance, consider the common business scenario wherein you have two applications that exchange a
common XML format: the server that generates the XML, relying on a particular namespace, and the
client that consumes this XML, which also must rely on the same namespace. Rather than generating two
namespaces (one for each application), a single namespace can be referenced by both applications in the
XML they generate. This enables namespaces to be reused, which is an important feature. Not only can
namespaces be reused by different parts of one application, they can be reused by different parts of any
number of applications. Therefore, investing in developing a well thought out namespace can pay
dividends for some time.

Multiple Namespaces

Just as multiple XML documents can reference the same namespace, one document can reference more
than one namespace. This is a natural by-product of dividing elements into logical, ordered groups. Just
as software development often breaks large processes into smaller procedures, namespaces are usually
chunked into smaller, more logical groupings. Creating one large namespace with every element you
think you might need doesn’t make sense. This would be confusing to develop and it certainly would be
confusing to anyone who had to use such an XML element structure. Rather, granular, more natural
namespaces should be developed to contain elements that belong together.

For instance, you can create the namespaces as building blocks, assembled together to form the vocabularies
required by a large program. For example, an application might perform services that help users to buy
products from an e-commerce Web site. This application would require elements that define product
categories, products, buyers, and so on. Namespaces make it possible to include these vocabularies inside
one XML document, pulling from each namespace as needed.

Ambiguity

Namespaces can sometimes overlap and contain identical elements. This can cause problems when an
XML document relies on the namespaces in question. An example of such a collision might be a namespace
containing elements for book orders and another with elements for book inventories. Both might use
elements that refer to a book’s title or an author’s name. When one document attempts to reference elements
from both namespaces, this creates ambiguity for the XML parser. You can resolve this problem by wrapping
the elements of book orders and book inventories in separate namespaces. Because elements and attributes
that belong to a particular namespace are identified as such, they don’t conflict with other elements and
attributes sharing the same name. This solves the previously mentioned ambiguity. By prefacing a particular
element or attribute name with the namespace prefix, a parser can correctly reconcile any potential name
collisions. The process of using a namespace prefix creates qualified names for each of the elements and
attributes used within a document.