XML Namespace: What Is A Namespace?
XML Namespace: What Is A Namespace?
XML Namespace
Abstract
A namespace is a set of names in which all names are unique. Any logically related set of names where each name
should be unique is a namespace. Namespaces make it easier to come up with unique names.
An XML namespace is a collection of names, identified by a URI reference [RFC2396], which are used in XML
documents as element types and attribute names. 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.
Introduction
What is a Namespace?
A namespace is a set of names in which all names are unique. Any logically related set of names where each
name should be unique is a namespace. Namespaces make it easier to come up with unique names. Before a new
name is added to a namespace, a namespace authority ensures that the new name doesn?t already exist in the
namespace.
Namespaces themselves must also be given names in order to be useful. Once a namespace has a name, it is possible
to refer to its members. For example, Micosoft.RadioService and AOL.RadioService. The names in the namespace
should be unique. If this cannot be guaranteed, then the actual namespace names themselves could also be placed
into a name space of their own. For example,US.Microsoft.RadioService and CHINA.Microsoft.RadioService. In order to
guarantee the uniqueness of namespace names, this pattern can be iterated as many times as necessary.
More than one namespace may appear in a single XML document, to allow a name to be used more than once. Each
reference can declare a prefix to be used by each name.
Most developers, including the XML 1.0 authors themselves, knew that this would eventually cause too much
ambiguity in large XML-based distributed systems.
Since element names in XML are not fixed, very often a name conflict will occur when two different documents use the
same names describing two different types of elements.
<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
<table>
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
If these two XML documents were added together, there would be an element name conflict because both documents
contain a <table> element with different content and definition.
The Namespaces in XML Recommendation is the W3C's solution to the XML 1.0 naming woes. This specification
defines how to extend the XML 1.0 concrete syntax to support namespaces. Because most developers consider this
addition fundamental and absolutely necessary, it is often given the respect of an official XML 1.0 addendum, even
1 of 9 08/12/2008 22:42
XML Namespace http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_ns_...
though it isn't one. In fact, today many developers refuse to refer to XML 1.0 alone but rather as "XML 1.0 +
Namespaces" for the same reason.
The Namespaces in XML Recommendation defines the syntax for naming XML namespaces as well as the syntax for
referring to something in an XML namespace. It doesn't address, however, the syntax for defining what's in the XML
namespace. This was left to another specification, namely XML Schema. There are more details in the XML Schema
section.
XML schema provides a "namespace" mechanism, which uses additional names to distinguish elements with the same
name but different meaning in different contexts.
The solution is to prefix a unique string to elements or attributes that need to be distinguished.
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
Now the element name conflict is gone because the two documents use a different name for their <table> element
(<h:table> and <f:table>). By using a prefix, we have created two different types of <table> elements.
The XML namespaces provides a way to distinguish between duplicate element types and attribute names. Duplicates
can occur when an XML document contains element types and attributes from more than one different XML schemas
(or DTD).
XML Namespace
Definition
An XML namespace is a collection of names, identified by a URI reference [RFC2396], which are used in XML
documents as element types and attribute names. 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.
URI references which identify namespaces are considered identical when they are exactly the same character-
for-character. Note that URI references which are not identical in this sense may in fact be functionally equivalent.
Examples include URI references which differ only in case, or which are in external entities which have different
effective base URIs.
Names from XML namespaces may appear as qualified name, which contain a single colon, separating the name into a
namespace prefix and a local part. The prefix, which is mapped to a URI reference, selects a namespace. The
combination of the universally managed URI namespace and the document's own namespace produces identifiers that
are universally unique. Mechanisms are provided for prefix scoping and defaulting.
URI references can contain characters not allowed in names, so cannot be used directly as namespace prefixes.
Therefore, the namespace prefix serves as a proxy for a URI reference. An attribute-based syntax described below is
used to declare the association of the namespace prefix with a URI reference; software which supports this namespace
proposal must recognize and act on these declarations and prefixes.
Namespaces in XML
2 of 9 08/12/2008 22:42
XML Namespace http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_ns_...
Any element or attribute in an XML namespace is uniquely identified by two parts: a namespace name (typeically a
URI) and a local name. Such a two-part name is known as a qualified name or QName.
In an XML document we use a namespace prefix to qualify the local names of both elements and attributes. A prefix is
really just an abbreviation for the namespace identifier (URI) which is typically quite long and is a painful job to add it
to element and attribute names in order to make them unique. ?
The prefix is first mapped to a namespace identifier through a namespace declaration. ?The syntax for a namespace
declaration is:
xmlns:<prefix>='<namespace identifier>'
A namespace declaration looks just like an attribute (on an element), but they're not officially considered attributes in
terms of the logical document structure (that is, they won't appear in an element's attributes collection when using
the DOM).
An example namespace declaration, which associates the namespace prefix soap with the namespace name
http://schemas.xmlsoap.org/soap/envelope:
<soap:Envelope
xmlns:soap='http://schemas.xmlsoap.org/soap/envelope'>
<!-- the "soap" prefix is bound to http://schemas.xmlsoap.org/soap/envelope -->
<!-- for the "Envelope" element and contents -->
</soap:Envelope>
Although a namespace usually looks like a URL, that doesn't mean that one must be connected to the Internet to
actually declare and use namespaces. Rather, the namespace is intended to serve as a virtual "container" for
vocabulary and undisplayed content that can be shared in the Internet space. In the Internet space URLs are
unique?hence you would usually choose to use URLs to uniquely identify namespaces. Typing the namespace URL in a
browser doesn't mean it would show all the elements and attributes in that namespace; it's just a concept.
Note that the address used to identify the namespace, is not used by the parser to look up information. The only
purpose is to give the namespace a unique name. However, very often companies use the namespace as a pointer to
real Web page containing information about the namespace.
The namespace attribute is placed in the start tag of an element. A namespace prefix is considered in-scope on the
declaration element as well as on any of its descendant elements. Once declared, the prefix can be used in front of
any element or attribute name separated by a colon (such as s:student). This complete name including the prefix is
the lexical form of a qualified name (QName):
The prefix associates the element or attribute with the namespace identifier mapped to the prefix currently in scope.
The following XML document describes about a room element. With two different namespaces defined for prefix 'h' and
'f', the 'table' elements are not conflict inside one XML document.
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
Since all XML documents must be well-formed, the start-tag to every element must have a matching end-tag. If a
namespace declaration is the attribute to an element, it must go in an element start-tag. A namespace declaration is
"in scope" for the entire content of that element, including the other attributes in the start-tag itself.
3 of 9 08/12/2008 22:42
XML Namespace http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_ns_...
Since namespaces "h" and "f" were declared in the document element of the room document, any element or attribute
in the document with the prefix "h:" is identified with the given http://www.w3.org/TR/html4 URI.
It is not possible to get ride of the prefix and qualify an XML element directly with the namespace name identifier.
Let?s rewrite the above XML document:
<http://www.w3.org/TR/html4:table>
<http://www.w3.org/TR/html4:tr>
<http://www.w3.org/TR/html4:td>Apples</http://www.w3.org/TR/html4:td>
<http://www.w3.org/TR/html4:td>Bananas</http://www.w3.org/TR/html4:td>
</http://www.w3.org/TR/html4:tr>
</http://www.w3.org/TR/html4:table>
<http://www.w3schools.com/furniture:table>
<http://www.w3schools.com/furniture:name>African Coffee Table</http://www.w3schools.com/furniture:name>
<http://www.w3schools.com/furniture:width>80</http://www.w3schools.com/furniture:width>
<http://www.w3schools.com/furniture:length>120</http://www.w3schools.com/furniture:length>
</http://www.w3schools.com/furniture:table>
Such an XML document is unreadable regardless of the fact that the syntax is not valid, as there are two colons (":")
in the qualifier.
Naming Namespaces
XML namespace identifiers must follow the generic syntax for Uniform Resource Identifier (URI) defined by RFC 2396.
Just like when you define a namespace in a programming language like C++, there are restrictions on the characters
that may be used in the name.
A (Uniform Resource Identifier) URI is defined as a compact string of characters for identifying an abstract or
physical resource. In most situations, URI references are used to identify physical resources (Web pages, files to
download, and so on), but in the case of XML namespaces, URI references identify abstract resources, specifically,
namespaces.
According to the URI specification, there are two general forms of URI: Uniform Resource Locators(URL) and Uniform
Resource Names (URN). Either type of URI may be used as a namespace identifier. Here is an example of two URLs
that could be used as namespace identifiers:
http://www.develop.com/student
http://www.ed.gov/elementary/students
And here are a few examples of URNs that could also be used as namespace identifiers:
urn:www-develop-com:student
urn:www.ed.gov:elementary.students
urn:uuid:E7F73B13-05FE-44ec-81CE-F898C4A6CDB4
The most important attribute of a namespace identifier is that it is unique. Authors can guarantee the uniqueness of a
URL by registering a domain name with an Internet naming authority. Then it's the author's responsibility to make
sure that all strings used after the domain name are unique.
URNs work the same way. The following is basic URN syntax:
To guarantee the uniqueness of a URN, authors must again register their namespace identifier with an Internet
naming authority. The author is then responsible for following a scheme for generating unique namespace-specific
strings.
Organizations defining XML namespaces should develop a consistent scheme for creating new namespace names. The
W3C, for example, is constantly defining new XML namespaces. They use a fairly intuitive heuristic that uses the
current year and the name of the working group.
4 of 9 08/12/2008 22:42
XML Namespace http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_ns_...
By definition, a URI is unique, so there is never a need to layer additional namespaces on top of XML namespace
identifiers. As long as the namespace author guarantees the uniqueness of the namespace identifier, it's always
possible to uniquely identify something in XML with only a single namespace qualifier. This greatly simplifies the job
of working with namespaces in XML.
XML processors treat namespace identifiers as opaque strings and never as resolvable resources. Let me repeat:
namespace identifiers are just strings! Two namespace identifiers are considered identical when they are exactly the
same, character for character.
In the end, it really doesn't matter which type of URI reference you choose to use. Many developers like to use URLs
because they are easier to read and remember, while others prefer URNs because of their flexibility. Whichever type
you choose, make sure you know how to ensure uniqueness.
Declaring Namespaces
Definiation
[Definition:] A namespace is declared using a family of reserved attributes. Such an attribute's name must either be
xmlns or have xmlns: as a prefix. These attributes, like any other XML attributes, may be provided directly or by
default.
[Definition:] The attribute's value, a URI reference, is the namespace name identifying the namespace. The
namespace name, to serve its intended purpose, should have the characteristics of uniqueness and persistence. It is
not a goal that it be directly usable for retrieval of a schema (if any exists). An example of a syntax that is designed
with these goals in mind is that for Uniform Resource Names [RFC2141]. However, it should be noted that ordinary
URLs can be managed in such a way as to achieve these same goals.
[Definition:] If the attribute name matches PrefixedAttName, then the NCName gives the namespace prefix, used to
associate element and attribute names with the namespace name in the attribute value in the scope of the element to
which the declaration is attached. In such declarations, the namespace name may not be empty.
[Definition:] If the attribute name matches DefaultAttName, then the namespace name in the attribute value is that
of the default namespace in the scope of the element to which the declaration is attached. In such a default
declaration, the attribute value may be empty.
An example namespace declaration, which associates the namespace prefix edi with the namespace name
http://ecommerce.org/schema:
<x xmlns:edi='http://ecommerce.org/schema'>
<!-- the "edi" prefix is bound to http://ecommerce.org/schema
for the "x" element and contents -->
</x>
5 of 9 08/12/2008 22:42
XML Namespace http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_ns_...
Prefixes beginning with the three-letter sequence x, m, l, in any case combination, are reserved for use by XML and
XML-related specifications.
Default Namespaces
A default namespace declaration uses the following syntax:
Notice that there is not prefix. Defining a default namespace for an element saves us from using prefixes in all the
child elements.
All unqualified child element names of the element which defines the default namespace are automatically associated
with the specific namespace identifier. The default namespace declarations, however, have absolutely no effect on
attributes. To apply a namespace to an attribute, the only way is through a prefix (i.e., explicit qualified).
<table xmlns="http://www.w3.org/TR/html4/">
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
In the above document, the default namespace identifier for the <table>, <tr>, and <td> elements is
"http://www.w3.org/TR/htm14/?.
The default namespace can be set to the empty string. This has the same effect, within the scope of the declaration,
of there being no default namespace.
xmlns =""
The containing element and its child elements are declared to be in no namespace. An element that is in the
NONAMESAPCE namespace has its namespace prefix and URI set to empty strings.
<d:student xmlns:d='http://www.develop.com/student'
xmlns='urn:foo' id='3235329' >
<name>John Smith</name>
<language xmlns="" >C#</language>
<score>A</score>
</d:student>
Here, "student" is from the http://www.develop.com/student namespace while "name" and "score" are from the
default namespace urn:foo. The id attribute doesn't belong to a namespace since attributes aren't automatically
associated with the default namespace identifier.
This example also illustrates that you can undeclare a default namespace by simply setting the default namespace
identifier back to the empty string, as shown in the language element (remember you cannot do this with prefix
declarations in XML 1.0). As a result, the language element also doesn't belong to a namespace.
Here's an invalid example of unbinding a prefix per Namespaces in XML 1.0 spec, but a valid example per Namespaces
in XML 1.1:
<language xmlns:lang="">
From this point on, the prefix lang cannot be used in the XML document because it is now undeclared as long as you
6 of 9 08/12/2008 22:42
XML Namespace http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_ns_...
are in the scope of element language. Of course, you can definitely re-declare it.
No namespace exists when there is no default namespace in scope. A {default namespace} is one that is declared
explicitly using xmlns. When a {default namespace} has not been declared at all using xmlns, it is incorrect to say
that the elements are in {default namespace}. In such cases, we say that the elements are in {no namespace}. {no
namespace} also applies when an already declared {default namespace} is undeclared.
If no default namespace is explicitly declared, as in the following XML document, then the URI associated with the
default namespace is the current document.
<?xml version="1.0"?>
<mydoc>
<X:a xmlns:X="http://www.mycorp.com/X.dtd">
<b>this is a test</b>
</X:a>
</mydoc>
It is possible, however, to set up a default namespace whose URI is some other document. To do that, use a
namespace declaration with a URI but no prefix.
Namespace Scope
The scope of an XML namespace declaration declaring a prefix extends from the beginning of the start-tag in which it
appears to the end of the corresponding end-tag, excluding the scope of any inner declarations with the same xmlns:
<prefux> or xmlns (i.e., default namespace) declaration (in such case, the namespace name identifier has been
changed).
In other words, the namespace declaration is considered to apply to the element where it is specified and to all
elements within the content of that element, unless overridden by another namespace declaration with the same
NSAttName part. For example:
<?xml version="1.0"?>
<!-- all elements here are explicitly in the school namespace -->
<school:school xmlns:school='http://www.theschool.com/'>
<school:department>Computer Science</school:department>
<school:building>Bill Gates Hall</school:building>
<school:lab>210</school:lab>
<school:classroom>212, 214, 216</school:classroom>
</school:school>
Multiple namespace prefixes can be declared as attributes of a single element, as shown in this example:
<?xml version="1.0"?>
<!-- both namespace prefixes are available throughout -->
<bk:book xmlns:bk='urn:loc.gov:books'
xmlns:isbn='urn:ISBN:0-395-36341-6'>
<bk:title>Cheaper by the Dozen</bk:title>
<isbn:number>1568491379</isbn:number>
</bk:book>
Re-declaring Namespaces
Namespace prefixes can be overridden by redeclaring the prefix at a nested scope, but it is not possible to undeclare a
namespace prefix.
Correct:
<d:student xmlns:d='http://www.develop.com/student'>
<d:id>3235329</d:id>
<d:name xmlns:d='urn:names-r-us'>John Smith</d:name>
<d:language>C#</d:language>
<d:score>A</d:score>
</d:student>
7 of 9 08/12/2008 22:42
XML Namespace http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_ns_...
Incorrect:
<d:student xmlns:d='http://www.develop.com/student'>
<d:id xmlns:d=''>3235329</d:id>
......
</d:student>
Uniqueness of Attributes
The W3C "Namespaces in XML" specification places restrictions on setting the name and namespace of an attribute.
No tag can contain two attributes with identical names, or with qualified names that have the same local name and
have prefixes that are bound to identical namespace names.
However, each of the following good tag is legal, because the default namespace does not apply to attribute names:
Uniqueness of Attributes
The W3C "Namespaces in XML" specification places restrictions on setting the name and namespace of an attribute.
No tag can contain two attributes with identical names, or with qualified names that have the same local name and
have prefixes that are bound to identical namespace names.
However, each of the following good tag is legal, because the default namespace does not apply to attribute names:
Summary
8 of 9 08/12/2008 22:42
XML Namespace http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_ns_...
A namespace is used to qualify XML element and attribute. In other words, both elements and attributes may
be namespace-qualified.
A prefix is not used to qualify an element. What effectively qualifies the element is the namespace to which the
prefix is mapped.
The scope of a declared namespace begins at the element where it is declared and applies to all the elements
within the content of that element, unless overridden by another namespace declaration with the same prefix
name.
A {default namespace} exists only when you have declared it explicitly. It is incorrect to use the term {default
namespace} when you have not declared it.
Both prefixed and {default namespace} can be undeclared in XML 1.1, but prefixed can not be undeclared in
XML 1.0.
9 of 9 08/12/2008 22:42