~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/xml-namespaces.html

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- xml-processing.qdoc -->
 
6
<head>
 
7
  <title>Qt 4.6: An Introduction to Namespaces</title>
 
8
  <link rel="contents" href="xml-processing.html" />
 
9
  <link rel="next" href="xml-streaming.html" />
 
10
  <link href="classic.css" rel="stylesheet" type="text/css" />
 
11
</head>
 
12
<body>
 
13
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
14
<tr>
 
15
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
 
16
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <a href="overviews.html"><font color="#004faf">Overviews</font></a></td><td class="searchBar" align="right" valign="center"><form action="http://www.google.com/cse" id="cse-search-box"><div><input type="hidden" name="cx" value="000136343326384750312:dhbxnqlakyu" /><input type="hidden" name="ie" value="UTF-8" /><input type="text" name="q" size="31" /><input type="submit" name="sa" value="Search" /></div></form></td>
 
17
</tr></table><p>
 
18
[<a href="xml-processing.html">XML Processing</a>]
 
19
[Next: <a href="xml-streaming.html">XML Streaming</a>]
 
20
</p>
 
21
<h1 class="title">An Introduction to Namespaces<br /><span class="subtitle"></span>
 
22
</h1>
 
23
<a name="namespaces"></a><p>Parts of the Qt XML module documentation assume that you are familiar with XML namespaces. Here we present a brief introduction; skip to <a href="#namespacesconventions">Qt XML documentation conventions</a> if you already know this material.</p>
 
24
<p>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.</p>
 
25
<p>Consider the following example:</p>
 
26
<pre> &lt;document&gt;
 
27
 &lt;book&gt;
 
28
   &lt;title&gt;Practical XML&lt;/title&gt;
 
29
   &lt;author title=&quot;Ms&quot; name=&quot;Eris Kallisti&quot;/&gt;
 
30
   &lt;chapter&gt;
 
31
     &lt;title&gt;A Namespace Called fnord&lt;/title&gt;
 
32
   &lt;/chapter&gt;
 
33
 &lt;/book&gt;
 
34
 &lt;/document&gt;</pre>
 
35
<p>Here we find three different uses of the name <i>title</i>. If you wish to process this document you will encounter problems because each of the <i>titles</i> should be displayed in a different manner -- even though they have the same name.</p>
 
36
<p>The solution would be to have some means of identifying the first occurrence of <i>title</i> as the title of a book, i.e&#x2e; to use the <i>title</i> element of a book namespace to distinguish it from, for example, the chapter title, e.g&#x2e;:</p>
 
37
<pre> &lt;book:title&gt;Practical XML&lt;/book:title&gt;</pre>
 
38
<p><i>book</i> in this case is a <i>prefix</i> denoting the namespace.</p>
 
39
<p>Before we can apply a namespace to element or attribute names we must declare it.</p>
 
40
<p>Namespaces are URIs like <i>http://www.example.com/fnord/book/</i>. This does not mean that data must be available at this address; the URI is simply used to provide a unique name.</p>
 
41
<p>We declare namespaces in the same way as attributes; strictly speaking they <i>are</i> attributes. To make for example <i>http://www.example.com/fnord/</i> the document's default XML namespace <i>xmlns</i> we write</p>
 
42
<pre> xmlns=&quot;http://example.com/fnord/&quot;</pre>
 
43
<p>To distinguish the <i>http://www.example.com/fnord/book/</i> namespace from the default, we must supply it with a prefix:</p>
 
44
<pre> xmlns:book=&quot;http://example.com/fnord/book/&quot;</pre>
 
45
<p>A namespace that is declared like this can be applied to element and attribute names by prepending the appropriate prefix and a &quot;:&quot; delimiter. We have already seen this with the <i>book:title</i> element.</p>
 
46
<p>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 &quot;traditional&quot; namespace of the element in which they appear. A &quot;traditional&quot; 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.</p>
 
47
<p>Due to the fact that attributes without prefixes are not in any XML namespace there is no collision between the attribute <i>title</i> (that belongs to the <i>author</i> element) and for example the <i>title</i> element within a <i>chapter</i>.</p>
 
48
<p>Let's clarify this with an example:</p>
 
49
<pre> &lt;document xmlns:book = 'http://example.com/fnord/book/'
 
50
           xmlns      = 'http://example.com/fnord/' &gt;
 
51
 &lt;book&gt;
 
52
   &lt;book:title&gt;Practical XML&lt;/book:title&gt;
 
53
   &lt;book:author xmlns:fnord = 'http://example.com/fnord/'
 
54
                title=&quot;Ms&quot;
 
