The flexibility that XML provides for defining elements may have a downside. There are cases in which you may want to use elements in an XML document from two different DTDs. It is not inconceivable that two elements from two different DTDs have the same name yet an entirely different meaning. For example, one DTD may use the element named
<TITLE>
to refer to the title of a book, while another may use the
<TITLE>
element to refer to the title of a house. To avoid these naming conflicts, often referred to as
name collisions, we have to associate a context with each element name. XML namespaces do just that. Namespaces are defined in W3C recommendation that was added to the XML specification.
- Declaring Namespaces
Namespaces are declared using an xmlns
(xml namespace) declaration with an XML element. An xmlns
declaration associates the namespace with a unique identifier in the form of a URI (Uniform Resource Identifier).
In addition, the xmlns
declaration may specify a prefix that is used to qualify elements and attributes as belonging to that specific namespace.
- Namespace URIs
The URI associated with a namespace does not necessarily contain meaningful content for the definition of the namespace. Thus, a namespace URI that points to
http://www.ucedu.org
cannot be expected to specify a detailed definition of all the elements and attributes that may be used in the ucedu namespace. The
URI's purpose is to define a unique, ubiquitously applicable name with the namespace. The key point to remember here is that
a namespace is a label and it is not required to access the URI specified to be useful.
When creating your first namespace you should use the URI format. The URI must be unique because you do not want it to clash with someone else's URI definition. Since most companies and independent software developers have their own registered domain, it is become fairly standard to use their domain name as a starting point. So your namespace starts with http://www.javadeploy.com. Following the domain name you can use most any combination of characters you want, although you should avoid spaces and the question mark. The definitive list of what is and is not allowed depends on whether you are using XML Namespace version 1.0 or version 1.1
- Namespaces and Prefixes:
A common way to create a globally unique name is by forming a pair that consists of a namespace prefix[1] and a local name. The namespace prefix uniquely identifies the namespace, the local name must be unique within that namespace, and the combination of the two creates a globally unique name. This is how Java classes and packages operate: the name of a package is globally unique (or at least has a good chance of being so), class names are unique within a package, and a fully qualified class name consists of the package name as a prefix, followed by a period and the class name. Reversed URLs are often used as package
names: a good deal of Sun's software is in the com.sun package or its subpackages, for instance:
com.sun.xml.parser.Resolver res = new com.sun.xml.parser.Resolver();
Here, Resolver is a local name, com.sun.xml.parser is both the name of the package and a unique namespace prefix, and the combination of the two is
a fully qualified, globally unique name of Sun's Resolver class. The designers of XML Namespaces had a well-known source of globally
unique names ready at hand: the URL, or its generalization, the URI. It was a natural decision to make it a source of unique namespace prefixes, so that a unique
element name would consist of a URI prefix to identify the namespace and a local name that is unique within that namespace. Conceptually, if our company
URL is http://www.n-topus.com, and we want to put our Address element in a protected namespace, we would say that its fully qualified globally unique
name is something like {https://www.cplusoop.com/basic-com/module4/add-com-class.php} Address. The problem is that this name, as written, is not a legal XML name, and it is
also extremely long. The solution of XML Namespaces is to use a two-step procedure for establishing a namespace. In the first step, a unique namespace URI is declared and mapped to a prefix that contains only legal characters. In the scope of that declaration, the prefix serves as a proxy for the namespace URI. Below is an example:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
</xsl:template>
</xsl:stylesheet>
The second line of this code contains a namespace declaration that maps the URI to a prefix. The prefix for this particular namespace is usually xsl, but any prefix would do as long as it is associated with the right URI. Syntactically, the declaration is an attribute. The name of the attribute consists of a reserved sequence of characters, xmlns, followed by a colon and the prefix to which the namespace URI is mapped; the value of the attribute is the namespace URI. The scope of the declaration includes the element whose start tag contains that declaration and all the descendants of that element , unless there is another declaration with more local scope, as discussed shortly.
The following
series of imagesshows the process for declaring namespaces.
The XML namespace
scope defines the part(s) of the XML document for which the namespace is in effect or is applicable. The namespace scope is indicated by the location of the
xmlns
declaration in the XML document. A namespace definition applies to the element in which it is defined and all descendant (child) elements. As a result, a namespace declaration that is placed with the root element of an XML document is applicable to the entire document. The descendant, or
child, elements inherit the namespace from the
parent elements. Even so, a given element can override the inherited namespace by defining its own. Multiple namespaces can also be used in the same document. For example, the following XML document uses two namespaces identified by the
ucedu
and
ncedu
prefixes respectively, declared in the root element of the document. In this example, the two
<NAME>
elements are clearly distinguished.
<ucedu:COURSE xmlns:ucedu="http://www.ucedu.org/names"
xmlns:ncedu="http://www.ncedu.org/names">
<ucedu:NAME> Introduction to XML programming</ucedu:NAME>
<ncedu:NAME> XML programming </ncedu:NAME>
<ucedu:LOCATION>Sander Hall</ucedu:LOCATION>
<ucedu:TIME> 11:00 am </ucedu:TIME>
</ucedu:COURSE>
Note:
The use of namespace declarations becomes even
more valuable when you are using two or more sets of tags and attributes defined by two or more entities. To declare multiple namespaces, just
add multiple namespace declarations. The next lesson explains the purpose of XML schemas.
[1] namespace prefix: An attribute-based declaration syntax is provided to bind prefixes to namespace names and to bind a default namespace that applies to unprefixed element names; these declarations are scoped by the elements on which they appear so that different bindings may apply in different parts of a document. Processors conforming to this specification MUST recognize and act on these declarations and prefixes.