MSc-IT Study Material
June 2010 Edition

Computer Science Department, University of Cape Town

XML Namespaces

Namespaces partition XML elements into well-defined subsets in order to prevent name clashes between elements. If two XML DTDs define the tag "title", which one is implied when the tag is taken out of its document context (e.g., during parsing)? Namespaces disambiguate the intended semantics of XML elements.

Default Namespaces

If no namespace is specified for an element, it is placed in the default namespace. An element's namespace (and the namespace of all of its children) is defined with the special "xmlns" attribute on an element. Example:

<uct	xmlns="http://www.uct.ac.za">

Namespaces are specified using URIs, thus maintaining uniqueness.

Universal Resource Locator (URL) = location-specific

Universal Resource Name (URN) = location-independent

Universal Resource Identifier (URI) = generic identifier

Explicit Namespaces

Multiple active namespaces can be defined using prefixes. Each namespace is declared with the attribute "xmlns:ns", where ns is the prefix to be associated with the namespace. The containing element and its children may then use this prefix to specify their membership to a namespace other than the default.

<uct xmlns="http://www.uct.ac.za" xmlns:dc="http://somedcns"> 
   <dc:title>test XML document</dc:title>
</uct>