An Introduction to Namespaces#

Parts of the Qt XML module documentation assume that you are familiar with XML namespaces. Here we present a brief introduction; skip to Qt XML documentation conventions if you already know this material.

Namespaces are a concept introduced into XML to allow a more modular design. With their help data processing software can easily resolve naming conflicts in XML documents.

Consider the following example:

<document>
<book>
  <title>Practical XML</title>
  <author title="Ms" name="Eris Kallisti"/>
  <chapter>
    <title>A Namespace Called fnord</title>
  </chapter>
</book>
</document>

Here we find three different uses of the name title. If you wish to process this document you will encounter problems because each of the titles should be displayed in a different manner – even though they have the same name.

The solution would be to have some means of identifying the first occurrence of title as the title of a book, i.e. to use the title element of a book namespace to distinguish it from, for example, the chapter title, e.g.:

<book:title>Practical XML</book:title>

book in this case is a prefix denoting the namespace.

Before we can apply a namespace to element or attribute names we must declare it.

Namespaces are URIs like http://www.example.com/fnord/book/. This does not mean that data must be available at this address; the URI is simply used to provide a unique name.

We declare namespaces in the same way as attributes; strictly speaking they are attributes. To make for example http://www.example.com/fnord/ the document’s default XML namespace xmlns we write

xmlns="http://example.com/fnord/"

To distinguish the http://www.example.com/fnord/book/ namespace from the default, we must supply it with a prefix:

xmlns:book="http://example.com/fnord/book/"

A namespace that is declared like this can be applied to element and attribute names by prepending the appropriate prefix and a “:” delimiter. We have already seen this with the book:title element.

Element names without a prefix belong to the default namespace. This rule does not apply to attributes: an attribute without a prefix does not belong to any of the declared XML namespaces at all. Attributes always belong to the “traditional” namespace of the element in which they appear. A “traditional” namespace is not an XML namespace, it simply means that all attribute names belonging to one element must be different. Later we will see how to assign an XML namespace to an attribute.

Due to the fact that attributes without prefixes are not in any XML namespace there is no collision between the attribute title (that belongs to the author element) and for example the title element within a chapter.

Let’s clarify this with an example:

<document xmlns:book = 'http://example.com/fnord/book/'
          xmlns      = 'http://example.com/fnord/' >
<book>
  <book:title>Practical XML</book:title>
  <book:author xmlns:fnord = 'http://example.com/fnord/'
               title="Ms"
               fnord:title="Goddess"
               name="Eris Kallisti"/>
  <chapter>
    <title>A Namespace Called fnord</title>
  </chapter>
</book>
</document>

Within the document element we have two namespaces declared. The default namespace http://www.example.com/fnord/ applies to the book element, the chapter element, the appropriate title element and of course to document itself.

The book:author and book:title elements belong to the namespace with the URI http://www.example.com/fnord/book/.

The two book:author attributes title and name have no XML namespace assigned. They are only members of the “traditional” namespace of the element book:author, meaning that for example two title attributes in book:author are forbidden.

In the above example we circumvent the last rule by adding a title attribute from the http://www.example.com/fnord/ namespace to book:author: the fnord:title comes from the namespace with the prefix fnord that is declared in the book:author element.

Clearly the fnord namespace has the same namespace URI as the default namespace. So why didn’t we simply use the default namespace we’d already declared? The answer is quite complex:

  • attributes without a prefix don’t belong to any XML namespace at all, not even to the default namespace;

  • additionally omitting the prefix would lead to a title-title clash;

  • writing it as xmlns:title would declare a new namespace with the prefix title instead of applying the default xmlns namespace.

With the Qt XML classes elements and attributes can be accessed in two ways: either by referring to their qualified names consisting of the namespace prefix and the “real” name (or local name) or by the combination of local name and namespace URI.

More information on XML namespaces can be found at http://www.w3.org/TR/REC-xml-names/ .

Conventions Used in the Qt XML Documentation#

The following terms are used to distinguish the parts of names within the context of namespaces:

  • The qualified name is the name as it appears in the document. (In the above example book:title is a qualified name.)

  • A namespace prefix in a qualified name is the part to the left of the “:”. (book is the namespace prefix in book:title.)

  • The local part of a name (also referred to as the local name) appears to the right of the “:”. (Thus title is the local part of book:title.)

  • The namespace URI (“Uniform Resource Identifier”) is a unique identifier for a namespace. It looks like a URL (e.g. http://www.example.com/fnord/ ) but does not require data to be accessible by the given protocol at the named address.

Elements without a “:” (like chapter in the example) do not have a namespace prefix. In this case the local part and the qualified name are identical (i.e. chapter).

See also

DOM Bookmarks Application