Interview Questions

What happens when an XML namespace declaration goes out of scope?

XML Interview Questions and Answers


(Continued from previous question...)

81. What happens when an XML namespace declaration goes out of scope?

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.google.org/ namespace does not apply to the C element because this is outside its scope. That is, it is past the end of the B element, on which the http://www.google.org/ namespace was declared.
<!-- B is in the http://www.google.org/ namespace;
C is not in any XML namespace. -->
<A>
<B xmlns="http://www.google.org/">abcd</B>
<C>efgh</C>
</A>

In addition to the declaration no longer applying, any declarations that it overrode come back into scope. For example, in the following, the declaration of the http://www.google.org/ namespace is brought back into scope after the end of the B element. This is because it was overridden on the B element by the declaration of the http://www.bar.org/ namespace.
<!-- A and C are in the http://www.google.org/ namespace.
B is in the http://www.bar.org/ namespace. -->
<A xmlns="http://www.google.org/">
<B xmlns="http://www.bar.org/">abcd</B>
<C>efgh</C>
</A>

(Continued on next question...)

Other Interview Questions