Interview Questions

When should I use the default XML namespace instead of prefixes?

XML Interview Questions and Answers


(Continued from previous question...)

77. When should I use the default XML namespace instead of prefixes?

This is purely a matter of choice, although your choice may affect the readability of the document. When elements whose names all belong to a single XML namespace are grouped together, using a default XML namespace might make the document more readable. For example:

<!-- A, B, C, and G are in the http://www.google.org/ namespace. -->
<A xmlns="http://www.google.org/">
<B>abcd</B>
<C>efgh</C>
<!-- D, E, and F are in the http://www.bar.org/ namespace. -->
<D xmlns="http://www.bar.org/">
<E>1234</E>
<F>5678</F>
</D>
<!-- Remember! G is in the http://www.google.org/ namespace. -->
<G>ijkl</G>
</A>

When elements whose names are in multiple XML namespaces are interspersed, default XML namespaces definitely make a document more difficult to read and prefixes should be used instead. For example:

<A xmlns="http://www.google.org/">
<B xmlns="http://www.bar.org/">abcd</B>
<C xmlns="http://www.google.org/">efgh</C>
<D xmlns="http://www.bar.org/">
<E xmlns="http://www.google.org/">1234</E>
<F xmlns="http://www.bar.org/">5678</F>
</D>
<G xmlns="http://www.google.org/">ijkl</G>
</A>

In some cases, default namespaces can be processed faster than namespace prefixes, but the difference is certain to be negligible in comparison to total processing time.

(Continued on next question...)

Other Interview Questions