6. • If these XML fragments were added together,
– There would be a name conflict.
• Both contain a <table> element,
– But the elements have different content and meaning.
• By using namespaces,
– XML parsers can easily tell the difference b/w the two <table> elements.
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
In this example,
there will be no conflict
because the two <table>
elements have different
names.
8. A Default Declaration
• 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
• Example :
<Customer xmlns=”http://www.eps-software.com/po”>
<Name>Travis Vandersypen</Name>
<Order>
<Product>
<Name>Hot Dog Buns</Name>
</Product>
</Order>
</Customer>
Default declaration for the <Customer> element is defined
by using the xmlns attribute
All child elements of <Customer> element are
specified as belonging to the
“http://www.eps-software.com/po” namespace
9. An Explicit Declaration
• This is similar to a default namespace declaration,
– Except a prefix is associated with the xmlns attribute.
• Example : A prefix “po” is with the elements within the document:
<cust:Customer xmlns:cust=“http://www.eps-software.com/customer”
xmlns:ord=“http://www.eps-software.com/order”>
<cust:Name>Travis Vandersypen</cust:Name>
<ord:Order>
<ord:Product>
<ord:Name>Hot Dog Buns</ord:Name>
</ord:Product>
</ord:Order>
</cust:Customer>
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.
10. Identifying the Scope of Namespaces
• By default,
– All child elements within a parent element appear within the parent’s namespace.
• This allows
– All child elements to “inherit” their parent element’s namespace.
• The “inherited” namespace can be overwritten
– By specifying a new namespace on a particular child element.
11. Identifying the Scope of Namespaces
• Example:
• <Customer> element declares a default namespace
• <Order> element also declares a default namespace.
• All unqualified elements within the <Order> element will inherit the namespace
declared by the <Order> element.
<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>
</Order>
</Customer>