Namespaces can be used to qualify names. If an XML document was created from multiple sources, then some of the elements may have the same names. If the XML document will be used in a basically self-contained environment, then a name space will not be necessary. See also W3C for Namespaces in XML [§].

There are four scopes for namespaces:

• Document. A namespace can be made for the XML document by including a processing instruction tag prior to the first XML data node. All elements in the document that belong to that namespace would have to have each element name prefixed. EG: <ns1:child>. It is arguable that an explicit namespace should be used on the root element instead of a document namespace.
• Explicit. A namespace can be made for a subset of elements within an XML document. All elements in that subset that belong to that namespace would have to have each element name prefixed. EG: <ns2:child>.
• Default. Just like explicit except that the prefix is removed. EG: <child>.
• Local. Just remove the namespace prefix if the element is not in a default namespace. EG: <child>.
<?xml version="1.0"?>
<?xml:namespace ns="http://www.yoursite.com/ns1" prefix="ns1"?>
<ns1:myrootnode>
<ns1:child>
This child belongs to the document namespace
</ns1:child>
<ns2:child xmlns:ns2="http://www.yoursite.com/ns2">
This child does not belong to the document namespace,
it belongs to an explicit namespace.
<ns1:gChild>
This gchild belongs to the doc ns.
<ns1:gChild>
<ns2:gChild>
This gchild belongs to the explicit ns.
<ns2:gChild>
<gChild>
This gchild is of local scope.
<gChild>
</ns2:child>
<child xmlns="http://www.yoursite.com/ns3">
This child does not belong to the document namespace,
it belongs to a default namespace.
<ns1:gChild>
This gchild belongs to the doc ns.
<ns1:gChild>
<gChild>
This gchild belongs to the default local scope.
It does not belong to a local scope.
<gChild>
<child>
<child>
This child does not belong to any namespace,
it is of local scope.
</child>
</ns1:myrootnode>

Note that the URI that a namespace points to does not necessarily have to point to a real document such as an XML Schema. All that counts is that the namespace is is unique on the Internet.

Page Modified: (Hand noted: ) (Auto noted: )