Xatapult's XML Blog

31/03/2010

Naming namespaces

Filed under: Opinion,Standards and guidelines — xatapult @ 05:55

Namespaces are inevitable when you work with XML. In the beginning they seem awkward and unnecessary, but after a while you find out that they have a purpose and are actually very useful.

When you start designing your own XML, sooner or later you’ll want your own namespace too. And there an important question arises: How to call your namespace, what name should your namespace have?

There are customs and, as usual, many opinions about this. Let me add mine…

Namespace basics

Before going to the actual subject, let’s do a quick recap on namespaces.

The main reason for namespaces is to differentiate between several types of XML in the same document. It’s handy for avoiding element and attribute name clashes and makes their meaning more clear. Here is a classic example: Assume you’re in an XML document about a book and the following element appears:

<name>Johnny Johnson</name>

Is this the name of a book? Or an author? Or a publisher? Adding a namespace clarifies this:

<name xmlns="authornamesnamespace">Johnny Johnson</name>

Another classic example is the envelope-content construction as used, for instance, in SOAP messaging:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:msg="mymessagenamespace">
  <soapenv:Header>
   […]
  </soapenv:Header>
  <soapenv:Body>
    <msg:MyMessage>Hello world!</msg:MyMessage>
  </soapenv:Body>
</soapenv:Envelope>

Prefix and name

Never confuse the namespace prefix in an XML document with the namespace name. In the example above soapenv is the namespace prefix for the namespace with the name schemas.xmlsoap.org/soap/envelope/.

Changing the prefix is no problem. You can use anything you like (within the rules of a no-colon-name, an NCName). For instance, this means exactly the same:

<bettyboop:Envelope xmlns:bettyboop="http://schemas.xmlsoap.org/soap/envelope/" […]

However, changing the namespace name is not allowed. Your SOAP software will no longer recognize your messages if you do something like:

<soapenv:Envelope xmlns:soapenv="somethingilikemore"

It expects the envelope to be in the right namespace and so it should be!

So, what you choose for a prefix is not important (at least not for the meaning of your document, it can be important for its legibility), but choosing a correct namespace name is!

What’s in a name?

As long as a namespace name complies with the syntactic rules for a URI, you can name a namespace anything you like. It’s just a string of characters and nothing stops you from calling your namespace mumbledumbledoozydoe.

However, the reason domain names (like xmlsoap.org) are often used inside namespace names, is that it makes the namespace globally unique. This is piggy-backing on the existing mechanism for the registration of domain names: This ensures that nobody in the world can register a namespace already belonging to somebody else. Ergo, a domain name is globally unique and so a namespace using the domain name is globally unique also.

So its wise to use your domain name inside a namespace name when your XML is send out into the big wide world.

Naming conventions for namespaces

There are several conventions for naming namespaces, but the most widely used is one that looks like a web address or URL. Examples are everywhere:

http://schemas.xmlsoap.org/soap/envelope/
http://www.w3.org/1999/XSL/Transform
http://www.w3.org/1999/xhtml
http://www.w3.org/2001/XMLSchema

I know its fighting windmills, but I am very strongly opposed to this convention. Why? Well, because we are using things that look like web addresses for things that aren’t web addresses at all. A namespace is not a web address, it’s a namespace!

What do you think when you see a namespace formatted like this for the first time? Exactly, that it is a URL. It creates a mental barrier understanding what this is all about. I must admit that I have suffered from this myself. But I also train people in XML basics and the concept of namespaces is very hard to explain because of this URL convention.

So IMHO this is a silly convention, unnecessary complicating a very simple concept. We should stop using it.

An alternative

Ok, but what should we use instead? A good naming convention for namespaces must have two characteristics:

  • It must be clear from the start that it is a namespace and not something else.
  • It must be easy to make a namespace globally unique

To accomplish this, I propose the following convention (for which the general structure was stolen from the standard for a URN):

ns:{domain name}:{further information}

For instance:

ns:xatapult.nl:generic
ns:myowndomain.com:storage.text.specials
ns:html8.org:linking

There: simple, straight and it cannot be mistaken for something else.

 

Advertisements

1 Comment »

  1. It’s a good idea not to use URL like namespaces. I has confused me – but I’m quite new to XSLT. But if you want a new way, then why not use a convention that for example Java and C# have been using successfully for years: com.business.yourapplication.logic. Simple string, just dots as separation (although as in XSLT the separation is meaningless, it’s just there for easy reading).

    Comment by Christiaan Knaap — 08/08/2012 @ 06:03 | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: