<mail>
<header>
<from>me@example.com</from>
<to>you@example.com</to>
<subject>PO for my trip</subject>
</header>
<body>
<purchaseorder>
<for>Airfare</for>
<bill_to>Editorial</bill_to>
<amount>349.50</amount>
</purchaseorder>
</body>
</mail>
It worked, but we can easily run into problems. For example, if the
purchase order used <to> instead of
<bill_to> to indicate the department to be
charged, we'd have two elements named <to>.
The resulting document is sketched here:
<mail>
<header>
<to>you@example.com</to>
</header>
<body>
<to>Editorial</to>
</body>
</mail>
This document uses to for two different purposes.
This is similar to the problem in programming where a global variable
in one module has the same name as a global variable in another
module. Programmers can't be expected to avoid variable names from
other modules, because that would require them to know every module's
variables.
The solution to the XML problem is similar to the programming
problem's solution: namespaces. A namespace is a unique prefix for
the elements and attributes in an XML vocabulary, and is used to
avoid clashes with elements from other vocabularies. If you rewrote
your purchase-order email example with namespaces, it might look like
this:
<mail xmlns:email="http://example.com/dtds/mailspec/">
<email:from>me@example.com</email:from>
<email:to>you@example.com</email:to>
<email:subject>PO for my trip</email:subject>
<email:body>
<purchaseorder xmlns:po="http://example.com/dtd/purch/">
<po:for>Airfare</po:for>
<po:to>Editorial</po:to>
<po:amount>349.50</po:amount>
</purchaseorder>
</email:body>
</mail>
An attribute like
xmnls:prefix="URL"
identifies the namespace for the contents of the element that the
attribute is attached to. In this example, there are two namespaces:
email and po. The
email:to element is different from the
po:to element, and processing software can avoid
confusion.
Most of the XML parsers in Perl support namespaces, including
XML::Parser and XML::LibXML.