`
niwtsew
  • 浏览: 68510 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

XML Namespaces by Example

阅读更多

refer to http://www.xml.com/pub/a/1999/01/namespaces.html

XML Namespaces by Example

January 19, 1999

 

 

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 .

 

分享到:
评论

相关推荐

    XML 命名空间(XML Namespaces).pdf

    XML 命名空间(XML Namespaces)

    Namespaces

    Namespaces

    XmlSchema标准参考手册.chm

    XML and XML Namespaces XML 和 XML 名称空间 A basic understanding of DTD 对DTD有基本的了解 如果你想先学这些内容,请看我们主页上的的学习教程 -------------------------------------------------------...

    XML轻松学习手册 / XML技术 /

    书籍简介: · 第一章:XML快速入门 ...· 五.Namespaces的语法 · 六.entity的语法 · 七.DTD的语法 · 第五章:XML实例解释 · 一.定义新标识 · 二.建立XML文档 · 三.建立相应的HTML文件 · 第六章:XML相关资源

    XML入门与实例

    注释的语法 · 四.CDATA的语法 · 五.Namespaces的语法 · 六.entity的语法 · 七.DTD的语法 · 第五章:XML实例解释 · 一.定义新标识 · 二.建立XML文档 · 三.建立相应的HTML文件 ·...

    pugixml1.0

    ·比RapidXML功能强很多.比Tiny都强多了.速度和Rapid差不多 ·源代码只有285k 3个文件 ...·Lacks validation, DTD processing, XML namespaces, proper handling of encoding. ·Lacks UTF-16/32 parsing.

    XML轻松学习手册 XML学习

    · 第一章:XML快速入门 ...· 五.Namespaces的语法 · 六.entity的语法 · 七.DTD的语法 · 第五章:XML实例解释 · 一.定义新标识 · 二.建立XML文档 · 三.建立相应的HTML文件 · 第六章:XML相关资源

    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

    XML轻松学习手册--XML肯定是未来的发展趋势,不论是网页设计师还是网络程序员,都应该及时学习和了解

    • 五.Namespaces的语法 • 六.entity的语法 • 七.DTD的语法 • 第五章:XML实例解释 • 一.定义新标识 • 二.建立XML文档 • 三.建立相应的HTML文件 • 第六章:XML相关资源 五. XML和HTML的区别 XML和HTML都...

    Learning.XML.2nd

    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

    [done]Cgroups and Namespaces.pdf

    XML轻松学习手册(chm)

    注释的语法 · 四.CDATA的语法 · 五.Namespaces的语法 · 六.entity的语法 · 七.DTD的语法 · 第五章:XML实例解释 · 一.定义新标识 · 二.建立XML文档 · 三.建立相应的HTML文件 ·...

    Namespaces and cgroups

    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轻松学习手册.rar

    ◎ XML轻松学习手册: ...· 五.Namespaces的语法 · 六.entity的语法 · 七.DTD的语法 · 第五章:XML实例解释 · 一.定义新标识 · 二.建立XML文档 · 三.建立相应的HTML文件 · 第六章:XML相关资源

    Beginning XML with C# 2008: From Novice to Professional

    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 ...

    XML经典教程

    18 XML 命名空间(XML Namespaces )..............................................................40 19 XML CDATA..........................................................................................

    Programming Excel With Vba And .net.chm

    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

    NET 3.5 Namespaces Poster LORES

    XPath,Xlink,XPointer,XML学习手册

    Hypermedia concepts and alternatives to the Web &lt;br&gt;XML Namespaces, XML Base, XInclude, XML Information Set, XHTML, and XSLT &lt;br&gt;XPath, XLink, and XPointer concepts, strengths, and limitations...

    哈佛大学 xml 课程讲义

    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 ...

Global site tag (gtag.js) - Google Analytics