XML Namespaces                                                    
                          
XML namespaces provide a simple method for qualifying element and attribute names used in XML
documents by associating them with namespaces identified by URI references.

An XML namespace is a collection of names, identified by a URI reference that are used in XML documents
as element types and attribute names.

Names from an XML namespace may appear as qualified names or by being mapped by some defined
scoping rules. A qualified name contains a single colon separating the name into a prefix and local part.  The
prefix is then mapped to a URI reference.

To declare a namespace, the reserved keyword is xmlns. To define qualified name it with a given schema, the
namespace prefix desired should be provided, in addition to the URI for the resource.


<Contacts xmlns:hr=”http://www.mycompany.com/humanresources/hr”
xmlns:customer=”http://www.mycompany.com/customer”>
…snip
</Contacts>


Now, the ambiguous elements in the instance document can be disambiguated using qualified names.


<Contacts xmlns:hr=”http://www.mycompany.com/humanresources/hr”
xmlns:customer=”http://www.mycompany.com/customer”>
<hr:Person><SSN>111-11-1111</SSN> … </hr:Person>
<customer:Person><FirstName>Fuad</FirstName> … </customer:Person>
</Contacts
>


If no namespace prefix is provided, the schema is assumed to be the default for the scope of the namespace.

The scope of a namespace can either be default or qualified. By default, the scope of a namespace is at the
element level it is defined and all its children.  Therefore, if declared at the root, it is the document default.
Qualified scope is to say the namespace has an associated qualified name, and is used throughout the element
and all its children and referenced within those levels using the qualified name.


<?xml version="1.0" ?>
<ClassMaterial xmlns="http://www.intertech-inc.com/ns "
 xmlns:mat="http://www.intertech-inc.com/ns/classmaterial"
 xmlns:book="http://www.intertech-inc.com/ns/pubmaterial"
 level="Complete"
 pagecount="356"
 ISBN="X-XXXX-XX-X">
 <mat:Title>Complete XML</mat:Title>
 <book:Title>XML for Teens</book:Title>
 <Author>Gina Accawi</Author>
 <book:Author>544-44-0495</book:Author>
 <Chapter>
       <Title>A History of XML</Title>
       <para>Here is a history of XML</para>
       <ul xmlns="http://www.w3.org/tr/rec/rec-html40">
             <li>Here is an HTML list item</li>
             <li>We are using the HTML namespace for </li>
             <li>This section</li>
       </ul>
</Chapter>
</ClassMaterial>


By using the qualified name, we are able to sprinkle references throughout the document to the namespaces.

The namespace for HTML 4.0 is used with local scope. Here we are utilizing this namespace to use the
HTML list and list item elements. The scope of the namespace is the level at which it is declared, and the
child items of the element.  In this case, UL and LI elements.

So, namespaces address one of the concerns previously mentioned. Ambiguity and name collisions are
addressed using qualified names. There is no allowance for including multiple DTDs, even if each one is given
a unique namespace.  To solve this issue, we must look to schemas.
Table of Contents
Namespaces
Courseware
Training Resources
Tutorials
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any copying is strictly prohibited.
Services