Reasons for Namespaces
The appeal of XML lies in the ability to invent tags that convey meaningful information that can be processed by applications and read by users. Namespaces allow you to associate a vocabulary (or namespace) with an element or attribute name.
Example
The following XML represents information about a book.
<BOOK> <TITLE>XML Developer's Guide</TITLE> <PRICE currency="US Dollar">44.95</PRICE> </BOOK>
The following XML represents information about an author.
<AUTHOR> <TITLE>Ms</TITLE> <NAME>Ambercrombie Kim</NAME> </AUTHOR>
Although the user can distinguish between the different interpretations of the <TITLE>
element, a computer program, such as an XML parser, cannot differentiate between the two meanings. Without additional information, the parser cannot determine that the first <TITLE>
element is intended to refer to a string representing the title of the book, and that the second refers to an enumeration representing the title of the author ("Mr. ", "Ms. " , "Mrs. " , and so on).
Example
The following examples shows TITLE
associated with BookInfo
and AuthorInfo
namespaces.
<BookInfo:TITLE xmlns:BookInfo="books-namespace-URI">XML Developer's Guide</BookInfo:TITLE> <AuthorInfo:TITLE xmlns:AuthorInfo="authors-namespace-URI">Ms.</AuthorInfo:TITLE>
The W3C Namespaces in XML recommendation provides the xmlns
attribute to uniquely define a namespace for an XML document to use.
Example
The following examples show how to declare a namespace with the xmlns
attribute.
xmlns=http://www.example.microsoft.com/books.dtd xmlns="urn:schemas-microsoft-com:xml-data