XML
namespaces
XML namespaces provide a way to create globally unique names to
distinguish between elements and attributes with the same name but of
different terminologies. For example, in an XML invoice document for a
bookstore, the name “date” could be used by accounting for the date of the
order and by order fulfillment for the date of publication.
An XML namespace is identified by a Uniform Resource Identifier
(URI), a short string that uniquely identifies resources on the Web. The
elements and attributes in each namespace can be uniquely identified by
prefixing the element or attribute name (the local name) with the URI of
the namespace.
Associating a prefix with a
namespace
You declare an XML namespace using xmlns as part of a namespace
declaration attribute. With the namespace declaration attribute, you can
associate a prefix with the namespace.
For example, the following namespace declaration attribute declares
the http://www.pre.com namespace and associates the prefix pre with this
namespace:
|
1 |
xmlns:pre="http://www.pre.com" |
Default XML namespace
If an XML namespace declaration does not specify a prefix, the
namespace becomes a default XML namespace. For example, the following
element, digicom, declares the namespace
http://www.digital_software.com:
|
1 |
<digicom xmlns="http://www.digital_software.com" /> |
The namespace http://www.digital_software.com is the in-scope
default namespace for the element digicom and any child elements that
digicom might contain. The child elements of digicom will automatically be
in this namespace.
The NONAMESPACE declaration
The following namespace declaration is known as the NONAMESPACE
declaration:
|
1 |
xmlns="" |
The containing element and its child elements are declared to be in
no namespace. An element that is in the NONAMESPACE namespace has its
namespace prefix and URI set to empty strings.
Initial state
When a PBDOM_ELEMENT or a PBDOM_ATTRIBUTE is first created, it has
no name, and the namespace information is by default set to the
NONAMESPACE namespace (that is, its namespace prefix and URI are both
empty strings). The SetName method is used to set the local name and the
SetNamespace method is used to set the namespace prefix and URI.
The name is required
The name is a required property of a PBDOM_ELEMENT and
PBDOM_ATTRIBUTE, but the namespace information is not.
Retrieving from a parsed
document
If a PBDOM_ELEMENT or PBDOM_ATTRIBUTE is retrieved programmatically
from a parsed document, then its name and namespace information are
inherited from the Element or Attribute contained in the parsed document.
However, even after parsing, the name and namespace information of the
PBDOM_ELEMENT and PBDOM_ATTRIBUTE can be further modified with the
SetName and SetNamespace methods.
The name and namespace information are stored separately internally.
Changing the name of a PBDOM_ELEMENT or PBDOM_ATTRIBUTE does not affect
its namespace information, and changing its namespace information has no
effect on its name.