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
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

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

Victims of Sandy Hook

Stop the Slaughter of Innocents. Congress is bought and paid for by gun lunatics and gun promotion groups. If you want to live in a safe America, help buy Congress back for America. Send a donation to Mayors Against Illegal Guns, 909 Third Avenue, 15th Floor New York, NY 10022


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-2017 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268