Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

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 refering 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 refered 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 Example and SAX Bookmarks Example.

[XML Processing] [Next: XML Streaming]

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année

Le Qt Quarterly au hasard

Logo

Des apparences personnalisées utilisant les feuilles de style

Qt Quarterly est la revue trimestrielle proposée par Nokia et à destination des développeurs Qt. Ces articles d'une grande qualité technique sont rédigés par des experts Qt. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.6
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web