Understanding Namespaces

People having the same names yet not being related is because it parallels the problem in XML when different markup languages have elements and attributes that are named the same. The XML problem is much more severe, however, because XML applications aren't smart enough to judge the difference between the context of elements from different markup languages that share the same name. For example, a tag named <goal> would have a very different meaning in a sports markup language than the same tag in a markup language for a daily planner. If you ever used these two markup languages within the same application, it would be very important for the application to know when you're talking about a goal in hockey and when you're talking about a personal goal. The responsibility falls on the XML developer to ensure that uniqueness abounds when it comes to the elements and attributes used in documents. Fortunately, namespaces make it possible to enforce such uniqueness without too much of a hassle.

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.

by BrainBellupdated