- 浏览: 68510 次
- 性别:
- 来自: 北京
文章分类
最新评论
refer to http://www.xml.com/pub/a/1999/01/namespaces.html
XML Namespaces by Example
January 14th saw the arrival of a new W3C Recommendation, Namespaces in XML. "Recommendation" is the final step in the W3C process; the status means that the document is done, frozen, agreed-upon and official.
Namespaces are a simple and straightforward way to distinguish names used in XML documents, no matter where they come from. However, the concepts are a bit abstract, and this specification has been causing some mental indigestion among those who read it. The best way to understand namespaces, as with many other things on the Web, is by example.
So let's set up a scenario: suppose XML.com wanted to start publishing reviews of XML books. We'd want to mark the info up with XML, of course, but we'd also like to use HTML to help beautify the display. Here's a tiny sample of what we might do:
<h:html xmlns:xdc="http://www.xml.com/books"
xmlns:h="http://www.w3.org/HTML/1998/html4">
<h:head><h:title>Book Review</h:title></h:head>
<h:body>
<xdc:bookreview>
<xdc:title>XML: A Primer</xdc:title>
<h:table>
<h:tr align="center">
<h:td>Author</h:td><h:td>Price</h:td>
<h:td>Pages</h:td><h:td>Date</h:td></h:tr>
<h:tr align="left">
<h:td><xdc:author>Simon St. Laurent</xdc:author></h:td>
<h:td><xdc:price>31.98</xdc:price></h:td>
<h:td><xdc:pages>352</xdc:pages></h:td>
<h:td><xdc:date>1998/01</xdc:date></h:td>
</h:tr>
</h:table>
</xdc:bookreview>
</h:body>
</h:html>
In this example, the elements prefixed with xdc are associated with a namespace whose name is http://www.xml.com/books , while those prefixed with h are associated with a namespace whose name is http://www.w3.org/HTML/1998/html4 .
The prefixes are linked to the full names using the attributes on the top element whose names begin. xmlns: . The prefixes don't mean anything at all - they are just shorthand placeholders for the full names. Those full names, you will have noticed, are URLs, i.e. Web addresses. We'll get back to why that is and what those are the addresses of a bit further on.
Why Namespaces?
But first, an obvious question: why do we need these things? They are there to help computer software do its job. For example, suppose you're a programmer working for XML.com and you want to write a program to look up the books at Amazon.com and make sure the prices are correct. Such lookups are quite easy, once you know the author and the title. The problem, of course, is that this document has XML.com's book-review tags and HTML tags all mixed up together, and you need to be sure that you're finding the book titles, not the HTML page titles.
The way you do this is to write your software to process the contents of <title> tags, but only when they're in the http://www.xml.com/books namespace. This is safe, because programmers who are not working for XML.com are not likely to be using that namespace.
Attributes Too
Attributes, not just elements, can have namespaces. For example, let's use the HTML STYLE attribute to allow an HTML browser to display our book review:
<h:html xmlns:xdc="http://www.xml.com/books"
xmlns:h="http://www.w3.org/HTML/1998/html4">
<h:head><h:title>Book Review</h:title></h:head>
<h:body>
<xdc:bookreview>
<xdc:title h:style="font-family: sans-serif;">
XML: A Primer</xdc:title>
<h:table>
<h:tr align="center">
<h:td>Author</h:td><h:td>Price</h:td>
<h:td>Pages</h:td><h:td>Date</h:td></h:tr>
<h:tr align="left">
<h:td><xdc:author>Simon St. Laurent</xdc:author></h:td>
<h:td><xdc:price>31.98</xdc:price></h:td>
<h:td><xdc:pages>352</xdc:pages></h:td>
<h:td><xdc:date>1998/01</xdc:date></h:td>
</h:tr>
</h:table>
</xdc:bookreview>
</h:body>
</h:html>
Beautification
That example above is, perhaps, kind of ugly, with all those prefixes and colons clutering up the tags. The Namespaces Recommendation allows you to declare a default namespace and leave out some prefixes, like this:
<html xmlns="http://www.w3.org/HTML/1998/html4"
xmlns:xdc="http://www.xml.com/books">
<head><title>Book Review</title></head>
<body>
<xdc:bookreview>
<xdc:title>XML: A Primer</xdc:title>
<table>
<tr align="center">
<td>Author</td><td>Price</td>
<td>Pages</td><td>Date</td></tr>
<tr align="left">
<td><xdc:author>Simon St. Laurent</xdc:author></td>
<td><xdc:price>31.98</xdc:price></td>
<td><xdc:pages>352</xdc:pages></td>
<td><xdc:date>1998/01</xdc:date></td>
</tr>
</table>
</xdc:bookreview>
</body>
</html>
In this example, anything without a prefix is assumed to be in the http://www.w3.org/HTML/1998/html4 namespace, which we're using as the namespace name for HTML (presumably, now that namespaces are official, the W3C will give HTML an official namespace name).
What Do Namespace Names Point At?
One of the confusing things about all this is that namespace names are URLs; it's easy to assume that since they're Web addresses, they must be the address of something. They're not; these are URLs, but the namespace draft doesn't care what (if anything) they point at. Think about the example of the XML.com programmer looking for book titles; that works fine without the namespace name pointing at anything.
The reason that the W3C decided to use URLs as namespace names is that they contain domain names (e.g. www.xml.com ), which work globally across the Internet.
Is That All There Is?
That's more or less all there is to it. The only purpose of namespaces is to give programmers a helping hand, enabling them to process the tags and attributes they care about and ignore those that don't matter to them.
Quite a few people, after reading earlier drafts of the Namespace Recommendation, decided that namespaces were actually a facility for modular DTDs, or were trying to duplicate the function of SGML's "Architectural Forms". None of these theories are true. The only reason namespaces exist, once again, is to give elements and attributes programmer-friendly names that will be unique across the whole Internet.
Namespaces are a simple, straightforward, unglamorous piece of syntax. But they are crucial for the future of XML programming. Because this is important, we at XML.com will be soon be posting an Annotated Namespaces, in a style similar to our Annotated XML 1.0 .
发表评论
-
How to be a Programmer: A Short,Comprehensive,and Personal Summary
2013-10-28 10:38 541well written. http://samizdat ... -
js module pattern
2013-10-12 16:21 354http://www.adequatelygood.com/ ... -
GZip compressing HTML, JavaScript, CSS etc. makes the data sent to the browser s
2013-07-31 15:48 623this is fun. http://tutorials ... -
java collection matrix
2012-08-07 11:24 695http://www.janeve.me/articles/w ... -
ghost text (aka in-field text)
2012-04-01 11:18 628http://archive.plugins.jquery.c ... -
What is Optimistic Locking vs. Pessimistic Locking
2011-09-09 16:50 792What is Optimistic Locking vs. ... -
what is DAO
2011-04-15 13:42 730http://java.sun.com/blueprints/ ... -
indenting xml in vim with xmllint
2011-01-10 09:48 670I added to my “.vimrc” file: ... -
css sprite
2010-12-15 16:57 603http://css-tricks.com/css-sprit ... -
最牛B 的 Linux Shell 命令
2010-10-30 00:08 671http://hi.baidu.com/hy0kl/blog/ ... -
GPS Bearing VS Heading
2010-10-21 15:40 1637http://gps.about.com/od/glossar ... -
Document Type Declaration
2010-07-19 22:01 795Document Type Declaration h ... -
XML Declaration must be the first line in the document.
2010-06-12 17:54 856The XML declaration typically a ... -
UCM
2010-05-08 11:41 709Two links about UCM The power ... -
What is an MXBean?
2010-01-28 11:10 687refer to http://weblogs.java. ... -
why wait() always in a loop
2010-01-19 00:17 811As we know ,jdk API doc suggest ... -
Locks in Java
2010-01-18 22:48 899copied from http://tutorials.je ... -
use jps instead of ps to find jvm process
2010-01-11 14:21 774copied from http://java.sun.com ... -
My first error of Hello Wolrd Struts
2010-01-04 09:10 839It's my first time to touch Str ... -
Unit Testing Equals and HashCode of Java Beans
2009-12-29 10:07 1275copy from http://blog.cornetdes ...
相关推荐
XML 命名空间(XML Namespaces)
Namespaces
XML and XML Namespaces XML 和 XML 名称空间 A basic understanding of DTD 对DTD有基本的了解 如果你想先学这些内容,请看我们主页上的的学习教程 -------------------------------------------------------...
书籍简介: · 第一章:XML快速入门 ...· 五.Namespaces的语法 · 六.entity的语法 · 七.DTD的语法 · 第五章:XML实例解释 · 一.定义新标识 · 二.建立XML文档 · 三.建立相应的HTML文件 · 第六章:XML相关资源
注释的语法 · 四.CDATA的语法 · 五.Namespaces的语法 · 六.entity的语法 · 七.DTD的语法 · 第五章:XML实例解释 · 一.定义新标识 · 二.建立XML文档 · 三.建立相应的HTML文件 ·...
·比RapidXML功能强很多.比Tiny都强多了.速度和Rapid差不多 ·源代码只有285k 3个文件 ...·Lacks validation, DTD processing, XML namespaces, proper handling of encoding. ·Lacks UTF-16/32 parsing.
· 第一章:XML快速入门 ...· 五.Namespaces的语法 · 六.entity的语法 · 七.DTD的语法 · 第五章:XML实例解释 · 一.定义新标识 · 二.建立XML文档 · 三.建立相应的HTML文件 · 第六章:XML相关资源
xml的入门教程,适合初学者 1 XML 简介 2 XML 的用途 3 XML 树结构 4 XML 语法规则 5 XML 元素 ...18 XML 命名空间(XML Namespaces) 19 XML CDATA 20 XML 编码 21 XML DOM 高级 22 XML Don't
• 五.Namespaces的语法 • 六.entity的语法 • 七.DTD的语法 • 第五章:XML实例解释 • 一.定义新标识 • 二.建立XML文档 • 三.建立相应的HTML文件 • 第六章:XML相关资源 五. XML和HTML的区别 XML和HTML都...
He outlines the elements of markup--demystifying concepts such as attributes, entities, and namespaces--and provides enough depth and examples to get started. Learning XML is a reliable source for ...
[done]Cgroups and Namespaces.pdf
注释的语法 · 四.CDATA的语法 · 五.Namespaces的语法 · 六.entity的语法 · 七.DTD的语法 · 第五章:XML实例解释 · 一.定义新标识 · 二.建立XML文档 · 三.建立相应的HTML文件 ·...
management solutions: namespaces and cgroups. We will look at: ● Kernel Implementation details. ●what was added/changed in brief. ● User space interface. ● Some working examples. ● Usage of ...
◎ XML轻松学习手册: ...· 五.Namespaces的语法 · 六.entity的语法 · 七.DTD的语法 · 第五章:XML实例解释 · 一.定义新标识 · 二.建立XML文档 · 三.建立相应的HTML文件 · 第六章:XML相关资源
It explains the basics of XML as well as the namespaces and objects you need to know in order to work efficiently with XML. You will see clear, practical examples that illustrate best practices in ...
18 XML 命名空间(XML Namespaces )..............................................................40 19 XML CDATA..........................................................................................
XmlNamespace and XmlNamespaces Members Section 15.8. XmlSchema and XmlSchemas Members Section 15.9. Get an XML Map from a List or Range Section 15.10. XPath Members Section 15.11. Resources ...
NET 3.5 Namespaces Poster LORES
Hypermedia concepts and alternatives to the Web <br>XML Namespaces, XML Base, XInclude, XML Information Set, XHTML, and XSLT <br>XPath, XLink, and XPointer concepts, strengths, and limitations...
Chapter 6.Namespaces in XML 1.1(Second Edition) SVG 1.1,and XSL(XSL-FO) 1.1 Chapter 7.HTTP 1.1,JavaServer Pages 2.1,and Java Servlet 2.5 Chapter 8.XQuery 1.0 and DTD Chapter 9.XML Schema(Second ...