IBM网站上给出的XML的介绍(8)不完全摘录
Namespaces
Page 10 of 10命名空间的“唯一性”,虽然使用类似的URL串来表示,但是,XML处理的时候并不对它进行具体的
定向,只是把URL当成是string
XML's power comes from its flexibility, the fact that you and I and millions of other people can define our own tags to describe our data. Remember the sample XML document for a person's name and address? That document includes the <title> element for a person's courtesy title, a perfectly reasonable choice for an element name. If you run an online bookstore, you might create a <title> element for the title of a book. If you run an online mortgage company, you might create a <title> element for the title to a piece of property. All of those are reasonable choices, but all of them create elements with the same name. How do you tell if a given <title> element refers to a person, a book, or a piece of property? With namespaces.
To use a namespace, you define a namespace prefix and map it to a particular string. Here's how you might define namespace prefixes for our three <title> elements:
<?xml version="1.0"?>
<customer_summary
xmlns:addr="http://www.xyz.com/addresses/"
xmlns:books="http://www.zyx.com/books/"
xmlns:mortgage="http://www.yyz.com/title/"
>
... <addr:name><title>Mrs.</title> ... </addr:name> ...
... <books:title>Lord of the Rings</books:title> ...
... <mortgage:title>NC2948-388-1983</mortgage:title> ...
In this example, the three namespace prefixes are addr, books, and mortgage. Notice that defining a namespace for a particular element means that all of its child elements belong to the same namespace. The first <title> element belongs to the addr namespace because its parent element, <addr:Name>, does.
One final point: The string in a namespace definition is just a string. Yes, these strings look like URLs, but they're not. You could define xmlns:addr="mike" and that would work just as well. The only thing that's important about the namespace string is that it's unique; that's why most namespace definitions look like URLs. The XML parser does not go to http://www.zyx.com/books/ to search for a DTD or schema, it simply uses that text as a string. It's confusing, but that's how namespaces work.
页:
[1]