Quackit Logo

FREE Hosting!

With every domain name you register with ZappyHost, you get FREE hosting.

$1.99 Domain Names

With every new non-domain purchase thru ZappyHost, you get a domain name for only $1.99.

XML Local Namespace

Print Version

In the previous lesson, we created a namespace to avoid a name conflict between the elements of two documents we wanted to combine. When we defined the namespace, we defined it against the root element. This meant that the namespace was to be used for the whole document, and we prefixed all child elements with the same namespace.

You can also define namespaces against a child node. This way, you could use multiple namespaces within the same document if required.

Example Local Namespace

Here, we apply the namespace against the title element only:

<books>
  <book>
    <bk:title xmlns:bk="http://somebooksite.com/book_spec">
      The Dream Saga
    </bk:title>
    <author>Matthew Mason</author>
  </book>
  ...
</books>

Multiple Namespaces

You could also have multiple namespaces within your XML document. For example, you could define one namespace against the root element, and another against a child element.

Example:

<bk:books xmlns:bk="http://somebooksite.com/book_spec">
  <bk:book>
    <bk:title>The Dream Saga</bk:title>
    <bk:author>Matthew Mason</bk:author>
    <pub:name xmlns:pub="http://somepublishingsite.com/spec">
      Sid Harta Publishers
    </pub:name>
    <pub:email>author@sidharta.com.au</pub:email>
  </bk:book>
  ...
</bk:books>

Enjoy this website?

  1. Link to this page (copy/paste into your own website or blog):
  2. Add this page to your favorite social bookmarks sites:
               
  3. Add this page to your Favorites

Oh, and thank you for supporting Quackit!