XML Interviews Question page20
-
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.
-
What is the scope of an XML namespace declaration?
The scope of an XML namespace declaration is that part of an XML document to which the declaration applies. An XML namespace declaration remains in scope for the element on which it is declared and all of its descendants, unless it is overridden or undeclared on one of those descendants.
For example, in the following, the scope of the declaration of the http://www.google.org/ namespace is the element A and its descendants (B and C). The scope of the declaration of the http://www.bar.org/ namespace is only the element C.
<google:A xmlns:google="http://www.google.org/">
<google:B>
<bar:C xmlns:bar="http://www.bar.org/" />
</google:B>
</google:A>
-
Does the scope of an XML namespace declaration include the element it is declared on?
Yes.
For example, in the following, the names B and C are in the http://www.bar.org/ namespace, not the http://www.google.org/ namespace. This is because the declaration that associates the google prefix with the http://www.bar.org/ namespace occurs on the B element, overriding the declaration on the A element that associates it with the http://www.google.org/ namespace.
<google:A xmlns:google="http://www.google.org/">
<google:B xmlns:google="http://www.bar.org/">
<google:C>abcd</google:C>
</google:B>
</google:A>
Similarly, in the following, the names B and C are in the http://www.bar.org/ namespace, not the http://www.google.org/ namespace because the declaration declaring http://www.bar.org/ as the default XML namespace occurs on the B element, overriding the declaration on the A element.
<A xmlns="http://www.google.org/">
<B xmlns="http://www.bar.org/">
<C>abcd</C>
</B>
</A>
A final example is that, in the following, the attribute name D is in the http://www.bar.org/ namespace.
<google:A xmlns:google="http://www.google.org/">
<google:B google:D="In http://www.bar.org/ namespace"
xmlns:google="http://www.bar.org/">
<C>abcd</C>
</google:B>
</google:A>
One consequence of XML namespace declarations applying to the elements they occur on is that they actually apply before they appear. Because of this, software that processes qualified names should be particularly careful to scan the attributes of an element for XML namespace declarations before deciding what XML namespace (if any) an element type or attribute name belongs to.