xml namespaces

XML namespaces are very easy to use, once we understand their purpose.

objective

Purpose of xml namespaces is to prevent namespace collision, ie. to prevent two objects having the same name but meaning two different things.

Bellow is an illustration taken from w3schools.com

Defining a default namespace for an element saves us from using prefixes in all the child elements. It has the following syntax:xmlns=”namespaceURI”

This XML carries HTML table information:

<table xmlns="http://www.w3.org/TR/html4/">
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>

This XML carries information about a piece of furniture:

<table xmlns="http://www.w3schools.com/furniture">
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>

how to define namespace

Basically you have two types of namespaces: default namespaces and prefixed namespaces.
Each namespace must have its own namespace name, which makes it unique (at least different from the other namespace we want to differentiate from). This namespace name can be any string, generally we use a url (it does not important if this uri points to a real document or not, it’s simply used as a unique string identifier). Exemple: ‘xmlns=”http://www.qc4web.com”‘

Declaring a default namespace is as follow: <node1 xmlns=”mynamespacename“>.
By doing so, all nodes within our namespaced <node1> elements that do not have a prefix on their node name are considered part of “mynamespacename”, except if a child node defines its own default namespace too.

Declaring a prefixed namespace is as follow: <h:node2 xmlns:h=”myprefixednamespace“>.
In this example, we’ve chose letter “h” as a prefix, but you can choose another string if you want. From this point on, any node within our <node2> using “h” as node-name prefix will be considered as part of myprerfixednamespace“. Any inner-node not prefixed with “h” will be considered part of first namespace defined within a <node1> parent, or default namespace if none.

namespace scope

We’ve already seen namespace scope characteristics, but here is a more formal (yet crystal clear) definition, taken from official technical paper:

The scope of a default namespace declaration 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 default namespace declarations. In the case of an empty tag, the scope is the tag itself.

This is very good news! It means we do not have to prefix all nodes when we merge two xml documents, we simply need to specify a different default namespace on the root of each merged node!

default namespaces vs. prefixed namespaces

Let’s see w3schools example stated above, imagine we want to merge the two xml documents, using prefixed namespace, we get:

<two_tables xmlns:g="http://www.w3.org/TR/html4/" xmlns:h="http://www.w3schools.com/furniture">
<g:table>
<g:tr>
<g:td>Apples</g:td>
<g:td>Bananas</g:td>
</g:tr>
</g:table>
<h:table>
<h:name>African Coffee Table</h:name>
<h:width>80</h:width>
<h:length>120</h:length>
</h:table>
<two_tables>

while using default namespace, we get:

<two_tables>
<table xmlns="http://www.w3.org/TR/html4/">
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
<table xmlns="http://www.w3schools.com/furniture">
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
</two_tables>

In our case, the result is the same, each <table> has its own namespace (and is considered different).

Basically using prefixed namespaces, instead of default namespaces, is usefull when you mix elements of differents namespaces multiple times within the same document.

sources

  • http://www.w3.org/TR/REC-xml-names/#sec-namespaces (especially section 6, on namespaces scope)
  • http://www.w3schools.com/Xml/xml_namespaces.asp

Post a Comment

Your email is never published nor shared. You're allow to say what you want...