Java Reference
In-Depth Information
We saw earlier that an XML document can only have one DOCTYPE declaration. This can identify an
external DTD by a URI or include explicit markup declarations, or it may do both. What happens if we
want to combine two or more XML documents that each have their own DTD into a single document?
The short answer is we can't - not easily anyway. Since the DTD for each document will have been
defined without regard for the other, element name collisions are a real possibility. It may be impossible
to differentiate between different elements that share a common name and in this case major revisions
of the documents' contents as well as a new DTD will be necessary to deal with this. It won't be easy.
XML namespaces are intended to help deal with this problem. They enable names used in markup to
be qualified so that you can make duplicate names used in different markup unique by putting them in
separate namespaces. An XML namespace is just a collection of element and attribute names that is
identified by a URI. Each name in an XML namespace is qualified by the URI that identifies the
namespace. Thus different XML namespaces may contain common names without causing confusion
since each name is notionally qualified by the unique URI for the namespace that contains it.
I say 'notionally qualified' because you don't actually qualify names using the URI directly. You use
another name called a namespace prefix whose value is the URI for the namespace. For example, I
could have a namespace that is identified by the URI http://www.wrox.com/Toys and a
namespace prefix, toys , that contains a declaration for the name rubber _ duck . I could have a second
namespace with the URI http://www.wrox.com/BathAccessories and the namespace prefix
BathAccessories that also defines the name rubber _ duck .
The rubber _ duck name from the first namespace is referred to as Toys:rubber _ duck and that from
the second namespace is BathAccessories:rubber _ duck so there is no possibility of confusing
them. The colon is used in the qualified name to separate the namespace prefix from the local name,
which is why we said earlier you should avoid the use of colons in ordinary XML names.
Let's come back to the confusing aspects of namespaces for a moment. There is a temptation to imagine
that the URI that identifies an XML namespace also identifies a document somewhere that specifies the
names that are in the namespace. This is not required by the namespace specification. The URI is just a
unique identifier for the namespace and a unique qualifier for a set of names. It does not necessarily
have any other purpose, or even have to refer to a real document. It only needs to be unique. The
definition of how names within a given namespace relate to one another and the rules for markup that
uses them is an entirely separate question. This may be provided by a DTD or some other mechanism
such as an XML Schema.
Namespace Declarations
A namespace is associated with a particular element in a document, which of course can be, but does
not have to be, the root element. A typical namespace declaration in an XML document looks like this:
<sketcher:sketch xmlns:sketcher="http://www.wrox.com/dtds/sketches">
Search WWH ::




Custom Search