XML Namespaces

[Updated: May 25, 2017, Created: Jul 16, 2016]

XML namespacea is a way to avoid element name conflicts.

They promote reusability and modularity.

XML can reuse elements/attributes from other XML documents instead of recreating them every time. An XML instance may contain element or attribute names from more than one XML document. It will be impossible to keep elements from different documents unique, specially when using someone else's document. Namespace is a way to resolve name conflicts among different element sources (e.g. XSD sources).


Namespace is W3C standard.

Namespaces in XML 1.0 is the W3C recommendation.


Syntax

A namespace consists of a prefix 'xmlns' and namespace reference name (identifier)


Example

<?xml version="1.0" encoding="UTF-8"?>
<o:order  xmlns:o="http://www.example.com/order">
    <o:id>2343</o:id>
    <o:currency>JPY</o:currency>
    <o:quantity>100</o:quantity>
    <o:date>2016-05-07</o:date>
    <b:brokerInfo  xmlns:b="http://www.example.com/brokerInfo">
        <b:id>13456</b:id>
    </b:brokerInfo>
</o:order>


How XML namespace URI is used?

The URI is not usually used by the parser to connect to the remote resource, the XML specification doesn't require that. URI is a way to give the namespace a unique name. However, a namespace can be used as a pointer to a web page containing namespace information but that's not necessary, namespaces just have to be unique.

For a particular case a namespace aware parser (a specialized parser for the that case) should be able to resolve elements/attributes by corresponding namespace URIs and react accordingly.



Default Namespace

If we declare an element's namespace without the identifier e.g. xmlns="http://www.example.com/order", rather than xmlns:o="http://www.example.com/order", it specifies the default namespace within the element.

In that case child elements without namespace identifier are considered to belong to the default namespace.

For example modify the above example and remove identifier with brokerInfo's namespace

<?xml version="1.0" encoding="UTF-8"?>
<o:order  xmlns:o="http://www.example.com/order">
    <o:id>2343</o:id>
    <o:currency>JPY</o:currency>
    <o:quantity>100</o:quantity>
    <o:date>2016-05-07</o:date>
    <brokerInfo  xmlns="http://www.example.com/brokerInfo">
        <id>13456</id>
    </brokerInfo>
</o:order>

Or we can also remove the identifier with the order's namespace like this:

<?xml version="1.0" encoding="UTF-8"?>
<order  xmlns="http://www.example.com/order">
    <id>2343</id>
    <currency>JPY</currency>
    <quantity>100</quantity>
    <date>2016-05-07</date>
    <b:brokerInfo  xmlns:b="http://www.example.com/brokerInfo">
        <b:id>13456</b:id>
    </b:brokerInfo>
</order>

Note that scope of a child element's namespace is overridden if the child element has it's own namespace. That means in above example if we remove identifiers for both order and brokerInfo, the document will still be well formed but brokerInfo will have it's own default namespace.



How namespaces are applied to attributes?

For an XML to be well formed there cannot be a single element having duplicate attribute names. There might be very very rare scenarios where we would need multiple same named attributes within an element. That might only be possible when those attributes are coming from different namespaces. In those cases we need namespace reference/prefix with the attributes as well.

In other cases we might have to use an attribute name from other namespace even though there's no multiple attribute names conflict.

Example

We are going to modify above example to show how to use attribute prefix. brokerInfo elements need an attribute of history-id attribute from order schema. history-id presumably corresponds to the history of the broker related to the current order.

<?xml version="1.0" encoding="UTF-8"?>
<o:order  xmlns:o="http://www.example.com/order">
    <o:id>2343</o:id>
    <o:currency>JPY</o:currency>
    <o:quantity>100</o:quantity>
    <o:date>2016-05-07</o:date>
    <b:brokerInfo o:history-id="32321"
        xmlns:b="http://www.example.com/brokerInfo">
        <b:id>13456</b:id>
    </b:brokerInfo>
</o:order>

If no namespace prefix is provided for the attribute name, the attribute will not belong to any namespace unless there's a default namespace.

See Also