Resolve conflicts with XML namespaces

Using XML to define your organization's business data and process information can be difficult when you have overlapping departments and redundant data definitions. XML namespaces can help resolve conflicts.
Written by Brian Schaffner, Contributor
Using XML to define your organization's business data and process information can be difficult when you have overlapping departments and redundant data definitions. For example, having two XML elements called CustomerInformation can be detrimental to your XML implementation if the two elements are not identical. XML namespaces can help resolve conflicts between element names.
Namespaces solve the problem of collision between identically named elements. A namespace is a domain that contains a set of names. Shoe sizes make a good analogy. A size nine is not always a size nine, not because different makers have different tolerances, but because a women's size nine is not the same as a men's size nine. These sizes exist in two different domains: women's and men's. These domains are analogous to namespaces. XML namespaces exist to help identify and resolve conflicts between elements that have the same name but mean different things. This situation can occur when elements from different Document Type Definitions (DTDs) are included in the same XML document. In this scenario, each DTD would maintain its own unique XML namespace, and the resulting XML document would refer to its elements in the context of the namespace they belong to.

How they work
Namespaces are identified within the XML document where they are used. An XML document may use multiple namespaces simultaneously. Each namespace is identified using a Unified Resource Identifier (URI), which points to an external file that defines the namespace. In addition to identifying the namespace resource, the XML document may assign the namespace a variable name that will be used in element names to identify the namespace they belong to. The following example illustrates how multiple namespaces might be defined in an XML document:
    <addr:Street>9902 Broadway</addr:Street>
In this example, two namespaces have been declared. The first one refers to the customers namespace and indicates that elements and attributes in the customers namespace will be prefixed by the tag cust. The second one refers to the address namespace, identified by the addr prefix.

Default namespace
In addition to declaring namespaces identified with a prefix, you can declare a default namespace. The default namespace handles the names of elements and attributes that don't contain a namespace prefix. The following example illustrates the use of both a default namespace and a named namespace:
      <cust:Name>Joe's Corp.</cust:Name>
        <Name>Widget Bracket</Name>

As you can see, both the <Name> and <Number> elements have been used more than once. Because the orders namespace has been declared as the default, the orders elements do not require a prefix. Thus, the elements <Name> and <Number> in the <Item> element refer to the orders definition of an item name and item number. The <cust:Name> and <cust:Number> elements above refer to the customers namespace definition of a customer name and customer number.

Putting names in their place
Creating XML documents and specifications that don't use overlapping or duplicate names can be a difficult task. This undertaking can be complicated even more when documents from multiple organizations are brought together. Using XML namespaces can help alleviate issues that arise where XML elements and attributes use identical names.

Editorial standards