XML Namespaces 101

XML Namespaces also carry typical advantages of using namespaces, i.e. all variables related to a particular category can go under that namespace.

Here is an example of namespaces being used for Spring.

Lets say in an xml file we have information about an html table and a real table.

<root>
  <item>
    <table>
      <name>Ikea Coffee table</name>
      <size>Large</size>
      <html>
        <table>
          <tr>
            <td>Name</td>
            <td>Size</td>
          </tr>
          <tr>
            <td>Ikea Coffee table</td>
            <td>Large</td>
          </tr>
        </table>
      </html>
    </table>
  </item>
</root>

Ok, there is a problem here. Clearly, there should be a distinction between an html table and real table. Namespaces to the rescue.

<root xmlns="http://www.softwareeverydayblog.com/" xmlns:table="http://www.softwareeverydayblog.com/realtable" xmlns:htmltable="http://www.softwareeverydayblog.com/htmltable"</strong> >
  <item>
    <table:table>
      <name>Ikea Coffee table</name>
      <size>Large</size>
      <html>
        <htmltable:table>
          <tr>
            <td>Name</td>
            <td>Size</td>
          </tr>
          <tr>
            <td>Ikea Coffee table</td>
            <td>Large</td>
          </tr>
        </htmltable:table>
      </html>
    </table:table>
  </item>
</root>

This works. An html table (xmlns:htmltable) and a real table (xmlns:table) have their own namespaces. xmlns is the default namespace. By default all the elements without a namespace go into this namespace (e.g. , etc).