When the Rolling Stones sang "You Can't Always Get What You Want," they were telling only half of the story. The other half is, "You Don't Always Want What You Get." Yeah, it doesn't roll off of the tongue the same way, and I don't sing anything like Mick Jagger; in fact, my children would prefer it if I didn't sing at all. So when I sing, they're both not getting something they want and getting something that they don't want. They'll get over it, but how would XML handle getting something expected and getting something unexpected?
Dealing with both the expected and the unexpected is what namespaces in XML are all about. A namespace is used to describe vocabularies because in some instances the same element name could have two different meanings, which is an unexpected occurrence often with undesirable results.
To put it in nontechnical terms, imagine that you have a shipment of cotton that you want to ship from India to England. Let's say that you want it to be sent on a particular ship that sails in November. Seems clear, doesn't it? Well, now imagine that there is another ship with the same name that sets sail in December. See the problem? Simply using the name isn't enough because it can have more than one meaning.
Namespaces are a URI that is used to get around this type of problem by providing what in law would be called a "meeting of the minds." It is a way to ensure that when the elements and attribute have the same names, the correct meaning is used. This is a good way to avoid conflict. The only alternative would be to guess, which was done in the previous example from the mid-1800s. In case you were wondering, they guessed wrong.
A Document Type Definition is used to describe and validate an XML document. Essentially, you spell out exactly what to expect in a particular XML document, to avoid confusion. Consider the XML document shown in Listing 6-9, basically a short list of monsters and where they've appeared.
Listing 6-9. An Example XML Document
If confusion concerning names were a possibility, a DTD like the one in Listing 6-10 would then be used.
Listing 6-10. The DTD for Listing 6-9
All that then would be left to do would be to save it in a folder called namespace on the C: drive and assign the DTD by inserting the following before the first element:
<!DOCTYPE monsters SYSTEM "C:\namespace\sample.dtd">
Just in case you haven't noticed something strange about Document Type Definitions, I want to point out that they are not XML. However, there is an XML equivalent to Document Type Definitions called schemas.
Schemas have the advantages of being XML and being able to provide greater validation than DTDs. The reason for this is that a schema can describe complex data types beyond the basic dateTime, decimal, integer, and string available with DTDs. This essentially means that it is possible to describe complex types, as shown in Listing 6-11.
Listing 6-11. Schema for Listing 6-9
Yes, it is longer, but it also better describes the XML document in greater detail than the DTD ever could. This leaves only the "how to assign it?" question, which Listing 6-12 answers.
Listing 6-12. The Document with the Schema Applied