Home TOC |
![]() ![]() ![]() |
Using Namespaces
As you saw previously, one way or another it is necessary to resolve the conflict between the
title
element defined inslideshow.dtd
and the one defined inxhtml.dtd
. In the previous exercise, you hyphenated the name in order to put it into a different "namespace". In this section, you'll see how to use the XML namespace standard to do the same thing without renaming the element.
Note: At this point in time, the Java XML parsers do not support namespaces. This section is for information only.
The primary goal of the namespace specification is to let the document author tell the parser which DTD to use when parsing a given element. The parser can then consult the appropriate DTD for an element definition. Of course, it is also important to keep the parser from aborting when a "duplicate" definition is found, and yet still generate an error if the document references an element like
title
without qualifying it (identifying the DTD to use for the definition).
Note: Namespaces apply to attributes as well as to elements. In this section, we consider only elements. For more information on attributes, consult the namespace specification athttp://www.w3.org/TR/REC-xml-names/
.
Defining a Namespace
To define a namespace that an element belongs to, it is necessary to add an attribute to the element's definition, where the attribute name is
xmlns
("xml namespace"). For example, you could do that inslideshow.dtd
by adding an entry like the following in thetitle
element's attribute-list definition:<!ELEMENT title (%inline;)*> <!ATTLIST title xmlns CDATA #FIXED "http://www.example.com/slideshow" >Declaring the attribute as
FIXED
has several important features:
- It prevents the document from specifying any non-matching value for the
xmlns
attribute (as described in Defining Attributes in the DTD).- The element defined in this DTD is made unique (because the parser understands the
xmlns
attribute), so it does not conflict with an element that has the same name in another DTD. That allows multiple DTDs to use the same element name without generating a parser error.- When a document specifies the
xmlns
attribute for a tag, the document selects the element definition with a matching attribute.To be thorough, every element name in your DTD would get the exact same attribute, with the same value. (Here, though, we're only concerned about the
title
element.) Note, too, that you are using aCDATA
string to supply the URI. In this case, we've specified an URL. But you could also specify a URN, possibly by specifying a prefix likeurn:
instead ofhttp:
. (URNs are currently being researched. They're not seeing a lot of action at the moment, but that could change in the future.)Referencing a Namespace
When a document uses an element name that exists in only one of the
.dtd
files it references, the name does not need to be qualified. But when an element name that has multiple definitions is used, some sort of qualification is a necessity.
Note: In point of fact, an element name is always qualified by it's default namespace, as defined by name of the DTD file it resides in. As long as there as is only one definition for the name, the qualification is implicit.
You qualify a reference to an element name by specifying the
xmlns
attribute, as shown here:<title xmlns="http://www.example.com/slideshow"> Overview </title>The specified namespace applies to that element, and to any elements contained within it.
Defining a Namespace Prefix
When you only need one namespace reference, it's not such a big deal. But when you need to make the same reference several times, adding
xmlns
attributes becomes unwieldy. It also makes it harder to change the name of the namespace at a later date.The alternative is to define a namespace prefix, which as simple as specifying xmlns, a colon (:) and the prefix name before the attribute value, as shown here:
<sl:slideshow xmlns:SL='http:/www.example.com/slideshow' ...> ... </SL:slideshow>This definition sets up
SL
as a prefix that can be used to qualify the current element name and any element within it. Since the prefix can be used on any of the contained elements, it makes the most sense to define it on the XML document's root element, as shown here.
Note: The namespace URI can contain characters which are not valid in an XML name, so it cannot be used as a prefix directly. The prefix definition associates an XML name with the URI, which allows the prefix name to be used instead. It also makes it easier to change references to the URI in the future.
When the prefix is used to qualify an element name, the end-tag also includes the prefix, as highlighted here:
<SL:slideshow xmlns:SL='http:/www.example.com/slideshow' ...> ... <slide> <SL:title>Overview<SL:title> </slide> ... </SL:slideshow>Finally, note that multiple prefixes can be defined in the same element, as shown here:
<SL:slideshow xmlns:SL='http:/www.example.com/slideshow' xmlns:xhtml='urn:...'> ... </SL:slideshow>With this kind of arrangement, all of the prefix definitions are together in one place, and you can use them anywhere they are needed in the document. This example also suggests the use of URN to define the
xhtml
prefix, instead of an URL. That definition would conceivably allow the app to reference a local copy of the XHTML DTD or some mirrored version, with a potentially beneficial impact on performance.
Home TOC |
![]() ![]() ![]() |