4

Apparently, the namespace URL that follows xmlns in HTML and XML pages are meaningless. And all this time I though there actually were namespaces at those addresses...

When I first read/heard about namespaces, I imagined there some large files at the URLs provided that contained a list of all the valid 'names' that could be used in instances of the document. I've come to learn I imagined wrong.

But if the URL is totally useless, what exactly is the point of a namespace? How do you know if something belongs to a namespace if it doesn't actually exist anywhere? When I'm specifying a "namespace" am I actually doing anything??

It sounds to me like it's totally arbitrary in every sense of the word.

qtmfld
  • 2,916
  • 2
  • 21
  • 36
CodyBugstein
  • 21,984
  • 61
  • 207
  • 363

2 Answers2

4

The essential function of a namespace name in XML is to provide a unique identifier which allows software or people who need to process XML documents to know whether a particular element encountered in the input is, or is not, one of the elements they are responsible for processing. Suppose you are a specialized piece of software designed to process elements named 'blort' and display the results in an area of the user's display. Whenever you see an element named 'blort' in the input, you must decide whether the 'blort' element you are seeing in the input is the 'blort' element you are supposed to work on, or another element with the same name but a different structure or meaning. Without namespaces, you cannot know for certain.

This problem can be solved if:

  • Everyone who invents an XML vocabulary (and cares about avoiding name conflicts) chooses a unique name for their vocabulary. For a variety of historical, technical, and stylistic reasons, we call this name a 'namespace name'.

  • We find some way to make the full name of an XML element or attribute include the namespace name. This is what some specs call the 'expanded name': a pair consisting of the namespace name and the 'local name'.

You can now distinguish between the 'blort' in the vocabulary you are designed to handle from any other 'blort' you may find (just look at their expanded names!), as long as the third criterion is met:

  • No two inventors of XML vocabularies choose the same namespace name for their vocabularies.

The usual way to ensure the uniqueness of arbitrary identifiers is to have some sort of central registry. Central registries, however, are difficult to build and maintain, and XML namespaces use URIs as a way of piggy-backing on one of the most successful of registration authorities: the one that maintains the domain-name system at the heart of the Internet. As long as inventors of namespace names follow the basic rule of assigning namespace names only in the part of URI space they own or control, different inventors will never choose the same namespace name.

Using URIs as namespace names also has the nice property that the definer of the namespace can put human- and machine-readable documentation of the namespace at the namespace URI. Some authorities recommend this as good practice, but it is not required by the W3C's Namespaces in XML recommendation, in order to keep namespace processing as simple as possible. Most of the XML vocabularies intended for public use that I'm familiar with do provide documentation at their namespace URIs, though not all do. So you find out what belongs to a namespace by finding documentation for it, issued by the responsible entities. If they provide it at the namespace URI, it's easy; if not, it's harder; in some cases the question may not be answerable in practice. Life is hard sometimes.

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
  • Really clear explanation! I think I get it now. So when I specify an `xmlns`, nothing is actually downloaded or checked against whatever is at that URL, right? – CodyBugstein Apr 24 '13 at 21:08
  • 2
    Right (at least usually). The XML parser has no need to download anything from the namespace URI. Other forms of processing (such as validation against an XSD schema) may dereference the URI as a way of trying to find relevant declarations. – C. M. Sperberg-McQueen Apr 24 '13 at 21:29
  • question. what would happen if you instead put a GUID in the namespace? or would the XML parse explicitly be wanting a URI schemed namespace? On that note, are both https and http allowed? – Zach Smith May 04 '17 at 15:02
  • @ZachSmith, if the namespace name is not a URI, the meaning of the document is formally undefined and there are no constraints on how conforming XML-aware software may choose to handle the situation. Some may report that the namespace name is not a URI and that the document is thus not well-formed with respect to the namespaces spec. Others are likely to carry on without reporting any error. – C. M. Sperberg-McQueen Sep 16 '17 at 02:12
3

The URL (actually, an URI) is not meaningless at all, it is an unique identifier. With it, you don't need a central registry of all possible namespaces. Just create an URL in a domain you control and you can be sure you have an unique identifier. (URIs are not only a way to retrieve resources on the Web, they are also identifiers.)

It is considered good practice for namespace identifiers to be actual URL with some documentation available if you give this URL to a Web browser. But it is not mandatory.

bortzmeyer
  • 34,164
  • 12
  • 67
  • 91
  • OK interesting... but that only answers half my question. How can you actually "inherit" tags/names from other namespaces if they are not actually stored anywhere? – CodyBugstein Apr 24 '13 at 21:00
  • In any system that provides for inheritance or reuse of things in other namespaces, there is likely to be some written form of declaration for the names in question. XSD schema documents and RDF schema documents are two obvious examples. – C. M. Sperberg-McQueen Apr 24 '13 at 21:24
  • @lmray Your confusion here is a conflation of DTDs (which specify the available tags and attributes, and are located via a document's `DOCTYPE` declaration) and namespaces (which are purely a prefix to create unique tag names, and are specified via `xmlns` attributes). – Alf Eaton Apr 04 '16 at 20:16