55
                fnord:title=&quot;Goddess&quot;
 
56
                name=&quot;Eris Kallisti&quot;/&gt;
 
57
   &lt;chapter&gt;
 
58
     &lt;title&gt;A Namespace Called fnord&lt;/title&gt;
 
59
   &lt;/chapter&gt;
 
60
 &lt;/book&gt;
 
61
 &lt;/document&gt;</pre>
 
62
<p>Within the <i>document</i> element we have two namespaces declared. The default namespace <i>http://www.example.com/fnord/</i> applies to the <i>book</i> element, the <i>chapter</i> element, the appropriate <i>title</i> element and of course to <i>document</i> itself.</p>
 
63
<p>The <i>book:author</i> and <i>book:title</i> elements belong to the namespace with the URI <i>http://www.example.com/fnord/book/</i>.</p>
 
64
<p>The two <i>book:author</i> attributes <i>title</i> and <i>name</i> have no XML namespace assigned. They are only members of the &quot;traditional&quot; namespace of the element <i>book:author</i>, meaning that for example two <i>title</i> attributes in <i>book:author</i> are forbidden.</p>
 
65
<p>In the above example we circumvent the last rule by adding a <i>title</i> attribute from the <i>http://www.example.com/fnord/</i> namespace to <i>book:author</i>: the <i>fnord:title</i> comes from the namespace with the prefix <i>fnord</i> that is declared in the <i>book:author</i> element.</p>
 
66
<p>Clearly the <i>fnord</i> 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:</p>
 
67
<ul>
 
68
<li>attributes without a prefix don't belong to any XML namespace at all, not even to the default namespace;</li>
 
69
<li>additionally omitting the prefix would lead to a <i>title-title</i> clash;</li>
 
70
<li>writing it as <i>xmlns:title</i> would declare a new namespace with the prefix <i>title</i> instead of applying the default <i>xmlns</i> namespace.</li>
 
71
</ul>
 
72
<p>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 &quot;real&quot; name (or <i>local</i> name) or by the combination of local name and namespace URI.</p>
 
73
<p>More information on XML namespaces can be found at <a href="http://www.w3.org/TR/REC-xml-names/">http://www.w3.org/TR/REC-xml-names/</a>.</p>
 
74
<a name="namespacesconventions"></a><a name="conventions-used-in-the-qt-xml-documentation"></a>
 
75
<h2>Conventions Used in the Qt XML Documentation</h2>
 
76
<p>The following terms are used to distinguish the parts of names within the context of namespaces:</p>
 
77
<ul>
 
78
<li>The <i>qualified name</i> is the name as it appears in the document. (In the above example <i>book:title</i> is a qualified name.)</li>
 
79
<li>A <i>namespace prefix</i> in a qualified name is the part to the left of the &quot;:&quot;. (<i>book</i> is the namespace prefix in <i>book:title</i>.)</li>
 
80
<li>The <i>local part</i> of a name (also refered to as the <i>local name</i>) appears to the right of the &quot;:&quot;. (Thus <i>title</i> is the local part of <i>book:title</i>.)</li>
 
81
<li>The <i>namespace URI</i> (&quot;Uniform Resource Identifier&quot;) is a unique identifier for a namespace. It looks like a URL (e.g&#x2e; <i>http://www.example.com/fnord/</i> ) but does not require data to be accessible by the given protocol at the named address.</li>
 
82
</ul>
 
83
<p>Elements without a &quot;:&quot; (like <i>chapter</i> in the example) do not have a namespace prefix. In this case the local part and the qualified name are identical (i.e&#x2e; <i>chapter</i>).</p>
 
84
<p>See also <a href="xml-dombookmarks.html">DOM Bookmarks Example</a> and <a href="xml-saxbookmarks.html">SAX Bookmarks Example</a>.</p>
 
85
<p>
 
86
[<a href="xml-processing.html">XML Processing</a>]
 
87
[Next: <a href="xml-streaming.html">XML Streaming</a>]
 
88
</p>
 
89
<p /><address><hr /><div align="center">
 
90
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
91
<td width="40%" align="left">Copyright &copy; 2009 Nokia Corporation and/or its subsidiary(-ies)</td>
 
92
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
93
<td width="40%" align="right"><div align="right">Qt 4.6.0</div></td>
 
94
<script type="text/javascript" src="http://www.google.com/jsapi"></script><script type="text/javascript">google.load("elements", "1", {packages: "transliteration"});</script><script type="text/javascript" src="http://www.google.com/coop/cse/t13n?form=cse-search-box&t13n_langs=en"></script><script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en"></script></tr></table></div></address></body>
 
95
</html>