- 浏览: 74218 次
- 性别:
- 来自: 北京
-
文章分类
最新评论
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 619well written. http://samizdat ... -
js module pattern
2013-10-12 16:21 438http://www.adequatelygood.com/ ... -
GZip compressing HTML, JavaScript, CSS etc. makes the data sent to the browser s
2013-07-31 15:48 686this is fun. http://tutorials ... -
java collection matrix
2012-08-07 11:24 783http://www.janeve.me/articles/w ... -
ghost text (aka in-field text)
2012-04-01 11:18 740http://archive.plugins.jquery.c ... -
What is Optimistic Locking vs. Pessimistic Locking
2011-09-09 16:50 865What is Optimistic Locking vs. ... -
what is DAO
2011-04-15 13:42 809http://java.sun.com/blueprints/ ... -
indenting xml in vim with xmllint
2011-01-10 09:48 730I added to my “.vimrc” file: ... -
css sprite
2010-12-15 16:57 712http://css-tricks.com/css-sprit ... -
最牛B 的 Linux Shell 命令
2010-10-30 00:08 745http://hi.baidu.com/hy0kl/blog/ ... -
GPS Bearing VS Heading
2010-10-21 15:40 1695http://gps.about.com/od/glossar ... -
Document Type Declaration
2010-07-19 22:01 861Document Type Declaration h ... -
XML Declaration must be the first line in the document.
2010-06-12 17:54 931The XML declaration typically a ... -
UCM
2010-05-08 11:41 795Two links about UCM The power ... -
What is an MXBean?
2010-01-28 11:10 815refer to http://weblogs.java. ... -
why wait() always in a loop
2010-01-19 00:17 858As we know ,jdk API doc suggest ... -
Locks in Java
2010-01-18 22:48 958copied from http://tutorials.je ... -
use jps instead of ps to find jvm process
2010-01-11 14:21 842copied from http://java.sun.com ... -
My first error of Hello Wolrd Struts
2010-01-04 09:10 887It's my first time to touch Str ... -
Unit Testing Equals and HashCode of Java Beans
2009-12-29 10:07 1334copy from http://blog.cornetdes ...
相关推荐
### Marchal - XML By Example #### 书籍概览与核心知识点 《Marchal - XML By Example》是一本全面介绍XML语言及其应用的书籍。作者Benoît Marchal通过丰富的示例来阐述XML的基础语法、高级特性以及实际应用场景...
XML(可扩展标记语言)是一种用于存储和传输数据的标记语言,它允许用户自定义标记来描述信息。随着XML在不同领域中的应用,数据的集成和共享变得越来越重要。然而,当两个不同的XML文档中使用了相同的元素名称时,...
【Namespaces】是XML文档中一个至关重要的概念,它用于解决元素和属性名称的命名冲突问题,以及在同一个文档中引用不同词汇表的需求。在XML 1.0规范中并没有直接涉及Namespaces的定义,而是通过《Namespaces in XML...
通过本教程的学习,参与者将能够掌握XML的基础知识及一系列重要的衍生技术,包括XML Schema、Namespaces、XPath、XSL 和 XSLT等。此外,教程还重点介绍了如何在Java程序中解析和处理XML文档,特别强调了这些技术在e-...
XML and XML Namespaces XML 和 XML 名称空间 A basic understanding of DTD 对DTD有基本的了解 如果你想先学这些内容,请看我们主页上的的学习教程 -------------------------------------------------------...
理解命名空间(Namespaces)是XML学习的关键,它解决了在同一个文档中使用相同标签名但含义不同的问题。通过使用命名空间,不同的开发者或应用可以共享相同的标签而不产生冲突,例如 `xmlns=...
打开终端,输入`vi example.xml`、`nano example.xml`或`gedit example.xml`。 二、XML语法基础 - **元素(Elements)**:XML文档由元素构成,如`<element>`...`</element>`。 - **属性(Attributes)**:元素可以...
sp_xml_preparedocument的语法要求提供三个参数:@hdoc(内存句柄)、@xmltext(XML文本内容)和@xmlnamespaces(XML数据所需的名字空间索引)。其中,@hdoc是一个输出变量,用于存放XML文件内容在内存中的地址指针...
6. **命名空间(Namespaces)**:XML命名空间用于区分具有相同标签名的不同来源的数据。例如: ```xml <ns:element xmlns:ns="http://example.com/ns">...</ns:element> ``` 7. **注释(Comments)**:XML支持注释...
XML的命名空间(Namespaces)是一个重要的特性,它允许在相同的文档中使用来自不同来源的元素和属性,通过前缀来区分,如`xmlns:books="http://example.com/books"`。这在整合来自多个数据源的数据时非常有用。 XML...
此外,我们还将讨论XML文档的基本结构,包括文档声明(Document Type Declaration, DTD)和XML命名空间(XML namespaces),这些都是确保XML文档规范的关键元素。 习题一: 这部分的练习旨在帮助读者熟悉XML文档的...
书籍简介: · 第一章:XML快速入门 ...· 五.Namespaces的语法 · 六.entity的语法 · 七.DTD的语法 · 第五章:XML实例解释 · 一.定义新标识 · 二.建立XML文档 · 三.建立相应的HTML文件 · 第六章:XML相关资源
在学习过程中,你还将了解到XML命名空间(Namespaces)、DTD(Document Type Definition)或XML Schema等概念,这些都是确保XML文档结构规范的关键。最后,理解XML的验证过程也很重要,这可以通过DTD或XML Schema...
2. **命名空间(Namespaces)**:XML命名空间用于区分相同名称的元素,通过前缀与URI(统一资源标识符)结合实现。例如,`<ns:element xmlns:ns="http://example.com/ns">`,这里的`ns`是前缀,`...
- Minixml不支持XML命名空间(XML Namespaces),所以在处理包含命名空间的XML时,需要自行处理。 - 由于Minixml库功能相对简单,它不包含XML验证或错误修复机制。因此,确保输入的XML文档格式正确是必要的。 - ...
XML命名空间(XML Namespaces)是解决元素名称冲突的一个机制,它允许在同一个文档中使用相同名称但来自不同源的元素。这通常通过使用URI(统一资源标识符)来区分不同的命名空间。 DOM(Document Object Model)是...
三、XML命名空间(Namespaces) 在处理多个数据源时,为了避免元素名称冲突,XML引入了命名空间的概念。通过使用`xmlns`属性,可以为元素指定一个URI(Uniform Resource Identifier),如 `<element xmlns="http://...
XML文档由元素(Elements)、属性(Attributes)、文本内容(Text Content)和命名空间(Namespaces)等组成。元素是XML文档的核心,它们可以嵌套并携带属性。例如: ```xml <author>Scott Meyers <year>1998 `...
五、XML命名空间(Namespaces) 在处理多个XML文档或库时,可能会出现元素名称冲突的情况。通过使用命名空间,我们可以给元素添加独特的前缀来避免这种冲突。例如: ```xml <book xmlns:myBooks="http://www.example...