What's in a namespace?
By Ed Tittel
Occasionally, it's interesting to hang back on the fringes of the XML community and watch certain heated technical discussions from a distance. Lately, I've been witnessing a tendency for people to talk about DTDs, schemas, and namespaces as part and parcel of the technology that allows one or more external sources of markup definitions to appear within a single XML document. While it's true that DTDs and schemas are mostly equivalent in terms of the functionality that they deliver, and that either of these types of documents can define markup elements, content models, attributes, and so forth, the same cannot be said for namespaces.
Namespaces have one major reason for existence--namely, to provide an unambiguous way of identifying elements or attributes that may have the same name, but that come from entirely different contexts. In other words, the element <name> may exist in two or more DTDs or schemas, and what namespaces do is to provide a way to keep those potential collisions from causing problems within XML documents where multiple DTDs or schemas may be active.
When a namespace is declared, in fact, that declaration takes the basic form xmlns="URI" (here, URI stands for Uniform Resource Identifier; although it looks like a URL, it's only job is to provide a unique name to be associated with the XML namespace that is guaranteed to be different from other URIs. (This explains why domain names with extensions are popular as URIs since most developers justifiably believe that it's unlikely that somebody else will re-use their domain name and extension as a URI for some different namespace. But I digress.) When it's necessary to distinquish among multiple namespaces, that declaration can be altered as follows: xmlns:nsid="URI", where nsid is a name space identifier created for use in the current document designed to distinguish one reference from another.
Returning to our earlier example, where the name element exists in two separate DTDs, let's assume that their namespace declarations looked like this:
Although there's no requirement that URIs be especially informative, the intent here is to illustrate that the <name> element for the db namespace comes from a schema for order records in a database management system, while the <name> element for the email namespace comes from an addressbook record format in a messaging system.
Then, within the scope of the namespace declaration (which XML permits to occur at the root level, so it applies to the entire document, or within child elements, so that it applies only within the scope of that particular element and all its children), whenever the database variety of name is needed, it would appear as <db:name>; likewise, when the e-mail variety of name is needed, it would appear as <email:name>.If you get this, you get namespaces! But alas, DTDs and schemas are another story entirely...
Ed Tittel is a principal at LANWrights, Inc., a wholly owned subsidiary of LeapIt.com. LANWrights offers training, writing, and consulting services on Internet, networking, and Web topics (including XML and XHTML), plus various IT certifications (Microsoft, Sun/Java, and Prosoft/CIW).