Section 16.1.  The namespaces solution

Prev don't be afraid of buying books Next

16.1. The namespaces solution

Given that what we want to do is qualify names, the most obvious idea is to have a prefix that does the qualification, as in myBrowserProgram:email or myEmailProgram:email. This seems to work at first, but eventually two people will make program names that clash.

In the real world people constantly choose names that other people also choose. Even city names can clash: consider how many there are named "Springfield"! If people are allowed to choose names without any central authority then they will eventually choose names that clash.

The World Wide Web Consortium could set up a registry of these prefixes, but a suitable registry already exists – the domain name registry. It has the extra benefit that the domains can be subdivided into multiple prefixes by using the familiar URI mechanism (typically http: URLs).

Example 16-1 shows what URI-based namespace prefixes might look like.

Example 16-1. Mythical (illegal!) URI-based namespace prefix
 <http://www.aol.com/EmailAppGuy;email>email@machine.com </http://www.aol.com/EmailAppGuy:email> 

There are two problems with these prefixes. First, they are not legal XML names because of all of the funny characters such as slashes and dots. Second, they are ugly and incredibly verbose.

The solution is to set up a local abbreviation, like an entity reference for the prefix. The Namespaces in XML specification defines the mechanism for setting up such abbreviations.

16.1.1 Namespace prefixes

The Namespaces in XML specification defines a rule that attributes that start with the prefix xmlns: should be interpreted as prefix-defining attributes. The name immediately following the prefix is a local abbreviation for the namespace.

The attribute value is a URI reference. You can use any URI (typically a URL) that you would normally have control over. Throughout the element exhibiting that attribute, the prefix stands for the namespace. Example 16-2 demonstrates.

Example 16-2. Using XML namespaces
 <eag:email xmlns:eag="http://www.aol.com/EmailAppGuy> email@machine.com </eag:email> 

The actual prefix you use is not relevant. It is just a stand-in for the URI. So, for example, when creating an XSL stylesheet you do not need to use the xsl: prefix for names defined in the XSL spec. Doing otherwise might be confusing, but it is totally legal.

Note that the details of the URI are not relevant either. It does not matter whether there is a document at that location or whether the client machine is Internet-connected. There is no need to connect to the Internet to check the contents of the document at that address.

That's an important point! Namespaces work with broken URIs because namespaces only disambiguate names, they don't define names. The only requirement is that you really do control the URI that you use.

16.1.2 Scoping

The prefix scheme is still pretty verbose, but it is some improvement. It looks better when you realize that namespace declarations are scoped by their declaring elements. That means that they apply to the element, its children, and the children's children and so forth unless some child has a declaration that specifically overrides the first declaration. Example 16-3 shows a document that uses a namespace associated with the myns prefix many times despite declaring it only once.

Example 16-3. Using an ancestor's namespace
 <myns:a xmlns:myns="http://www.someurl.com/2002">     <myns:b>     </myns:b>     <myns:c>         <myns:d>         </myns:d>     </myns:c> </myns:a> 

Therefore you could declare namespaces in the document (root) element and have them apply throughout the entire document! Example 16-4 demonstrates.[1]

[1] Note that .con is the new high-level domain for Internet scams.

Example 16-4. XML namespace scope
 <html:html     xmlns:eag="http://www.aol.com/EmailAppGuy"     xmlns:html="http://www.w3.org/TR/WD-HTML40"     xmlns:math="http://www.w3.org/TR/REC-MathML/">   <html:title>George Soros Personal Wealth Page</html:title>   <html:h2>Counting My Cash</html:h2>   <html:p>As you know, my cash rivals the gross national product of some small countries. Consider the following equation:   <math:reln>     <math:eq/>     <math:ci>wealth</math:ci>     <math:ci>gnp</math:ci>   </math:reln> If you have any ideas of how I could spend this money. Please contact   <eag:email>georges@aol.con</eag:email>. </html:p> </html:html> 

16.1.3 Default namespace

We can minimize namespace clutter even more by removing some of the prefixes. There is a special namespace called the default namespace. This namespace is defined without a prefix, so element-type names in the scope of the definition that have no prefix are considered to be in this namespace.[2] If you expect to use many elements from a particular namespace, you can make it the default namespace for the appropriate scope. In fact, you can even have a document in which the namespaces correspond cleanly to the elements and there are no prefixes at all, as Example 16-5 demonstrates.

[2] You can think of the default namespace as having a null prefix, if that helps any.

Example 16-5. Two default namespaces: HTML and MathML
 <html     xmlns="http://www.w3.org/TR/WD-HTML40">   <title>George Soros Personal Wealth Page</title>   <h2>Counting My Cash</h2>   <p>As you know, my cash rivals the gross national product of some small countries. Consider the following equation:   <reln xmlns="http://www.w3.org/TR/REC-MathML/">     <eq/>     <ci>wealth</ci>     <ci>gnp</ci>   </reln> If you have any ideas of how I could spend this money.</p> </html> 

Note that the default namespace is HTML both before and after the reln element. Within the reln element the default namespace is MathML. As you can see, we can eliminate many of the prefixes but still keep the relationship between the element-type names and the namespaces.

We can also establish a scope in which names without prefixes have no namespace, by using an empty string instead of a URI. In Example 16-6, notes and todo are not in the MathML namespace.

Example 16-6. A scope for local names
 <reln xmlns="http://www.w3.org/TR/REC-MathML/">   <eq/>   <ci>wealth</ci>   <ci>gnp</ci>   <notes xmlns="">     <todo>check the math</todo>   </notes> </reln> 

16.1.4 Attribute names

Attribute names can also come from a namespace, which is indicated in the usual way by prefixing them with a namespace prefix. For instance, the XML Linking Language (XLink) uses the namespace mechanism to allow XLink attributes to appear on elements that themselves come from some other namespace. Example 16-7 shows such attributes.

Example 16-7. Attributes in XLink namespace
 <myLink xmlns:xlink="http://www.w3.org/1999/xlink"    xlink:type="simple"> ... </myLink> 

It does not matter what the namespace of the element type is. The XLink attributes are in the XLink namespace even when they are exhibited by an element type that is not in the XLink namespace.

In fact, even attributes without prefixes are not in the same namespace that their element type is in. Nor are they in the default namespace. Attributes without prefixes are in no namespace at all.

From a processing standpoint, the lack of a namespace doesn't matter. The attribute name can still be specified in a stylesheet pattern or utilized by the template for the element type's template rule.

Amazon


XML in Office 2003. Information Sharing with Desktop XML
XML in Office 2003: Information Sharing with Desktop XML
ISBN: 013142193X
EAN: 2147483647
Year: 2003
Pages: 176

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net