XML Namespaces Made Simple
By Stephen Bucaro
XML (Extensible Markup Language) is a very powerful language becuase it can be used to create
databases that are well formed and human readable. But if you're going to use your XML file
for more than just your own local purposes, and exchange it with other organizations, you need a
way to validate the XML.
The Document Type Definition (DTD) language was created for that purpose. However DTD has
limited support for datatypes. For example it has no way to specify a type as numeric. It has only
PCDATA (Parsed Character DATA). Also DTDs have no notation for namespaces. For this reason, on
2 May 2001 the W3C XML Schema 1.0 specification was released.
Namespaces are a source of much complexity and confusion in XML. Why do we need namespaces?
Lets consider the XML tag <name>. Maybe it refers to a person's name, maybe the name of a
book, maybe a product name. If you use your XML file for just your own local purposes, you know
what it refers to, but if you're going to exchange your XML file with other organizations, you
need to be more specific. A namespace is a set of names in which all names are unique. We could
use the following system.
The part before the dot would be the namespace. This clears things up a little, but what if
several organizations have XML files using the <employee.name>,<book.name>, or
<product.name> XML tag? As you can see these names are still not unique enough. The W3C
XML Schema 1.0 specification requires XML namespace identifiers to conform to a specific syntax
— the syntax for Uniform Resource Identifier (URI) references.
A URI is 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,
There are two forms of URI: Uniform Resource Locators (URL) and Uniform Resource Names (URN).
Either type of URI may be used as a namespace identifier.
Shown below are examples of URLs that could be used as namespace identifiers:
Shown below are examples of URNs that could be used as namespace identifiers:
Because of their specificity and length, URLs can be assumed to be unique identifiers. But to
guarantee uniquess you would use a URN. Authors must register their URN namespace identifier
with an Internet naming authority.
XML processors treat namespace identifiers as inactive strings and never actually attempt to
access the resource identified by the URI. So it really doesn't matter what you use as a URI, as
long as it has the length required to be unique. However most XML namespaces are defined in
formal specifications that describe the names of elements and attributes along with their semantics.
The XML Schema working group (http://www.w3.org/XML/Schema) has put together a specification
(XML Schema) that defines an XML-based syntax for defining elements, attributes, and types in a namespace.
<element name="id" type="long"/>
<element name="name" type="string"/>
<element name="language" type="string"/>
<element name="rating" type="double"/>
The example schema shown above defines the namespace http://www.bucarotechelp.com/student
as containing four named elements: student, id, name, language, and rating. In addition to
providing a namespace, this schema also provides metadata, such as the order of student child
elements and their datatypes.