An XML namespace is a collection of names that can be used as element or attribute names in an XML document. The namespace qualifies element names uniquely on the Web in order to avoid conflicts between elements with the same name. The namespace is identified by some Uniform Resource Identifier (URI), either a Uniform Resource Locator (URL), or a Uniform Resource Name (URN), but it doesn't matter what, if anything, it points to. URIs are used simply because they are globally unique across the Internet.
Namespaces can be declared either explicitly or by default. With an explicit declaration, you define a shorthand, or prefix, to substitute for the full name of the namespace. You use this prefix to qualify elements belonging to that namespace. Explicit declarations are useful when a node contains elements from different namespaces. A default declaration declares a namespace to be used for all elements within its scope, and a prefix is not used.
The following explicit declaration declares bk
and money
to be shorthand for the full names of their respective namespaces. The xmlns
attribute is an XML keyword for a namespace declaration.
<BOOKS>
<bk:BOOK xmlns:bk="urn:example.microsoft.com:BookInfo"
xmlns:money="urn:Finance:Money">
<bk:TITLE>Creepy Crawlies</bk:TITLE>
<bk:PRICE money:currency="US Dollar">22.95</bk:PRICE>
</bk:BOOK>
</BOOKS>
All elements beginning with bk:
or money:
are considered to be from the namespace urn:example.microsoft.com:BookInfo
or urn:Finance:Money
, respectively. Run the mouse over the XML to reveal information about the different elements.
A namespace declared without a prefix becomes the default namespace for the document. All elements and attributes in the document that do not have a prefix will then belong to the default namespace. The following example declares that the <BOOK>
element and all elements and attributes within it (<TITLE>
, <PRICE>
, currency
) are from the namespace urn:example.microsoft.com:BookInfo
.
<BOOK xmlns="urn:example.microsoft.com:BookInfo"> <TITLE>Creepy Crawlies</TITLE> <PRICE currency="US Dollar">22.95</PRICE>
In the following text box, add an explicit namespace declaration to the <BOOK>
element of the following XML document for the namespace urn:example.microsoft.com:AuthorInfo
with a prefix of author
. Click the Well-formed? button to see whether your XML document conforms to the XML specification.
<BOOKS>
<bk:BOOK xmlns:bk="urn:example.microsoft.com:BookInfo"
xmlns:money="urn:Finance:Money">
<bk:TITLE>Creepy Crawlies</bk:TITLE>
<bk:PRICE money:currency="US Dollar">22.95</bk:PRICE>
</bk:BOOK>
</BOOKS>
Now, add an element to the XML above called <AUTHOR>
, qualified with the namespace prefix author
, containing the book's author, Stefan Knorr
. Click the Well-formed? button to see whether your XML document conforms to the XML specification.
<BOOKS>
<bk:BOOK xmlns:bk="urn:example.microsoft.com:BookInfo"
xmlns:money="urn:Finance:Money"
xmlns:author="urn:example.microsoft.com:AuthorInfo">
<bk:TITLE>Creepy Crawlies</bk:TITLE>
<bk:PRICE money:currency="US Dollar">22.95</bk:PRICE>
<author:AUTHOR>Stefan Knorr </author:AUTHOR>
</bk:BOOK>
</BOOKS>