Naming Namespaces

The whole point of namespaces is that they provide a means of establishing unique identifiers for elements and attributes. It is therefore imperative that each and every namespace have a unique name. Obviously, there would be no way to enforce this rule if everyone was allowed to make up their own names out of thin air, so a clever naming scheme was established that tied namespaces to URIs (Uniform Resource Identifiers). URIs usually reference physical resources on the Internet and are guaranteed to be unique. So, a namespace is essentially the name of a URI. For example, my web site is located at http://www.xyz.com. To help guarantee name uniqueness in any XML documents that I create, I could associate the documents with my namespace:

<mediacollection xmlns:mov="http://www.xyz.com/ns/movies">

The ns in the namespace name http://www.xyz.com/ns/movies stands for "namespace" and is often used in URL namespace names. It isn't a necessity but it's not a bad idea in terms of being able to quickly identify namespaces. If you don't want to use a URI as the basis for a namespace name, you could also use the URN (Universal Resource Name) of a web resource to guarantee uniqueness. URNs are slightly different from URLs and define a unique location-independent name for a resource that maps to one or more URLs. Following is an example of using a URN to specify a namespace for my web site:

<mediacollection xmlns:mov="urn:xyz.com:ns:movies">

Making Sense of URLs, URNs, and URIs

There is often confusion among XML developers regarding the relationship between URLs, URNs, and URIs. Perhaps the most important distinction to make is that URIs encompass both URLs and URNs. URNs differ from URLs in that URLs describe the physical location of a particular resource, whereas URNs define a unique location-independent name for a resource that maps to one or more URLs. An easy way to distinguish between URLs and URNs is to examine their names: URLs all begin with an Internet service prefix such as ftp:, http:, and so on, whereas URNs typically begin with the urn: prefix.

Keep in mind that a namespace doesn't actually point to a physical resource, even if its URI does. In other words, the only reason namespaces are named after URIs is because URIs are guaranteed to be uniquethey could just as easily be named after social security numbers. This means that within a domain name you can create URIs that don't actually reference physical resources. So, although there may not be a directory named pets on my web server, I can still use a URI named http://www.xyz.com/ns/pets to name a namespace. The significance is that the xyz.com domain name is mine and is therefore guaranteed to be unique. This is important because it allows you to organize XML documents based upon their respective namespaces while guaranteeing uniqueness among the namespace names.