XML Interviews Question page18
-
How do I override a default XML namespace declaration?
To override the current default XML namespace, you simply declare another XML namespace as the default. For example, in the following, the default XML namespace is the http://www.google.org/ namespace on the A and B elements and the http://www.bar.org/ namespace on the C and D elements. That is, the names A and B are in the http://www.google.org/ namespace and the names C and D are in the http://www.bar.org/ namespace.
<A xmlns="http://www.google.org/">
<B>
<C xmlns="http://www.bar.org/">
<D>abcd</D>
</C>
</B>
</A>
Using multiple default XML namespaces can lead to documents that are confusing to read and should be done carefully.
-
How do I undeclare an XML namespace prefix?
In version 1.0 of the XML namespaces recommendation, you cannot "undeclare" an 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:
<google:A xmlns:google="http://www.google.org/">
<google:B>
<google:C xmlns:google=""> <==== This is an error in v1.0, legal in v1.1.
<google:D>abcd</google:D>
</google:C>
</google:B>
</google:A>
In version 1.1 of the XML namespaces recommendation [currently a candidate recommendation -- February, 2003], you can undeclare an XML namespace prefix by redeclaring it with an empty name. For example, in the above document, the XML namespace declaration xmlns:google="" is legal and removes the mapping from the google prefix to the http://www.google.org URI. Because of this, the use of the google prefix in the google:D element results in a namespace error.
-
How do I undeclare the default XML namespace?
To "undeclare" the default XML namespace, you 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.google.org/ for the A and B elements and there is no default XML namespace for the C and D elements. That is, the names A and B are in the http://www.google.org/ namespace and the names C and D are not in any XML namespace.
<A xmlns="http://www.google.org/">
<B>
<C xmlns="">
<D>abcd</D>
</C>
</B>
</A>