Home | | Service Oriented Architecture | Namespaces in XML

Chapter: XML and Web Services : Essentials of XML : The Fundamentals of XML

Namespaces in XML

Declaring Namespaces, Identifying the Scope of Namespaces.

Namespaces in XML


As XML documents become more predominate, the increasing interdependence of XML documents will require the use of technologies to separate XML documents with multi-ple, possibly conflicting tag sets. For example, our shirt XML DTD may include part of a more general “apparel” DTD. In this case, a mechanism is provided to associate these various XML data sources. In order for a processing application to properly understand and associate the correct elements, it must know which tag set the elements come from.


XML solves this problem with namespaces. Namespaces use a colon-delimited prefix to associate external semantics with elements that can be identified via a Universal Resource Identifier (URI). The use of the namespace-identified element then acts as if the element was defined in a local manner. Listing 2.17 shows an example of namespace usage.


LISTING 2.17    Namespace Example

<?xml  version=”1.0”?>


<shirt:shirt xmlns:shirt=”http://xmlshirts.org/schema” xmlns:apparel=”http://xmlapparel.org/schema”>


<shirt:model>Zippy Tee</shirt:model>



<shirt:description>This is a <b>funky</b> Tee shirt similar to the Floppy Tee shirt






Because XML is an open standard in which XML authors are free to create whatever ele-ments and attributes they wish, it’s inevitable that multiple XML developers will choose the same element and attribute names for their standards. However, they could mean, depending on the document, entirely different things. For instance, let’s examine the following sample XML document:




<Name>John Smith</Name> </Customer>

This sample document contains the root element <Customer>, which contains a child element called <Name>. We can clearly determine that the <Name> element contains the name of the customer referred to by the <Customer> element.


Now, let’s look at another sample XML document. This time, however, the XML document contains details regarding a product, as shown here:




<Name>Hot Dog Buns</Name> </Product>


You can see that this document contains a <Product> element as the root element and a <Name> element, which contains the name of the product. Now, let’s look at another typi-cal scenario. Let’s imagine that the customer places an order for a product (a very typical scenario for a Web store). The following XML document could be constructed to indi-cate that a customer has placed an order for a particular product:




<Name>John Smith</Name> <Order>




<Name>Hot Dog Buns</Name> </Product>






We can easily distinguish the differences between the two <Name> elements. The first <Name> element, which appears as a child of the <Customer> element, contains the cus-tomer’s name. The second <Name> element, on the other hand, contains the product’s name. However, how can the parser tell the difference? It can’t, not unless we explicitly tell it what the difference is. This is where XML namespaces come in. By using name-spaces, XML parsers can easily tell the difference between the two <Name> elements.


Therefore, modifying the preceding XML document to specify the appropriate name-spaces turns it into this:




<cust:Name xmlns:cust=”customer-namespace-URI”>John Smith</cust:Name> <Order>




<prod:Name xmlns:prod=”product-namespace-URI”>Hot Dog Buns</prod:Name>







Now, the XML parsers can easily tell the difference between any validation rules between the customer’s <Name> element and the product’s <Name> element.

Declaring Namespaces


Within an XML document, namespaces can be declared using one of two methods: a default declaration or an explicit declaration. Which method to use is completely open and left up to you; either way will suffice.


A default namespace declaration specifies a namespace to use for all child elements of the current element that do not have a namespace prefix associated with them. For instance, in the following XML document, a default declaration for the <Customer> ele-ment is defined by using the xmlns attribute on the parent element without specifying or attaching a prefix to the namespace:


<Customer xmlns=”http://www.eps-software.com/po”> <Name>Travis Vandersypen</Name>






<Name>Hot Dog Buns</Name> </Product>






For this XML document, all child elements of the <Customer> element are specified as belonging to the http://www.eps-software.com/po namespace.


Sometimes, however, it may be necessary and more readable to explicitly declare an ele-ment’s namespace. This is accomplished much the same way in which a default name-space is declared, except a prefix is associated with the xmlns attribute. If you examine the following XML document, you can see that a prefix of po is with the elements within the document:


<po:Customer xmlns:po=”http://www.eps-software.com/po”>

<po:Name>Travis Vandersypen</po:Name>






<po:Name>Hot Dog Buns</po:Name> </po:Product>






One thing worth pointing out here is that the prefix associated with the elements is a shorthand notation to be used in place of the full namespace. Although the preceding XML document provides a rather simple scenario for explicitly identifying namespaces, the true power behind explicitly declaring namespaces becomes clear when you utilize elements from different namespaces, as is the case in the following XML document:

<cust:Customer xmlns:cust=”http://www.eps-software.com/customer”


<cust:Name>Travis Vandersypen</cust:Name> <ord:Order>




<ord:Name xmlns:prod=”product-namespace-URI”>Hot Dog Buns</ord:Name> </ord:Product>






From looking at this example, you can see that two different namespaces are referenced: one for customers and one for orders. This allows a different set of rules to be applied for customer names versus product names.


Identifying the Scope of Namespaces


By default, all child elements within a parent element, unless indicated otherwise by ref-erencing another namespace, appear within the parent’s namespace. This allows all child elements to “inherit” their parent element’s namespace. However, this “inherited” name-space can be overwritten by specifying a new namespace on a particular child element.


Let’s examine the following XML document:


<Customer xmlns=”http://www.eps-software.com/customer”> <Name>Travis Vandersypen</Name>


<Order xmlns=”http://www.eps-software.com/order”> <Product>


<Name>Hot Dog Buns</Name> </Product>






In the preceding XML document, the <Customer> element declares a default namespace located at http://www.eps-software.com/customer. All elements contained within the <Customer> element that do not explicitly qualify a namespace “inherit”, the namespace declared by the <Customer> element. However, the <Order> element also declares a default namespace. Starting at the <Order> element, all unqualified elements within the <Order> element will inherit the namespace declared by the <Order> element.

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
XML and Web Services : Essentials of XML : The Fundamentals of XML : Namespaces in XML |

Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.