Java Reference
In-Depth Information
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
xmlns by a colon is the namespace prefix and it has the value
http://www.wrox.com/dtds/sketches . You can use the namespace prefix to qualify names
within the namespace, and since 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, we 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 radius="15" angle="0">
<sketcher:color R="150" G="250" B="100"/>
<sketcher:position x="30" y="50"/>
</sketcher:circle>
</sketcher:sketch>
Each reference to the element name is qualified by the namespace prefix sketcher . A reference in the
same document to a <circle> element that is defined within another namespace can be qualified by
the prefix specified in the declaration for that namespace. By qualifying each element name by its
namespace prefix, we avoid any possibility of ambiguity.
A namespace has scope - a region of an XML document over which the namespace declaration is
visible. The scope of a namespace is the content of the element within which it is declared, plus all
direct or indirect child elements. The namespace declaration above applies to the <sketch> element
and all the elements within it. If you declare a namespace in the root element for a document, its scope
is the entire document.
You can declare a namespace without specifying a prefix. This namespace then becomes the default
namespace in effect for this element and its content, and unqualified element names are assumed to
belong to this namespace. Here is an example:
<sketch xmlns="http://www.wrox.com/dtds/sketches">
There is no namespace prefix specified so the colon following xmlns is omitted. This namespace
becomes the default, so we can use element and attribute names from this namespace without
qualification and they are all implicitly within the default namespace. For instance:
<sketch xmlns="http://www.wrox.com/dtds/sketches">
<circle radius="15" angle="0">
<color R="150" G="250" B="100"/>
<position x="30" y="50"/>
</circle>
</sketch>
Search WWH ::




Custom Search