- Index of parts:
- WHY do we need namespaces?
- What? (the heck do you mean)
- What is a namespace?
- How do I use a namespace?
- Multiple Namespaces
- The Default namespace
- Attributes and Namespace
- What do I put at the end a namespace URI?
WHY do we need namespaces?
- There are really two fundamental needs for namespaces:
- To disambiguate between two elements that happen to share the name name
- To group elements relating to a common idea together
OK, so these statements are a bit vague - lets give some examples here:
To disambiguate between elements that happen to share the same name:
- In (x)html there is a table element. There is also an element of the same name in XSL-FO.
- a, title and style are all elements in both (x)html and SVG.
So: how do you tell an SVG title from an (x)html one?
To group elements relating to a common idea together:
- In (x)html, the table, style and a elements have specific rules on what is required, and what may (& may not) be included. This should all be done in the same place.
My own XML-based data may have validating rules, and I will want to define those rules in the same place, and differentiate these particular rules from any other rule-set I (or someone else) define.
What is a namespace?
A namespace is a unique URI (Uniform Resource Locator)
The advantage of this format is that anyone who is transmitting XML can be assumed to have access to a domain name (the bit after the http://, but before the next /.
It is bad form to piggy-back on someone elses domain (especially is they don't know you're doing it!)
- In an XML document, the URI is assocciated with a prefix, and this prefix is used with each element to indicate which namespace the element belongs to. eg:
- The terms are:
- The part before the colon is the prefix
- The part after the colon is the Local part
- The a prefixed element is a qualified name
- The an un-prefixed element is an unqualified name
How do I use a namespace?
- To use a namespace, you first assocciate the URI with a namespace:
- <foo:tag xmlns:foo="http://me.com/namespaces/foofoo">.
This defines foo as the prefix for the namespace for the element tag.
The attribute prefixed with xmlns is like a command to say link the following letters to a URI. As no well-formed document can contain two identical attributes, the bit after the colon stops the same prefix being defined twice at the same time.
Here is an example:
Defining one prefix for a
<foo:tag xmlns:foo="http://me.com/namespaces/foofoo"> <foo:head> <foo:title>An example document</foo:title> </foo:head> <foo:body> <foo:e1>a simple document</foo:e1> <foo:e2> Another element </foo:e2> </foo:body> </foo:tag>
For all elements within <foo:tag>, the namespace prefix foo is assocciated with the namespace URI http://me.com/namespaces/foofoo.
Different prefix, same namespace:
It is, therefor, possible for different prefixes to actually refer to the name namespace:
Defineing more than one prefix of the
same namespace :
<tag> <foo:head xmlns:foo="http://me.com/namespaces/foofoo"> <foo:title>An example document</foo:title> </foo:head> <bar:body xmlns:bar="http://me.com/namespaces/foofoo"> <bar:e1>a simple document</bar:e1> <bar:e2> Another element </bar:e2> </bar:body> <tag>
Same prefix, different namespaces:
It is also possible for the same prefix refer to different namespaces depending on the context
The same prefix, different
<myns:html xmlns:myns="http://www.w3c.org/1999/xhtml"> <myns:head> <myns:title>A really bad idea</myns:title> </myns:head> <myns:body> <myns:h1>A really bad idea</myns:h1> <myns:pre> <myns:pre xmlns:myns="http://my.com/namespaces/test-data"> <myns:table> <myns:data> Hello World </myns:data> </myns:table> </myns:pre> </myns:pre> </myns:body>
|NOTE: THIS IS NOT A GOOD IDEA!|
If you are using namespaces, you will almost certainly need to use several namespaces at once, so how do you declare more than one namespace at a time?
What you do is use more than one xmlns declaration:
Defining more than one namespace:
<foo:tag xmlns:foo="http://me.com/namespaces/foofoo" xmlns:bar="http://me.com/namespaces/foobar" > <foo:head> <foo:title>An example document</foo:title> </foo:head> <bar:body> <bar:e1>a simple document</bar:e1> <bar:e2> Another element </bar:e2> </bar:body> </foo:tag>
The Default namespace
Question: If you use any namespaces, do all elements have to exist in a namespace?
Answer: Yes, but this may not be a problem:
It is permissable to define a namespace that is associated with no prefix, ie with unqualified names.
- This is of particular importance for xhtml, as one of the requirements is that xhtml does not break HTML, and HTML does not understand prefixes!
- To define the default namespace, simply allocate an xmlns with no prefix:
- <xhtml xmlns="http://www.w3c.org/1999/xhtml">
Defining a default namespace:
<html xmlns="http://www.w3c.org/1999/xhtml" xmlns:bar="http://me.com/namespaces/foobar" > <head> <title>An example document</title> </head> <body> <bar:e1>a simple document</bar:e1> <bar:e2> Another element </bar:e2> </body> </html>
Attributes and Namespace
For any specific element, an attribute may only exist once. This makes attributes slightly different to elements.
Attributes may be placed in a specific namespace ( <.... myns:myattib="foo" ...>), or the may be left unqualified.
The normal "rule" for Attributes is to put them in a namespace only if they are defined in a particular namespace (such as xlink or rdf attributes).
Attributes that have no namespace prefix are not in any namespace. Note: this is not the same as being the in the default namespace.
Attributes in namespaces only becomes important if you require the document to conform to a DTD or Schema - and the schemata defines the attribute as being qualified.
What do I put at the end a namespace URI?
Ok, so this isn't really helpful. The problem here is that humans see a URL, so they want to point their web-browser at it and see what they get. This is purely a human thing, and is a consiquence of the choice of standardising on URIs for namespaces.
- To quote Claude L. Bullard (from the XML-Dev email list):
- The flaw is the conflation of name, location and identity but that flaw is the basic feature by which the WWW runs so we are stuck there. All the handwaving about URN/URI/URL doesn't avoid the simple fact that if one puts http:// anywhere in browser display space, the system colors it blue and puts up a finger.
- The monkey expects a resource and when it doesn't get one, it shocks the monkey. Monkeys don't read specs to find out why they should be shocked. They turn red and put up a finger.
- (Thanks to Elliotte Rusty Harold for finding it)
What many people do is place an document there, describing the namespace
A new idea emerging from XML-dev is RDDL (the Resource Directory Description Language).
[an error occurred while processing this directive]