So I've been reading about namespace on w3schools and I understand they are to uniquely identify an element. But what is the point of having namespaces if you have to have prefixes along side them. Are you always required to have prefixes with namespaces? If not are you always required to have namespaces with prefixes? If so why?

For example:

<root xmlns:h="http://www.w3.org/TR/html4/" xmlns:f="http://www.w3schools.com/furniture">


<f:name>African Coffee Table</f:name>

Doesn't f and h uniquely identify the different element types?

Thanks for your help. I'm really at a loss at understanding why namespaces exists.

  • 1
    Duplicate; So you can include data specified in another namespace within that document, and so any documents that might consume your document can do the same. This is answered in more detail here what is meant by namespaced content and what advantages does it have – JustinC Jun 19 '13 at 7:43
  • Right, but wouldn't prefixes already take care of that? The prefix f or h might specify its on another "namespace". What's the point of given f or h a namespace when they will always be unique in the document? Why can't I just the following without declaring a namespace: <h:table> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> – Uriel Katz Jun 19 '13 at 7:47
  • Do you mean: What's the point of associating a URI with a namespace? Or: What's the point of explicitly declaring namespaces? – MetaFight Jun 19 '13 at 7:59
  • I mean what's the point of associating a URI with a namespace. From what I read is so that they are unique. But if you can easily have the same prefixes, then doesn't this defeat the purpose of having a namespace be a URI? – Uriel Katz Jun 19 '13 at 8:11

An XML namespace is supposed to have a reasonable chance of being universally unique. A prefix, especially a single-letter one, is almost certain not to be. A namespace declaration creates a mapping from an otherwise meaningless prefix to a stable identifier that software can rely on to identify the schemas of XML elements.

You're right: within a single document, an author can make sure that different prefixes are used for different groups of elements. But without tying those prefixes to unique namespaces, how do you accomplish any of the following:

  • Sending that XML file to an organisation that uses different prefixes for the same groups of elements, and expecting it to be interpreted correctly by (hypothetical) software that only uses prefixes?
  • Any automated processing of XML documents at all? Even something as fundamental as schema validation: how does your HTML parser know to associate the prefix h with the collection of HTML elements, and therefore knows how to render h:table?

There is no central database of prefixes where h is forever and all time associated with HTML elements - that's the purpose of the namespace declaration mapping the prefix to the namespace within the scope of the document.

Update - clarification of terminology

  • Namespace URI, also just called namespace: A stable name uniquely identifying a collection of XML elements and attributes.
    • This is just an identifier, chosen to be unique.
    • Although it is often a URL, whatever document might be available at this URL is irrelevant and is not consulted during XML processing. Browsing to the URL does not have to produce any valid content, although sometimes the company which produced the XML schema, and chose its namespace URI to have a domain which they control, chooses to publish some information about the schema at that address.
    • Note that this means you cannot expect to download a schema given a namespace URI. You will need to get hold of schemas using some other mechanism, probably documented by whoever produced the schema.
  • Prefix: a document-local representation of a namespace URI within an XML document. Any arbitrary prefix can be mapped to a namespace using xmlns. If you really wanted to, you could have both xmlns:f="http://www.w3schools.com/furniture" and xmlns:argos="http://www.w3schools.com/furniture", and then use the prefixes interchangeably: <f:table> or <argos:table>. The parser doesn't care whether the prefix is f or argos - it only cares that both of these table elements belong to the namespace http://www.w3schools.com/furniture. Further, the URL http://www.w3schools.com/furniture does not have to be a valid document. When used as an XML namespace URI, its only function is to be a unique identifier. If the parser had available a schema whose targetNamespace was http://www.w3schools.com/furniture, it could validate the table elements and their contents using that schema.
| improve this answer | |
  • So the namespace can associate an an element with a specific attribute as well? – Uriel Katz Jun 19 '13 at 8:24
  • I'm not sure I understand what you mean. If you mean that: once an element is identified as belonging in a particular namespace, can the valid attributes of that element be determined? Then the answer is yes; this falls under schema validation. For example, the parser sees f:table, and it knows (from the xmlns:f declaration) that f: means the namespace http://www.w3schools.com/furniture. If it's validating and has the schema for http://www.w3schools.com/furniture available, it can determine which other elements and attributes are required and/or legal under f:table, e.g. f:width. – shambulator Jun 19 '13 at 8:34
  • Yes, but couldn't you link the schema to f: without having to have a namespace. What specifies the schema, is the schema information inside the URI, for example will w3schools.com/furniture contain a page with the schema? Or is the schema linked to the namespace URI inside the xml, if so couldn't it be linked to the prefix? Is this so that a parser has unique names to attach to schemas? – Uriel Katz Jun 19 '13 at 8:51
  • @UrielKatz See my update on terminology. – shambulator Jun 19 '13 at 9:11

The point of namespaces in XML is not just to make it clear for the human reader that <h:table> and <f:table> are different things, but also to tell a (validating) XML parser how they should be parsed (for example, which attributes and sub-elements are valid).

The prefixes are only needed if there can be confusion about which namespace the element comes from. For example, this is also valid, because all elements come from the same namespace:

<root xmlns="http://www.w3schools.com/furniture">
    <name>African Coffee Table</name>

The prefixes are mostly a shorthand to indicate a particular namespace, but they don't have any meaning of their own. It is the namespace URI that tells the XML parser how to interpret the XML elements that are part of the namespace.

| improve this answer | |
  • So can a namespace URI tell a parser attributes of an element? – Uriel Katz Jun 19 '13 at 8:27
  • @UrielKatz: Yes. The namespace URI w3.org/1999/xhtml, for example, specifies exactly what a valid XHTML document looks like and how it should be interpreted. – Bart van Ingen Schenau Jun 19 '13 at 8:42
  • 2
    @UrielKatz not directly. The URL should link to a specification of the namespace, but this specification is not required to be machine-readable (like in XSD format). That means that an XML validator is responsible for providing a schema for each namespace URL it knows. – Philipp Jun 19 '13 at 8:48

In your example, h is a shortcut for the fully qualified namespace http://www.w3.org/TR/html4. Now, imagine your document is processed by an XSLT stylesheet that you didn't write. In this stylesheet, the same http://www.w3.org/TR/html4 namespace is associated with anoter prefix, html. Without the namespace declaration, you wouldn't know that html and h refer to the same namespace.

| improve this answer | |
  • But isn't the point of using URI's as namespaces to have a unique name for every namespace? If prefixes can have the same name pointing to a namespace doesn't this defeat the purpose since you could run into name conflicts anyhow? – Uriel Katz Jun 19 '13 at 8:08
  • But you would'nt prefix every element with the fully qualified URI, would you? Prefix is a local symbol in your document that refers to the URI. Even if you have two documents using the same prefix p, what matters is which URI they respectively point to. Of course, you cannot merely copy/paste the content of one document into another. You must dereference prefixes to fully qualify all elements, with respect to each document's namespace declaration. – coredump Jun 19 '13 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.