Chapter 3. XML Namespaces

You have seen why XML provides some benefits over binary formats and can now create well-formed XML documents. At some point, however, your applications will become more complex, and you will need to combine elements from various document types into one XML document.

Unfortunately, two document types often have elements with the same name, but with different meanings and semantics. This chapter introduces XML namespaces, the means by which you can differentiate elements and attributes of different XML document types from each other when combining them into other documents, or even when processing multiple documents simultaneously.

In this chapter, you will learn the following:

  • Why you need namespaces

  • What namespaces are, conceptually, and how they solve the problem of naming clashes

  • The syntax for using namespaces in XML documents

  • What is a URI, a URL, and a URN

Why We Need Namespaces

Because of the nature of XML, it is possible for any company or individual to create XML document types that describe the world in their own terms. If your company feels that an <order> should contain a certain set of information, while another company feels that it should contain a different set of information, both companies can go ahead and create different document types to describe that information. Both companies can even use the name <order> for entirely different uses if desired.

However, if everyone is creating personalized XML vocabularies, you'll soon run into a problem: ...

Get Beginning XML, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.