Namespaces give a unique name or URI as identifiers of elements and attributes. XML Namespace specification has added "Qualified Names" to XML.

Qualified Names

A tag or attribute name associated with the namespace is called qualified name or Qname. It is used to refer to a particular element or attribute in XML document. Since URI reference is long and may contain prohibited characters for element or attribute naming, qualified names are used.

It is used as a “URI:local name” pair where, URI is the namespace to which the local name belong to. Qname makes it possible to have same local name for multiple elements and attributes. Using HTTP URI for namespace helps in storing all the elements, attributes without requiring a central repository.

Example of qualified names in XML

<?xml version="1.0" encoding="UTF-8"?>

<root xmlns:r=”http://furniture.com/table”>

      <r:table/>

</root>

Here “r” is the abbreviation to URI http://furniture.com/table. Here “r:table” is the Qname with "r" as namespace reference and table as local part.

URIs and namespaces

Uniform Resource Identifier (URI) identifies an internet resource. It is used to define namespaces of attributes and elements. The URI is not used by parser to look up information. They give uniqueness to the elements. 

Namespace declarations

A namespace is defined by the xmlns attribute, which expands to XML namespace attribute. The namespace defined for an element is shared with all it’s child elements. Elements defined in default namespaces do not have to carry prefixes.

Syntax for namespace declaration

xmlns:prefix=”URI”

The namespaces can be declared at the XML root or where it is used.

Breif example of namespace declaration

<root xmlns:r=”http://furniture.com/table”>

Namespaces and attributes

An attribute declared generally, is not said to be present in a namespace. Similarly if the parent is in a namespace, attribute is not said to be in namespace. Through it is unlikely, there could be a name collision for attribute, and namespace can be defined for attribute. In a document element and attributes can be in different namespace.

In the example below only the attribute “breadth” is said to be in a namespace and "length" is not in namespace.      

Example of namespace for attributes

<?xml version="1.0" encoding="UTF-8"?>

<root xmlns:r=”http://furniture.com/table”>

<table length=”300”>

</table>

      <r:table length=”300”>

</r:table>

<r:table r:breadth=”90”>

</r:table>

</root>

By adding a namespace in documents, it’s declaration can be added to DTD’s as attribute. But making this forces to declare all the elements in their namespace’s in DTD.  If the namespace is declared in root, all it’s elements and child elements should declare their namespace values. Therefore it is advised not to add attributes in DTD but use XMLSchema instead.

Syntax below declares the namespace for above example to be included in DTD.

Syntax for declaring namspace

<!ATTLIST root xmlns:r CDATA #fixed “http://furniture.com/table”>

 

›› go to examples ››