Namespaces

DTD and XML

You can use namespaces in your XML document. In writing an XML document you can use the namespace setup by the writer of the DTD you are using.
If DTDs supported real namespaces, in the XML document you would set up a local name to use for each namespace. You cannot choose a local name for the namespace, so you must use the name set up by the author of the DTD.

In theory, there is a way to fake a limited way to use local names with a DTD. It is dificult to set up, and could sometimes be impossible to use. We will ignore that approach.

Selecting a name

If you were to set up a namespace, the first thing you need to do is select a name for your namespace. It might be good to select a long name, so it will not be the same as the name someone else is using. You might select:
mary-mary-quite-contrary-how-does-your-garden-grow
Of course, perhaps someone else likes nursery rimes, and might select the same name.

I know a better way to select a name, so it will be unique. I will select the name:
http://voyager.deanza.edu/~oldham/mary
That is a good choice.   http://voyager.deanza.edu   is a web address that is registered by De Anza, so it will not be used by someone at West Valley College.   ~oldham   is my directory name on voyager, so it will not be used by another instructor or student. I just made up the name   mary
The name   mary   looks like it is a directory in my web site; actually there is no such directory, I just made the name up.

A real address is called a Uniform Resource Locator(URL). A name that looks like a URL is called a Uniform Resource Identifier(URI). A URI might be a real URL address, or it might not be. I do not have a directory mary, so the name I selected is just a URI.

If you look at the source for this document, you can see a namespace specified in the   <html>   opening tag. The namespace is   http://www.w3.org/1999/xhtml   That is the namespace for XHTML. This page is an XML document, using the namespace provided by the w3.org organization for XHTML documents.

Default namespace

The simplest use of a namespace is to specify a default namespace for everything in the document. To do this, put an   xmlns   attribute in the root element of the document. The value of the attribute is the namespace you wish to use. If you do this, the namespace will be used for all elements and attributes in your XML document.

The root element for an XHTML 1.0 page is always the   html   element. In the source for his page, in the   html   element, you see the attribute:
xmlns="http://www.w3.org/1999/xhtml"
The attribute is   xmlns   and the value is the namespace for XHTML:   http://www.w3.org/1999/xhtml

Changing the namespace

You may wish to provide a default namespace for the whole document in the root element, but change to a different namespace for one of the elements within the document.
For example, you might use the XHTML namespace as the default, so you have an XHTML page, but might wish to include a   math   element.
There is a mathML specification to represent mathematical expressions in XML.
You can specify the namespace for mathML in the   math   element. Then everything within the   math   element container will use the mathML namespace.
See the example link to the left.