Microsoft Word Applied xml a toolkit for Programmers Wiley doc




Download 2,96 Mb.
Pdf ko'rish
bet91/131
Sana14.05.2024
Hajmi2,96 Mb.
#232039
1   ...   87   88   89   90   91   92   93   94   ...   131
Bog'liq
Ceponkus, Hoodbhoy - Applied XML - Toolkit for Programmers

Namespaces
We’ve stressed over and over how great XML is because you can create your own tags 
and how you can use an object-oriented approach toward documents and import data 
from various sources. However, in our glee, we would be remiss if we didn’t mention the 
possibility of importing data from various sources that use the same element and attribute 
names. This is not as uncommon as you’d think. In fact, once you do think about it, you 
may end up being upset with us for not having mentioned it earlier.
Let us create a scenario for describing how and when namespaces are important. Let’s 
say that you are creating a comparative shopping application displaying information 
about books from different vendors. The vendors we are interested in are, for lack of 
better names, Narnes&Bobel and Mamazon.com. The vendors you are dealing with are 
able to supply information to you in the form of XML markup, but it is your job to put 
information from both vendors into a single XML document.
It is extremely likely that you will come across information from both vendors that utilize 
the same tag names, for example, book, author, title, price, and isdn. It is also likely that 
information from each is going to be defined by DTDs, and there is no guarantee that 
both bookstores are going to be using the exact same DTD as the other one is (after all, 
they’re competitors).
For instance, let’s say that the Narnes&Bobel site uses the following structure to 
represent information about a particular book:
title = "The Hobbit"
price = "$3.50"

while Mamazon may choose to use this format:

The Hobbit
J.R.R. Tolkein
3.25



- 96 -
Both are legitimate ways of representing information. Both structures can be defined in a 
DTD. But how does one differentiate between these book types? This example is 
intentionally scaled down, but you can easily imagine what much more complicated real-
life situations could be. For example, when the same attribute name is used in a single 
document with many elements:




Furthermore, when you decide to do special formatting on the book’s name, how do you 
distinguish it from the reviewer name attribute? Things can get pretty nasty very quickly, 
but lest you think we’re evil people for making you read this entire chapter only to tell you 
that an insurmountable obstacle prevents you from making a real-world XML application, 
rest assured! There is a solution.
Fortunately, the good people at the W3C have been giving a lot of thought to this 
situation. The solution they recommend was finalized in January 1999 and is expressed 
in the concept of 
namespaces
. Namespaces are a way of uniquely qualifying element 
and attribute names on the Web, thus avoiding conflicts between elements (and 
attributes) that have the same name.
The idea is to declare a namespace and a corresponding prefix for each DTD or schema 
(see the section 
“Concluding Thoughts on DTDs”
) and to preface every element or 
attribute that is from a particular DTD with its corresponding namespace prefix.
We see the look of consternation on your face, which means its time for another 
example. In this one, we’ve combined the information from both our vendors:

Download 2,96 Mb.
1   ...   87   88   89   90   91   92   93   94   ...   131




Download 2,96 Mb.
Pdf ko'rish

Bosh sahifa
Aloqalar

    Bosh sahifa



Microsoft Word Applied xml a toolkit for Programmers Wiley doc

Download 2,96 Mb.
Pdf ko'rish