Namespace Declarations

Namespaces are declared on elements using the xmlns: attribute, and the value of that attribute is the URI that identifies the namespace. The syntax for a namespace declaration is xmlns:<name>=<"uri">, where <name> is the name of the namespace prefix, and the <"uri"> is a string depicting the namespace URI. Once declared, the prefix can be used to qualify elements and attributes in an XML document and associate them with the namespace URI. Because the namespace prefix is used throughout a document, it should be short in length.

The following example below defines two BOOK elements. The two BOOK elements are not identical, as each one is associated with a different namespace. The first BOOK element is qualified by the namespace prefix, mybook, while the second BOOK element is qualified by the prefix, bb. Each namespace prefix is associated with a different namespace URI by the use of the namespace declarations on each BOOK element.

<mybook:BOOK xmlns:mybook="https://www.contoso.com/books.dtd">
<bb:BOOK xmlns:bb="urn:blueyonderairlines">

To signify that an element is a part of a particular namespace, prepend the namespace prefix to it, thereby making it a fully qualified element name. For example, if the Publisher element exists in a document, and a namespace has been declared for it, the Publisher element needs to have the namespace alias prepended to it with a colon. If the Publisher element belongs to the mybook namespace, it is declared as <mybook:Publisher>. Thus, the Publisher element is now fully qualified.

See Also

Concepts

Namespaces in an XML Document

Default Namespaces

Namespace Declaration Scope

Manage Namespaces Using the XmlNamespaceManager

Information Obtained from the XmlNamespaceManager