Namespaces in XML

As markup languages proliferate, markup language designers will want to reuse portions of languages instead of reinventing the wheel. This poses the problem of naming collisions. For example, what if we mixed HTML tags with our own Book Review Markup Language that also had a <TITLE> tag?

For example:

<HTML>
<HEAD>
      <TITLE> Book Review Page </TITLE>
</HEAD>
<BODY>
      <BOOK>
            <TITLE>  Developing XML in Java </TITLE>
            <AUTHOR>  Michael C. Daconta   </AUTHOR>
     </BOOK>
</BODY>
</HTML>

If a program were to parse this document, how would the programmer know which TITLE was the book title? In order to accomplish this, element and attribute names must be universal. To create a universal name, an XML name is separated into two parts: a namespace prefix and a local part. The World Wide Web Consortium (W3C) formalized the rules for creating these universal names in the Namespaces Specification, which became a W3C Recommendation on January 14, 1999. So, rewriting the previous example using namespaces produces

<ht:HTML xmlns:ht="http://www.w3.org/1999/xhtml"
                 xmlns:bk="http://www.gosynergy.com/brml">
<ht:HEAD>
      <ht:TITLE> Book Review Page </ht:TITLE>
</ht:HEAD>
<ht:BODY>
      <bk:BOOK>
            <bk:TITLE>  Developing XML in Java </bk:TITLE>
            <bk:AUTHOR>  Michael C. Daconta   </bk:AUTHOR>
     </bk:BOOK>
</ht:BODY>
</ht:HTML>

Declaring Namespaces

A namespace is declared using an attribute whose prefix is xmlns as follows:

<TEST  xmlns:syn="http://www.gosynergy.com/example">

The value of the xmlns attribute is any Uniform Resource Identifier, which functions as the namespace name. The URI does not have to actually exist. Attributes, not just elements, can also have namespaces. As an example, we could use an HTML alignment attribute to align our book title like this:

<ht:HTML xmlns:ht="http://www.w3c.org/HTML/1999/html4"
                 xmlns:bk="http://www.gosynergy.com/brml">
<ht:HEAD>
      <ht:TITLE> Book Review Page </ht:TITLE>
</ht:HEAD>
<ht:BODY>
      <bk:BOOK>
            <bk:TITLE ht:ALIGN="left"> Developing XML in Java </bk:TITLE>
            <bk:AUTHOR>  Michael C. Daconta   </bk:AUTHOR>
     </bk:BOOK>
</ht:BODY>
</ht:HTML>

Before a prefix can be used in a document, it must be declared in the current tag or in an ancestor tag that contains the current tag. Lastly, a namespace has scope. This means that the namespace applies to the element in which it is declared and all elements within the content of that element.

How Namespaces Affect the DTD

Attribute and element names are also given as qualified names (what we called universal names) in the DTD declarations. Here is an example of the DTD for our BOOK example:

<!ELEMENT bk:BOOK (bk:TITLE, bk:AUTHOR?)>
<!ATTLIST bk:BOOK
          xmlns:bk CDATA #FIXED
                   "http://www.gosynergy.com/brml">
<!ATTLIST bk:BOOK
          bk:pages CDATA #IMPLIED>

You should understand that to keep backward compatibility with SGML (which allows a colon as part of an SGML name), this is really just a syntactic trick to separate one name into two parts. Therefore, to validate the document, all names in the DTD must be modified to include the prefix part in each element and attribute declaration. Here is another example of a simple markup language that uses namespaces:

<?xml version="1.0" ?>
<!DOCTYPE slf:entries [
<!ELEMENT slf:entries (entry)* >
<!ELEMENT slf:entry (field)* >
<!ELEMENT slf:field (#PCDATA) >
<!ATTLIST slf:entries
          xmlns:slf CDATA #FIXED "http://www.gosynergy.com/slf">
<!ATTLIST slf:entry
          slf:type (general|security|fatalerror|
          error|warning|info|trace)   #REQUIRED
          slf:source CDATA #IMPLIED>
<!ATTLIST slf:field
          slf:name CDATA #REQUIRED >
]>

<slf:entries>

<slf:entry slf:type = 'trace'>
<slf:field slf:name='timestamp'>January 24, 2000 12:21:13 PM EST</slf:field>
<slf:field slf:name='class'> java.lang.Exception</slf:field>
<slf:field slf:name='method'>&lt;init&gt;</slf:field>
<slf:field slf:name='message'>View</slf:field>
</slf:entry>
<slf:entry slf:type = 'trace'>
<slf:field slf:name='timestamp'>January 24, 2000 3:51:48 PM EST</slf:field>
<slf:field slf:name='class'>GOV.dia.mditds.audit.AuditManagerApplet</slf:field>
<slf:field slf:name='method'>actionPerformed</slf:field>
<slf:field slf:name='message'>View</slf:field>
</slf:entry>

</slf:entries>

Applying Namespaces

In order to remove the burden of redundant typing, the specification allows a default namespace. A default namespace will apply to the current element where the namespace is declared if it does not have a prefix and to all subelements that do not have a prefix. So the example could be rewritten:

<HTML xmlns="http://www.w3.org/1999/xhtml"
        xmlns:bk="http://www.gosynergy.com/brml">
<HEAD>
      <TITLE> Book Review Page </TITLE>
</HEAD>
<BODY>
      <bk:BOOK>
            <bk:TITLE ALIGN="left"> Developing XML in Java </bk:TITLE>
            <bk:AUTHOR>  Michael C. Daconta   </bk:AUTHOR>
     </bk:BOOK>
</BODY>
</HTML>

In this example, the HTML namespace is the default namespace for all tags that do not have a prefix. A namespace can be overridden by another namespace declaration with the same namespace attribute name (either xmlns or xmlns:name). Lastly, the default namespace can be set to the empty string. This has the same effect, within the scope of the declaration, of there being no default namespace.

Parser Support for Namespaces

At the time of this writing, SAX is being extended to incorporate namespace support with a new set of interfaces referred to as SAX2. SAX2 will support namespace processing by default. This means that every element and attribute will be reported with a two-part name. The new API for an element is as follows:

public void startElement (String uri, String localName,
                               String rawName, Attributes atts)
       throws SAXException;
public void endElement (String uri, String localName, String rawName)
       throws SAXException;

Note

At the time of this writing, SAX2 is not widely supported. Go to the URL http://www.megginson.com/SAX/ for more information.


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

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