Java Reference
In-Depth Information
separate namespaces. An XML namespace is just a collection of element and attribute names that is identi-
fied 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 because each name is
notionally qualified by the unique URI for the namespace that contains it.
I say “notionally qualified" because you don't usually qualify element and attribute names using the URI
directly, although you could. Normally, in the interests of not making the markup overly verbose, you use an-
other 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
www.wrox.com/Toys
and a namespace prefix,
toys,
that contains
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 I said earlier
in the chapter that 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 in the namespace. This is not required by the namespace specification. The URI is just a unique iden-
tifier 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 separ-
ate 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">
A namespace declaration uses a special reserved attribute name,
xmlns
, within an element, and in this
instance the namespace applies to the
<sketch>
element. The name
sketcher
that is separated from
xm-
lns
by a colon is the namespace prefix, and it has the value
www.wrox.com/dtds/sketches
.
You can use the
namespace prefix to qualify names within the namespace, and because this maps to the URI, the URI is
effectively the qualifier for the name. The URL that I've given here is hypothetical — it doesn't actually
exist, but it could. The sole purpose of the URI identifying the namespace is to ensure that names within
the namespace are unique, so it doesn't matter whether it exists or not. You can add as many namespace
declarations within an element as you want, and each namespace declared in an element is available within
that element and its content.
With the namespace declared with the
sketcher
prefix, you can use the
<circle>
element that is defined
in the
sketcher
namespace like this:
<sketcher:sketch xmlns:sketcher="http://www.wrox.com/dtds/sketches">
<sketcher:circle angle="0" diameter="30">
<sketcher:color R="150" G="250" B="100"/>
<sketcher:position x="30" y="50"/>
<sketcher:bounds x="30" y="50"
width="32" height="32">
</sketcher:bounds>