XML - Managing Data Exchange/Namespace
|Previous Chapter||Next Chapter|
|← JDNC||Business Intelligence and XML →|
Upon completion of this chapter, you will:
- be able to understand what XML Namespace is and its purpose
- be able to recognize XML Namespace structure and what each part is doing
- be able to think of organizations in which Namespace would be necessary
What is Namespace?Edit
An XML namespace is a collection of names that are identified by a Uniform Resource Identifier (URI) reference, which are used in XML documents as element types and attribute names. URIs were used simply because they are a well-known system for creating unique identifiers. Namespaces consist of several parts including local names, namespace URIs, prefixes and declarations. The combination of a local name and a namespace is called a universal name. You might find it easier to think of a namespace as a dictionary that is a source of definitions for items that you use within an XML document.
All schemas include the namespace http://www.w3.org/2001/XMLSchema-instance. You can think of this as the master dictionary to which all schemas must refer because it defines the fundamental items of an XML schema. The namespace's address looks like a URL, but in XML we use the broader term Uniform Resource Identifier (URI).
Because a document can refer to multiple namespace, we need a convenient short form for referencing the namespace. One of the common forms used is xsd as illustrated in the following.
The xlmns informs XML that you are referencing a name space, and the xsd indicates this is the short form of the namespace.
For example, you might use the following line of code in an XML schema
<xsd:element name="item" type="xsd:string">
The previous line of code states that the definition of element name and string are found in "http://www.w3.org/2001/XMLSchema-instance"
Namespace enables you to use elements described in multiple schemas within your XML document, so the short form of a namespace's URI is useful for identifying the namespace to which you are referring.
Namespace in XML was a new W3C recommendation in January, 1999. Namespace was created to be a pretty simple method to distinguish names used in XML documents. The main purpose of Namespace is to provide programmers a method for which to grab elements and attributes that they want, leaving behind other tags that they do not need. These programmer-friendly names will be unique across the Internet. The XML namespaces recommendation does not define anything except a two-part naming system for element types and attributes.
For additional information regarding the W3C recommendation, follow this link: http://www.w3.org/TR/REC-xml-names/.
When would you use Namespace?Edit
It would mainly be used to avoid naming conflicts. If you don’t have any duplicate elements or attributes in the XML that you use, namespaces are not necessary. It is however beneficial if you have duplicate elements or attributes. It basically makes two part structures that make it unique. Instead of just defining element A, for example, you have to define element A with some other type of identifier. That is where the URI comes into play. The URI in combination with the element or attribute creates your namespace and it is then a universal name.
XML namespaces differ from the "namespaces" conventionally used in computing disciplines in that the XML version has internal structure and is not, mathematically speaking, a set.
This is an example of 2 Namespace declarations:
<Organization xmlns:addr="http://www.example.com/addresses" xmlns="http://www.example.com/files">
The first declaration associates the addr prefix with the “www.example.com/addresses” URI. The second declaration defines www.example.com/files as the default namespace. If there is not a prefix defined for that element, a default namespace is applied. This default namespace is applied to all elements without a prefix. Please note, however, that default namespaces do not apply directly to attributes.
How Does It Work?Edit
When specifying a universal name in an XML document, you use an abbreviation based on an optional prefix that's attached to the local name. This abbreviation is called the qualified name or qname. To declare an XML namespace, you use an attribute whose name has the form:
These attributes are often called xmlns attributes and their value is the name of the XML namespace being declared. This is a Uniform Resource Identifier. The first form of the attribute (xmlns:prefix) declares a prefix to be associated with the XML namespace. The second form (xmlns) declares that the specified namespace is the default XML namespace.
Namespace Best PracticesEdit
- Try to limit the number of Namespaces to about 5 per document. More than five namespaces in a document gets unwieldy.
- Make distinctions in XML namespaces only when there are truly distinctions between the things being named.
- Try to stick to documents in namespace normal form wherever possible because they are simplest to read and to process.
- Avoid overriding namespaces frequently because it can cause confusion in your documents.
Example of Namespace UseEdit
Let’s say we are going to be pulling address values from two different sources and address from one source pulls in a mailing address while from the other source, it pulls in a computer IP address. We’ll need to create a Namespace so that we can distinguish the two addresses elements.
Postal Address XML document
<address>100 Elm St., Apt#1</address>
IP Address XML document
How do we distinguish these Address elements in the case that they need to be combined into the same document? We would assign each address name to a namespace. Therefore, it becomes defined in two parts, the address element and the XML namespace. Every time the element Address comes up, it will have to look at two things instead of one for definition, but this look up only has to be performed one time because the combination is universally unique.
In this instance, we could create Namespaces for the address element:
<Example Organization xmlns: addr="http://www.example.com/postal_addresses" xmlns="http://www.example.com/ip_addresses">
The first declaration associates the prefix 'addr' with the URI, "www.example.com/postal_addresses and the second declaration sets "www.example.com/ip_addresses" as the default namespace. So, where a the prefix 'addr' is used, it will pull the postal address and for others, it will pull the IP address.
Defining the location of an XML schemaEdit
Assume you have created a schema, example.xsd, that is located in the same directory as your XML document, example.xml. In the XML document you will indicate the location of the schema with the following code.
Of course, if example.xsd is stored somewhere other than the same directory as example.xml, you specify the full path.
Potential Problems with NamespaceEdit
- Different XML technologies are going to process namespaces differently. Some will see namespace declarations as such and some will just see them as attributes.
- Namespace is a compromise solution that doesn't meet the needs of all users.
- XML namespaces seem simple on their face, but they can cause real confusion and increased complexity if they are not handled or managed correctly. To manage Namespaces correctly, you must understand thoroughly the meaning, rules, and implications of the various concepts that make up the XML namespaces mechanism and stick consistently to simple conventions.
- As mentioned in Best Practices, using more than 5 namespaces can get unwieldy. So, how do large organizations tackle this design difficulty if there is a need for many namespaces? The basic source of this problem is that naming convention for most information architecture is fundamental, but with XML, it was patched together as an afterthought. Namespaces have been very difficult to incorporate smoothly.