XML namespaces are simple in concept but difficult to explain because they can be declared and used in many different ways. In this article, I explain XML namespaces in their most basic form. Let's start with a basic XML document used to store air travel reservations.
Welcome to Bucaro TecHelp!

Bucaro TecHelp
HTTPS Encryption not required because no account numbers or
personal information is ever requested or accepted by this site

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

XML Namespaces Explained

The XML document, shown below, contains a top-level node, identified by the <reservations> tag, which contains several <trip> child nodes.

<reservations>
  <trip>
    <origin>Chicago</origin>
    <destination>Phoenix</destination>
    <seat>d4</seat>  
    <name>Jim Shoe</name>
    <date>06/05/05</date>
  </trip>
  <trip>
    <origin>Boston</origin>
    <destination>Miami</destination>
    <seat>c1</seat>  
    <name>Bob Sled</name>
    <date>06/05/05</date>
  </trip>
</reservations>

The names within the tags in this XML document are referred to as it's "vocabulary". If this was the only XML document we needed to deal with, namespaces would be unnecessary. In fact version 1.0 of the XML specification did not contain namespaces. But let's consider the The XML document shown below which is used to store rental car reservations.

<reservations>
  <trip>
    <make>Chevrolet Cavalier</make>
    <origin>Chicago</origin>
    <destination>Phoenix</destination>
    <name>Jim Shoe</name>
    <date>06/06/05</date>
  </trip>
  <trip>
    <make>Dodge Caravan</make>
    <origin>Miami</origin>
    <destination>Chciago</destination>
    <name>Bob Sled</name>
    <date>06/06/05</date>
  </trip>
</reservations>

This XML document also contains a top-level node, identified by the <reservation> tag. In fact this document uses almost the same vocabulary as the XML document used to store air travel reservations. What happens if we combine both XML documents in a application that displays all reservations.

We can't distinguish, for example an air travel reservations <reservations><trip><origin> from a rental car reservations <reservations><trip><origin>. We need some way to distinguish air travel reservations from rental car reservations. The solution is to identify the air travel reservations with a prefix, as shown below.

<air:reservations>
  <trip>
    <origin>Chicago</origin>
    <destination>Phoenix</destination>
    <seat>d4</seat>  
    <name>Jim Shoe</name>
    <date>06/05/05</date>
  </trip>
  <trip>
    <origin>Boston</origin>
    <destination>Miami</destination>
    <seat>c1</seat>  
    <name>Bob Sled</name>
    <date>06/05/05</date>
  </trip>
</air:reservations>

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro


Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2024 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268