Namespace Declaration Scope
Namespace declarations have scope. This means that the namespaces can appear anywhere in the document, but have scope, like programming variables, and the namespaces are in effect for their appropriate scope. There are two kinds of scope, default and qualified.
A default namespace is a namespace declared in the root element. It applies to all unqualified elements in the document. A qualified namespace is one declared when a more specific namespace overrides in somewhere in the document.
Although a namespace must be declared before it can be used, it does not mean that it must appear at the top of the XML document. For example, the following sample shows a qualified namespace being declared in the middle of the data, at the BOOK
element level, and the namespace applies to all of its descendants only.
<Author>Joe Smith</Author>
<BOOK xmlns:book="https://www.contoso.com">
<title>My Wonderful Day</title>
<price>$3.95</price>
</BOOK>
<Publisher>
<Name>MSPress</Name>
</Publisher>
The namespace defined at the BOOK
element does not apply to elements outside of the BOOK
element, such as the Publisher
element. When a namespace appears in a document, it implies that the namespace being declared is effective from its point of declaration until the end of the element, where it was declared.
If a namespace had been declared for the Publisher
element, it needs to be fully qualified by prepending it to the element with a colon. Assuming the Publisher
element belongs to the mybook
namespace, the element is declared as <mybook:Publisher>
.
See Also
Namespaces in an XML Document | Namespace Declarations | Default Namespaces | Manage Namespaces Using the XmlNamespaceManager | Information Obtained from the XmlNamespaceManager