Describe the use of namespaces in an XML document.

The XML namespaces recommendation defines a way to distinguish between duplicate element type and attribute names. Such duplication might occur, for example, in an XSLT stylesheet or in a document that contains element types and attributes from two different DTDs.

An XML namespace is a collection of element type and attribute names. The namespace is identified by a unique name, which is a URI. Thus, any element type or attribute name in an XML namespace can be uniquely identified by a two-part name: the name of its XML namespace and its local name. This two-part naming system is the only thing defined by the XML namespaces recommendation.

XML namespaces are declared with an xmlns attribute, which can associate a prefix with the namespace. You can declare an XML namespace on any element in an XML document. The declaration is in scope for the element containing the attribute and all its descendants (unless it is overridden or undeclared on one of those descendants). It is a common practice to declare all namespaces within the root element. For example:


<!-- Declares two XML namespaces. Their scope is the 'aaa' and 'bbb' elements. -->

<aaa xmlns:foo="http://www.foo.org/" xmlns="http://www.bar.org/">
  <bbb>abcd</bbb>
</aaa>

					

If an XML namespace declaration contains a prefix, you refer to element type and attribute names in that namespace with the prefix. For example:


<!-- 'aaa' and 'bbb' are in the 'http://www.foo.org/' namespace, 
     which is associated with the 'foo' prefix. -->

<foo:aaa xmlns:foo="http://www.foo.org/">
  <foo:bbb>abcd</foo:bbb>
</foo:aaa>

					

If an XML namespace declaration does not contain a prefix, the namespace is the default XML namespace and you refer to element type names in that namespace without a prefix. For example:


<!-- This is equivalent to the previous example but uses a 
     DEFAULT namespace instead of the 'foo' prefix. -->

<aaa xmlns="http://www.foo.org/">
  <bbb>abcd<bbb>
</aaa>

					

The value (unique URI) of the xmlns attribute identifies the namespace, not the prefix. In this example, all elements belong to the same namespace although different prefixes are used.


<!-- 'aaa' and 'bbb' belong to the same 'http://www.foo.org/' namespace. -->

<foo:aaa xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.foo.org/">
  <bar:bbb>abcd</bar:bbb>
</foo:aaa>

					

In this example, all elements belong to different namespaces although they have the same prefixes.


<!-- 'aaa' and 'bbb' belong to different namespaces. -->

<foo:aaa xmlns:foo="http://www.foo.org/">
  <foo:aaa /> 
</foo:aaa>
<foo:bbb xmlns:foo="http://www.bar.org/">
  <foo:bbb /> 
</foo:bbb>

					

You may override the prefix used in an XML namespace declaration, simply declare another XML namespace with the same prefix. For example, in the following, the foo prefix is associated with the http://www.foo.org/ namespace on the aaa element and the http://www.bar.org/ namespace on the bbb element. That is, the name aaa is in the http://www.foo.org/ namespace and the name bbb is in the http://www.bar.org/ namespace.


<!-- 'bbb' belongs to overriden 'http://www.bar.org/' namespace. -->

<foo:aaa xmlns:foo="http://www.foo.org/">
  <foo:bbb xmlns:foo="http://www.bar.org/">abcd</foo:bbb>
</foo:aaa>

					
This practice leads to documents that are confusing to read and should be avoided.

When an XML namespace declaration goes out of scope, it simply no longer applies. For example, in the following, the declaration of the http://www.foo.org/ namespace does not apply to the bbb element because this is outside its scope.


<!-- 'bbb' does NOT belong to any  namespace. -->

<aaa xmlns="http://www.foo.org/">abcd</aaa>
<bbb>abcd</bbb>

					

You may undeclare the default XML namespace - declare a default XML namespace with an empty (zero-length) name (URI). Within the scope of this declaration, unprefixed element type names do not belong to any XML namespace. For example, in the following, the default XML namespace is the http://www.foo.org/ for the aaa and there is no default XML namespace for the bbb elements. The name aaa is in the http://www.foo.org/ namespace and the name bbb is not in any XML namespace.


<!-- 'bbb' does NOT belong to any  namespace. -->

<aaa xmlns="http://www.foo.org/">
  <aaa>
    <bbb xmlns="">
      <bbb>abcd</bbb>
    </bbb>
  </aaa>
</aaa>

					

You may NOT undeclare XML namespace prefix. It remains in scope until the end of the element on which it was declared unless it is overridden. Furthermore, trying to undeclare a prefix by redeclaring it with an empty (zero-length) name (URI) results in a namespace error. For example:


<!-- You may NOT undeclare XML namespace prefix. -->

<foo:aaa xmlns:foo="http://www.foo.org/">
  <foo:aaa>
    <foo:bbb xmlns:foo=""> <!-- ERROR -->
       <foo:bbb>abcd</foo:bbb>
     </foo:bbb>
  </foo:aaa>
</foo:aaa>

					

Attributes can be also explicitly assigned to the given namespace. See the example:


<!-- 
     You may assign namespaces to attributes. 
     'bbb' element belongs to 'http://www.bar.org/' namespace.
     'attr' attribute belongs to 'http://www.foo.org/' namespace.
-->

<foo:aaa xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org/">
  <bar:bbb foo:attr="attribute">abcd</bar:bbb>
</foo:aaa>

							
Attributes without a prefix never belongs to any namespace. The attributes do not belong to any namespace even if a default namespace is defined for the relevant element.

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCBCD 1.3 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     Free SCJP 5.0 (Tiger) Study Guide     Free Mock Exam Engine     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     IBM Test 000-255. Developing with IBM Rational Application Developer for WebSphere Software V6 Study Guide