A namespace is a collection of element and attribute names that can be used in an XML document. To draw a comparison between an XML namespace and the real world, if you considered the first names of all the people in your immediate family, they would belong to a namespace that encompasses your last name. XML namespaces represent groups of names for related elements and attributes. Most of the time an individual namespace corresponds directly to a custom markup language, but that doesn't necessarily have to be the case. You also know that namespaces aren't a strict requirement of XML documents.
The purpose of namespaces is to eliminate name conflicts between elements and attributes. To better understand how this type of name clash might occur in your own XML documents, consider an XML document that contains information about a video and music collection. You might use a custom markup language unique to each type of information (video and music), which means that each language would have its own elements and attributes. However, you are using both languages within the context of a single XML document, which is where the potential for problems arises. If both markup languages include an element named title
that represents the title of a video or music compilation, there is no way for an XML application to know which language you intended to use for the element. The solution to this problem is to assign a namespace to each of the markup languages, which will then provide a clear distinction between the elements and attributes of each language when they are used.
In order to fully understand namespaces, you need a solid grasp on the concept of scope in XML documents. The scope of an element or attribute in a document refers to the relative location of the element or attribute within the document. If you visualize the elements in a document as an upside-down tree that begins at the top with the root element, child elements of the root element appear just below the root element as branches (see Figure 5.1). Each element in a "document tree" is known as a node. Nodes are very important when it comes to processing XML documents because they determine the relationship between parent and child elements. The scope of an element refers to its location within this hierarchical tree of elements. So, when I refer to the scope of an element or attribute, I'm talking about the node in which the element or attribute is stored.
Figure 5.1. An XML document coded in ETML can be visualized as a hierarchical tree of elements, where each leaf in the tree is known as a node.
In this figure, the hypothetical ETML example markup language first mentioned in Defining Data With DTD Schemas, "Defining Data with DTD Schemas," is used to demonstrate how an XML document consists of a hierarchical tree of elements. Each node in the tree of an XML document has its own scope, and can therefore have its own namespace.
Scope is important to namespaces because it's possible to use a namespace within a given scope, which means it affects only elements and attributes beneath a particular node. Contrast this with a namespace that has global scope, which means the namespace applies to the entire document. Any guess as to how you might establish a global namespace? It's easy you just associate it with the root element, which by definition houses the remainder of the document. You learn much more about scope as it applies to namespaces throughout the remainder of this tutorial. Before you get to that, however, it's time to learn where namespace names come from.