Integrating Namespaces in XML Documents

There are a few ways in which we can incorporate Namespaces into our XML documents. All of these methods still use the same syntax we just covered for declaring the Namespace, with some variations on where the declaration is placed, and how the prefixes are used.

A Single Default Namespace

The simplest application of Namespaces is for documents that have only one Namespace, so that a default Namespace can be declared. This is a useful technique for declaring a Namespace for XML documents that you are going to share with other users, either within your organization or in the public, so that if they use the document with other documents, you can avoid Namespace collisions. It is good XML style to include a default Namespace, even if you do not intend to do much with the document and Namespaces.

To declare a default Namespace for your document, you simply use the xmlns attribute with your document's root element:

<?xml version="1.0"?> 
<journal xmlns="http://www.myserver.com/journal">
 <article>
  <headline>Introduction to Namespaces</headline>
  <byline>Jane Doe</byline>
  <body>Namespaces are simple...</body>
 </article>
</journal>

The advantage of declaring a default Namespace in this manner is that we do not need to use any prefix on any of the elements within the document. This keeps the document clean and easy to read, but we still have a Namespace declared so that if later we integrate this document with other documents or share it, we can avoid possible collisions between elements or attributes of the same name.

Multiple Prefixed Namespaces

Declaring a default Namespace can make a document more readable; however, there are instances in which you may want to explicitly use a prefix with all the elements in your document. You might want to do this to ensure that if the file is imported or included in other documents, the users are aware of the Namespace issues, or even just to make sure anyone who reads the document will notice that the document uses multiple Namespaces.

To make all the Namespaces in the document require a prefix, we use the same basic structure as declaring a default Namespace, only we add the : prefix to the xmlns attribute:

<?xml version="1.0"?> 
<journal:journal xmlns:journal="http://www.myserver.com/journal"
        xmlns:html="http://www.w3.org/TR/REC-html40">
 <html:html>
  <journal:article>
   <html:head>
    <html:title>
    <journal:headline>Introduction to Namespaces</journal:headline>
    </html:title>
   </html:head>
   <html:body>
    <html:i><journal:byline>Jane Doe</journal:byline></html:i>
    <journal:body>Namespaces are simple...</journal:body>
   <html:body>
  </journal:article>
 </html:html>
</journal:journal>

Note

In this example, you will notice that even the root element journal has the Namespace prefix, resulting in a journal:journal element. That is necessary because even though we are defining the Namespace in this element with the xmlns attribute, the journal root element is still part of the Namespace.


Because we have declared both Namespaces in this document with a prefix, there is no default Namespace, and we have to use a prefix with all the elements in the document. Although this does slightly impact the ease of readability, it is now explicitly clear which elements belong to which Namespace.

Default and Prefixed Namespaces

Most commonly, you will find that XML documents that utilize multiple Namespaces will use a mixture of a default Namespace and Namespaces that require the colon prefix. This can be accomplished by declaring both Namespaces—one as the default and one requiring a prefix—in the xmlns attribute with the root element:

<?xml version="1.0"?> 
<journal xmlns="http://www.myserver.com/journal"
        xmlns:html="http://www.w3.org/TR/REC-html40">
 <html:html>
  <article>
   <html:head>
    <html:title>
    <headline>Introduction to Namespaces</headline>
    </html:title>
   </html:head>
   <html:body>
    <html:i><byline>Jane Doe</byline></html:i>
    <body>Namespaces are simple...</body>
   <html:body>
  </article>
 </html:html>
</journal>

The advantage of this method is that now both Namespaces are declared globally. That is because they are declared in the root element, and the default Namespace applies to any element without a prefix in the document. Because the second Namespace is also declared globally, any element in the document can be made part of that Namespace by appending the prefix.

We could also declare the default Namespace globally, but then have the second Namespace declared locally:

<?xml version="1.0"?> 
<journal xmlns="http://www.myserver.com/journal">
 <article>
  <headline>Introduction to Namespaces</headline>
  <byline>Jane Doe</byline>
  <body>Namespaces are simple... an HTML Example	
   <html:html xmlns:html="http://www.w3.org/TR/REC-html40">
   <html:b>This is HTML</html:b>
   </html:html>
  </body>
 </article>
</journal>

This allows us to restrict the usage of the second Namespace to the children of the <html> element, because the Namespace is not declared at the root level, but instead it is declared as an attribute of the <html> element. In fact, in the preceding example we used the html: prefix to underscore which elements were members of which Namespace, but that is not actually necessary. Because the second Namespace is being declared locally, we could eliminate the prefix altogether, and essentially declare a local default Namespace:

<?xml version="1.0"?> 
<journal xmlns="http://www.myserver.com/journal">
 <article>
  <headline>Introduction to Namespaces</headline>
  <byline>Jane Doe</byline>
  <body>Namespaces are simple... an HTML Example	
   <html xmlns="http://www.w3.org/TR/REC-html40">
    <b>This is HTML</b>
   </html>
  </body>
 </article>
</journal>

Using this technique, all the children of the <html> element will be considered part of the http://www.w3.org/TR/REC-html40 Namespace, because that is the declared default Namespace for the <html> element. However, because this is a locally scoped declaration, when we close the <html> element, all the elements from that point on will be members of the document default Namespace—only the children of the <html> element will be members of the local Namespace.

There are a number of ways in which you can use Namespaces in your documents, and how you choose to incorporate Namespaces will depend largely on the specifics of your XML implementation. You may just want to declare a simple default Namespace for all of your documents, or you may want to incorporate several different Namespaces in your documents.

Namespaces apply to many of the XML technologies we are describing in this title. So, let's take a look at how Namespaces apply specifically to some of those technologies.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.103.183