XML Namespaces Explained
By Stephen Bucaro
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>
|