代码改变世界

XML经典教程

2007-08-30 11:16  Jacky_Xu  阅读(6746)  评论(0编辑  收藏  举报

XML教程

XML 代表可扩展标记语言。
在这份XML教程中你将了解XML是什么,以及它和HTML的区别。
你还会学到怎样在你的应用软件中开始使用XML

XML实例

通过实例学习XML,通过我们的编辑器,您可以随时查看效果。

快来尝试下吧

教程内容一览

XML基础知识

XML介绍
讲述了什么是XML,以及它与HTML的不同。

XML的用途
介绍了一些可应用XML技术的领域。

XML语法
XML的语法虽然严格但又非常的简单。

XML元素
介绍了XML元素,它们的关联,内容以及命名的规则。

XML属性
讲述了XML的属性是如何来描述元素的,或是给相关的元素提供补充信息。

XML的确认
格式标准的XML文档和有效的XML文档有怎样的区别,DTD又是怎样来定义XML文档的

XML校验器
一个简单的在线工具,来校验XML是否有效

XML与浏览器
支持XML的浏览起有那些?

在IE中查看XML
怎样用IE来查看XML文件?

通过CSS来修饰XML
怎样使用CSS来修饰XML文件?

通过XSL来修饰XML
怎样使用XSL来修饰XML文件?

在HTML中嵌入XML
怎样在HTML里嵌入XML文档?

Microsoft XML 解析器
怎样使用XML解析器来打开并操作XML文档?

XML在实际生活中的应用
观察一些在实际生活中应用到XML的地方

XML 深入篇

XML 命名空间
怎样利用XML命名空间来避免元素名称的冲突

XML CDATA
它可以让XML解析器跳过某些文字的解析

XML 编码格式
怎样为你的XML指定编码格式?

XML Server
怎样在服务器上生成XML?

XML 应用程序
怎样在IE平台上建立一个完整的XML应用程序

XML HTTP 请求
通过使用XMLHttpRequest对象,WEB开发人员可以将来自服务器的数据调用到另一张页面的内容里

将数据保存到XML文件里
通常我们会把数据保存到数据库里,但如果我们需要让数据更加轻便的话我们可以把它们存放到XML文件里去.

针对HTML以及XML的行为
怎样让CSS行为选择器建立动态内容

XML 技术
与XML相关联的技术

XML 编辑器
当你在编辑XML文档,应该选择一款优秀的编辑器

XML 摘要
回顾所学内容并给进一步学习提供一些推荐


XML 介绍
w3pop.com / 2006-09-18

XML was designed to describe data and to focus on what data is.
XML用于描述数据,侧重于数据本身(如:数据类型和数据结构)的定义。

HTML was designed to display data and to focus on how data looks.
HTML用于显示数据,侧重于数据的显示形式。


What You Should Already Know
你应具备的知识

Before you continue you should have a basic understanding of the following:
在继续下面的学习前,你必须对以下内容有基本认识:

  • HTML / XHTML
  • JavaScript 或 VBScript

If you want to study these subjects first, find the tutorials on our Home page.
如果你想先学习上述内容,请参照知识库的教程。


What is XML?
什么是XML?

  • XML stands for EXtensible Markup Language
    XML代表EXtensible Markup Language(可扩展标记语言)
  • XML is a markup language much like HTML
    XML是一种标记语言,这点与HTML非常相似
  • XML was designed to describe data
    XML是用于定义数据的
  • XML tags are not predefined. You must define your own tags
    XML标记不是预先定义的,你bixu自己定义标记
  • XML uses a Document Type Definition (DTD) or an XML Schema to describe the data
    XML使用DTD(Document Type Definition)或XMLSchema来描述数据
  • XML with a DTD or XML Schema is designed to be self-descriptive
    带有DTD或XMLSchema的XML具有“自我描述[self-descriptive]”的特点。
  • XML is a W3C Recommendation
    XML是W3C推荐使用的标准

XML is a W3C Recommendation
XML是W3C推荐使用的标准

The Extensible Markup Language (XML) became a W3C Recommendation 10. February 1998.
1998年2月10日, Extensible Markup Language (XML)成为W3C推荐使用的标准。

You can read more about XML standards in our W3C tutorial.
W3C教程中你能看到更多关于XML的标准


The Main Difference Between XML and HTML
XML和HTML的主要差别

XML was designed to carry data.
XML用于承载数据

XML is not a replacement for HTML.
XML并不能当做HTML的替代品。
XML and HTML were designed with different goals
XML和HTML的作用不同:

XML was designed to describe data and to focus on what data is
XML用于描述数据,侧重于数据本身(如:数据类型和数据结构)的定义.
HTML was designed to display data and to focus on how data looks
HTML用于显示数据,侧重于数据的显示形式.

HTML is about displaying information, while XML is about describing information.
HTML侧重于显示信息,而XML则侧重于描述信息.


XML Does not DO Anything
XML是“不干任何事情的”

XML was not designed to DO anything.
XML是不用于”干事”的。

Maybe it is a little hard to understand, but XML does not DO anything. XML was created to structure, store and to send information.
也许这么评价XML有点难义理解,但XML确实不干任何事,XML只是用来架构信息、储存信息以及传送信息的。

The following example is a note to Tove from Jani, stored as XML:
下面的例子是以XML形式书写的一张Tove 给Jani的便条:

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>
</note>

 

The note has a header and a message body. It also has sender and receiver information. But still, this XML document does not DO anything. It is just pure information wrapped in XML tags. Someone must write a piece of software to send, receive or display it.
这份便条包含标题[header]和信息内容[body],还包含发出者和接受者的相关信息。但在这样强调的是:XML文件并未作任何处理。它只是将信息包裹在XML标记里。别人必须书写一个指令才能发送、接受或显示它。


XML is Free and Extensible
XML可自由扩展

XML tags are not predefined. You must "invent" your own tags.
XML标签不是预先定义的,你必须得自己“创造”标签。

The tags used to mark up HTML documents and the structure of HTML documents are predefined. The author of HTML documents can only use tags that are defined in the HTML standard (like <p>, <h1>, etc.).
HTML文档标签和HTML文档结构是预先定义好的,如果你需要对HTML文件进行编写,你只能使用HTML中已经定义好的标记(像<p>, <h1>,等等).。

XML allows the author to define his own tags and his own document structure.
XML允许编者自定义标签和文档结构.

The tags in the example above (like <to> and <from>) are not defined in any XML standard. These tags are "invented" by the author of the XML document.
上述例子中使用的标签(像<to> 和<from>)并未在任何XML标准中定义,这些标签是XML文档编者自己“创造”的.


XML is a Complement to HTML
XML是HTML的补充

XML is not a replacement for HTML
XML不能当作HTML的替代品.

It is important to understand that XML is not a replacement for HTML. In future Web development it is most likely that XML will be used to describe the data, while HTML will be used to format and display the same data.
这里有一点非常重要:XML不是HTML的替代品。即使在将来的互联网中,XML也是很可能只用于定义数据,而HTML则用于格式化数据和显示数据.

My best description of XML is this: XML is a cross-platform, software and hardware independent tool for transmitting information.
我们对XML最好的解释是:XML是一个交互式平台[cross-platform],它是一种独立于软硬件的数据传输工具。


XML in Future Web Development
XML在未来网络中的发展

XML is going to be everywhere
XML发展到后来将随处可见。

We have been participating in XML development since its creation. It has been amazing to see how quickly the XML standard has been developed and how quickly a large number of software vendors have adopted the standard.
自XML诞生以来,我们就已经参与到它的发展过程之中。XML标准如此的发展,并迅速地被大批的软件卖家所接纳和使用,说起来真是让人难以想象。

We strongly believe that XML will be as important to the future of the Web as HTML has been to the foundation of the Web and that XML will be the most common tool for all data manipulation and data transmission.
我们坚信XML对未来网络的重要性决不亚于当初HTML对网络架构的重要性,并且,XML将成为数据处理和数据传输的最普遍工具。


XML Joke
关于XML的笑话

Question: When should I use XML?
问:我什么时候该用XML呢?

Answer: When you need a buzzword in your resume.
答:当你想在履历表中强调某些词的时候~.

XML 的用途
w3pop.com / 2006-09-18

It is important to understand that XML was designed to store, carry, and exchange data. XML was not designed to display data.
要明白的很重要的一点是:XML是用来储存,传送,交换数据的,而不是用来显示数据的


XML can Separate Data from HTML
XML可以从HTML中分离出数据

With XML, your data is stored outside your HTML.
通过使用XML,数据在HTML之外被储存起来

When HTML is used to display data, the data is stored inside your HTML. With XML, data can be stored in separate XML files. This way you can concentrate on using HTML for data layout and display, and be sure that changes in the underlying data will not require any changes to your HTML.
用HTML形式 显示数据时,数据存储在HTML中。用XML形式时,数据可以储存在其 它的单独的XML文件中。在种情况下你就不用担心由于潜在数据的改变会改变HTML ,从而可以一心一意的用HTML进行数据的排版和显示

XML data can also be stored inside HTML pages as "Data Islands". You can still concentrate on using HTML only for formatting and displaying the data.
XML数据可以作为“数据岛”(Data Islands)储存在HTML页面中。你仍可以专注于只用HTML进行数据的格式化和显示.


XML is Used to Exchange Data
XML用来交换数据

With XML, data can be exchanged between incompatible systems
用XML形式,数据可以在不兼容的系统中进行交换.

In the real world, computer systems and databases contain data in incompatible formats. One of the most time-consuming challenges for developers has been to exchange data between such systems over the Internet
现实中,计算机系统和数据库都包含着不兼容格式的数据。开发者们面临的最费时的挑战之一就是如何在与互联网不兼容的系统中交换数据.

Converting the data to XML can greatly reduce this complexity and create data that can be read by many different types of applications.
将数据转换成XML形式可以极大的减少以上的复杂性,而且造出的数据可以被很多不同种应用软件读出.


XML and B2B
XML 与 B2B

With XML, financial information can be exchanged over the Internet.
用XML,可以在英特网上交换金融信息.

Expect to see a lot about XML and B2B (Business To Business) in the near future.
我们期望在不久将来看到许多关于XML和 B2B (Business To Business企业对企业) 的东西.

XML is going to be the main language for exchanging financial information between businesses over the Internet. A lot of interesting B2B applications are under development.
XML将成为商务界在英特网上交换金融信息的主要语言。大量有趣的B2B应用软件正在开发之中.


XML Can be Used to Share Data
XML可用于数据共享

With XML, plain text files can be used to share data.
用XML,普通的文本文件可用来共享数据.

Since XML data is stored in plain text format, XML provides a software- and hardware-independent way of sharing data.
因为XML数据以普通文本格式储存,XML用“软硬件分离”(software- and hardware-independent)的方式共享数据.

This makes it much easier to create data that different applications can work with. It also makes it easier to expand or upgrade a system to new operating systems, servers, applications, and new browsers. 
这样可容易地制造出能被不同应用软件处理的数据。这样系统也可容易地扩展或升级为新的操作系统、服务器、应用软件和新的浏览器等;


XML Can be Used to Store Data
XML可用于存储数据

With XML, plain text files can be used to store data.
用XML。普通文本文件可以储存数据.

XML can also be used to store data in files or in databases. Applications can be written to store and retrieve information from the store, and generic applications can be used to display the data.
XML可以在文件或数据库中储存数据。应用软件可从储备中找回,储存信息。一般的应用软件都可以展示数据


XML Can Make your Data More Useful
XML可让你的数据更有用

With XML, your data is available to more users.
通过使用XML可以让更多的使用者利用数据.

Since XML is independent of hardware, software and application, you can make your data available to other than only standard HTML browsers.
因为XML与硬件,软件,应用软件独立,你能让数据被更多人得到,而不只限于标准的HTML浏览者.

Other clients and applications can access your XML files as data sources, like they are accessing databases. Your data can be made available to all kinds of "reading machines" (agents), and it is easier to make your data available for blind people, or people with other disabilities.
其它的客户和应用软件可以把你的XML文件作为数据资源进行访问,就如同他们取得数据库资源一样。所有种类的阅读器reading machines(代理工具agents)都可取得你的数据,盲人,或其他残疾人也能容易地取得数据.


XML Can be Used to Create New Languages
XML可以创造新的语言

XML is the mother of WAP and WML.
XML 是WAP和WML之“母”.

The Wireless Markup Language (WML), used to markup Internet applications for handheld devices like mobile phones, is written in XML.
无线标记语言The Wireless Markup Language (WML),用来为象手机之类的手握部件进行英特网应用软件的标记,它就是用XML写成的

You can read more about WML in our WML tutorial.
在我们的WML教程里你可以了解更多


If Developers Have Sense
如果开发者们是具有理性思维的话

If they DO have sense, all future applications will exchange their data in XML.
如果他们非常具有理性思维的话,未来所有的应用软件都将以XML形式进行数据交换了.

The future might give us word processors, spreadsheet applications and databases that can read each other's data in a pure text format, without any conversion utilities in between.
未来也许会给我们这样的单词处理器、电子表应用软件、数据库。用它们文本格式就可以读到相互间的数据,而不需要进行有效地转换.

We can only pray that Microsoft and all the other software vendors will agree.
我们只能祈盼微软和别的软件卖家会同意喽.

XML 语法规则
w3pop.com / 2006-09-19

The syntax rules of XML are very simple and very strict. The rules are very easy to learn, and very easy to use.
XML的语法规则是非常简单而又非常严格的;它非常易于学习,也非常易于使用。

Because of this, creating software that can read and manipulate XML is very easy.
因此,开发能够识别和处理XML的软件也非常的容易。


An Example XML Document
XML文档样文

XML documents use a self-describing and simple syntax.
XML文档使用自述性质的简单的语法.

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>

<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

The first line in the document - the XML declaration - defines the XML version and the character encoding used in the document. In this case the document conforms to the 1.0 specification of XML and uses the ISO-8859-1 (Latin-1/West European) character set.
文档的第一行 — XML声明 — 定义XML的版本和文档所用字体的编码,这个例子里文档使用XML1.0版,设置的字体编号是ISO-8859-1 (Latin-1/West European)

The next line describes the root element of the document (like it was saying: "this document is a note"):
第二行描述了文档的根元素 (像它说的那样: "this document is a note"):

<note>

 

The next 4 lines describe 4 child elements of the root (to, from, heading, and body):
接下来4行描述了根元素的4个子元素 (to, from, heading, 和 body):

<to>Tove</to>

<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>

 

And finally the last line defines the end of the root element:
最后一行定义了根元素的结尾:

</note>

 

Can you detect from this example that the XML document contains a Note to Tove from Jani? Don't you agree that XML is pretty self-descriptive?
你能从这例子中发现其实这份XML文档包含了一个Jani给Tove的便条(Note)吗?你不认为XML是非常具有自我描述性质的语言吗?


All XML Elements Must Have a Closing Tag
所有的XML元素必须有一个结束标签

With XML, it is illegal to omit the closing tag.
XML中,省略结束标签是不正确的

In HTML some elements do not have to have a closing tag. The following code is legal in HTML:
在HTML中一些元素是不必一定得有结束标签的.下面的代码在HTML中是合法的:

<p>This is a paragraph
<p>This is another paragraph

In XML all elements must have a closing tag, like this:
XML中所有的元素都必须有结束标签,像这样:

<p>This is a paragraph</p>

<p>This is another paragraph</p> 

Note: You might have noticed from the previous example that the XML declaration did not have a closing tag. This is not an error. The declaration is not a part of the XML document itself. It is not an XML element, and it should not have a closing tag.
注意:
:也许你在前面的例子里已经注意到,XML声明没有结束标签.这并不是个错误.声明并不是XML文档本身的一部分.声明不是一个XML元素,所以它不该有结束标签.


XML Tags are Case Sensitive
XML标签区分大小写

Unlike HTML, XML tags are case sensitive.
这点和HTML不一样,XML标签是区分大小写的

With XML, the tag <Letter> is different from the tag <letter>.
在XML中,标签<letter>和 <letter>.是不同的.</letter></letter>

Opening and closing tags must therefore be written with the same case:
因此始标签与末标签必须一律用大写字母或一律用小写字母.

<Message>This is incorrect</message>

<message>This is correct</message>


XML Elements Must be Properly Nested
XML元素必须嵌套合理

Improper nesting of tags makes no sense to XML.
不合理的标签嵌套对XML来说毫无意义.

In HTML some elements can be improperly nested within each other like this:
在HTML中某些元素相互间可以被不合理的欠套,就像这样:

<b><i>This text is bold and italic</b></i>

In XML all elements must be properly nested within each other like this:
在XML中所有元素都必须像这样被合理的嵌套:

<b><i>This text is bold and italic</i></b>


XML Documents Must Have a Root Element
XML文档必须有一个根元素

All XML documents must contain a single tag pair to define a root element.
所有的XML文档必须包含一对单一的标签来定义一个根元素

All other elements must be within this root element.
所有其他的元素必须包含在这个根元素中

All elements can have sub elements (child elements). Sub elements must be correctly nested within their parent element:
所有元素都可以有副元素(子元素).副元素必须在它们的父元素里面被正确的嵌套

<root>

<child>
<subchild>.....</subchild>
</child>
</root>


XML Attribute Values Must be Quoted
XML属性值必须被引号引起来

With XML, it is illegal to omit quotation marks around attribute values.
XML中,省略属性值外面的引号是不正确的

XML elements can have attributes in name/value pairs just like in HTML. In XML the attribute value must always be quoted. Study the two XML documents below. The first one is incorrect, the second is correct:
XML元素可以有像在HTML里的那些“属性名/值”这样成对的属性.在XML里属性值必须引在引号里.看看下面这两个XML文档.第一个是错误的,第二个是正确的:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note date=12/11/2002>

<to>Tove</to>
<from>Jani</from>
</note>

<?xml version="1.0" encoding="ISO-8859-1"?>

<note date="12/11/2002">
<to>Tove</to>
<from>Jani</from>
</note>

The error in the first document is that the date attribute in the note element is not quoted.
第一份文档的错误是便条元素里的日期属性未被引在引号里

This is correct: date="12/11/2002". This is incorrect: date=12/11/2002.
这是正确的: date="12/11/2002". 这是错误的: date=12/11/2002.


With XML, White Space is Preserved
在 XML里,空格是被保留的.

With XML, the white space in your document is not truncated.
在XML里,你的文档里的空格是不会被删除的
.

This is unlike HTML. With HTML, a sentence like this:
这点和HTML不同.在HTML中,象这样的句子:

Hello              my name is Tove,

will be displayed like this:
会被显示成这样:

Hello my name is Tove,

because HTML reduces multiple, consecutive white space characters to a single white space.
因为HTML会缩减倍数,连续的空格字符会被减少为一个空格符


With XML, CR / LF is Converted to LF
在XML中, CR / LF 会被修改为 LF

With XML, a new line is always stored as LF.
在XML中,新的一行会作为LF储存起来
.

Do you know what a typewriter is? Well, a typewriter is a mechanical device which was used last century to produce printed documents. :-)
你知道打字机是什么吗?打字机是上世纪被用于制造打印文件的机器装置

After you have typed one line of text on a typewriter, you have to manually return the printing carriage to the left margin position and manually feed the paper up one line.
在打字机上打完一行文本后,你得手工地把打印架推回左边的页面空白处,而且手工地把纸调高一行

In Windows applications, a new line is normally stored as a pair of characters: carriage return (CR) and line feed (LF). The character pair bears some resemblance to the typewriter actions of setting a new line. In Unix applications, a new line is normally stored as a LF character. Macintosh applications use only a CR character to store a new line.
在Windows应用软件中,新的一行通常被储存为一对字符:打印架推回(carriage return 简称CR),新行补给(line feed简称 LF).字符对和打字机设置新行时的系列动作有相似之处,在Unix应用软件中,新行通常被储存为”LF”字符, Macintosh应用软件只用”CR”字符来储存新行.


Comments in XML
XML中的注释

The syntax for writing comments in XML is similar to that of HTML.
XML注释的写法和HTML类似

<!-- This is a comment -->


There is Nothing Special About XML
XML没有什么特别的

There is nothing special about XML. It is just plain text with the addition of some XML tags enclosed in angle brackets.
XML没有什么特别的,它只是普通文本,此外包含着些被关在角型括号的XML标签.

Software that can handle plain text can also handle XML. In a simple text editor, the XML tags will be visible and will not be handled specially.
能处理普通文本的软件同样能处理XML文件.在一个简易的文本编辑器里,XML标签是可见的,而且不会被特殊地处理

In an XML-aware application however, the XML tags can be handled specially. The tags may or may not be visible, or have a functional meaning, depending on the nature of the application.
然而在能识别XML的应用软件里,XML标签可以被特殊处理.标签可以是可见的,也可以是不可不见,或者可能含有一种功能性的含义,这得取决于应用软件的性质.

XML 元素
w3pop.com / 2006-09-19

XML Elements are extensible and they have relationships.
XML元素是可扩展的并且它们是互相关联的

 

XML Elements have simple naming rules.
XML元素有简单的命名规则


XML Elements are Extensible
XML元素是可扩展的

XML documents can be extended to carry more information.
XML文档可以通过扩展来携带更多信息

Look at the following XML NOTE example:
请看下面的XML形式便条(Note)的例子

<note>
<to>Tove</to>
<from>Jani</from>

<body>Don't forget me this weekend!</body>
</note>

Let's imagine that we created an application that extracted the <to>, <from>, and <body> elements from the XML document to produce this output:
假设我们已经创造了一个应用软件,用它从XML文档中获取<to>, <from>, 和 <body>元素,产出这样的结果:

MESSAGE

To: Tove
From: Jani

Don't forget me this weekend!

Imagine that the author of the XML document added some extra information to it:
假设XML文档的作者又添加了一些信息:

<note>

<date>2002-08-01</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>

</note>

Should the application break or crash?
应用程序是会被终止还是被破坏?

No. The application should still be able to find the <to>, <from>, and <body> elements in the XML document and produce the same output.
不!应用程序仍会获取XML文档中的<to>, <from>,和 <body>元素,产出同样的输出。

XML documents are Extensible.
XML文档是可扩展的。


XML Elements have Relationships
XML元素是互相关联的

Elements are related as parents and children.
元素们的关系就像父母与子女一样

To understand XML terminology, you have to know how relationships between XML elements are named, and how element content is described.
想要了解XML的术语,你必须得了解怎样命名XML元素之间的关系,怎样描述元素所含的内容。

Imagine that this is a description of a book:
假设下面的是对一本书的描述:

My First XML

Introduction to XML

  • What is HTML
  • What is XML

XML Syntax

  • Elements must have a closing tag
  • Elements must be properly nested

Imagine that this XML document describes the book:
假设下面的XML文档具体描述了这本书:

<book>
<title>My First XML</title>

<prod id="33-657" media="paper"></prod>
<chapter>Introduction to XML
<para>What is HTML</para>
<para>What is XML</para>

</chapter>

<chapter>XML Syntax
<para>Elements must have a closing tag</para>
<para>Elements must be properly nested</para>
</chapter>

</book>

 

Book is the root element. Title, prod, and chapter are child elements of book. Book is the parent element of title, prod, and chapter. Title, prod, and chapter are siblings (or sister elements) because they have the same parent.
Book 是根元素。Title, prod, 和 chapter 是书的子元素。Book是title, prod, 和 chapter的父元素。Title, prod, 和 chapter 是“同胞手足”(或姐妹元素)因为他们拥有相同的父类(parent),因为它们有相同的“父亲”


Elements have Content
元素里有内容

Elements can have different content types.
元素可以有不同的内容类型

An XML element is everything from (including) the element's start tag to (including) the element's end tag.
一个XML元素是元素始标签和末标签之间的一切内容(始末标签也算在内)

An element can have element content, mixed content, simple content, or empty content. An element can also have attributes.
一个元素可以含有元素内容,混合内容,简易内容,甚至空内容。一个元素也有属性

In the example above, book has element content, because it contains other elements. Chapter has mixed content because it contains both text and other elements. Para has simple content (or text content) because it contains only text. Prod has empty content, because it carries no information.
上述例子中,book含有元素内容,因为它包含着其他元素。Chapter拥有混合内容因为它既有文本又有其他元素。Para拥有家简易内容(或者文本内容),因为它只含有文本。Prod 有空内容,因为它没有携带信息。

In the example above only the prod element has attributes. The attribute named id has the value "33-657". The attribute named media has the value "paper". 
上述例子中只有prod元素有属性。ID属性值为"33-657"。Media属性值为"paper".


Element Naming
元素命名

XML elements must follow these naming rules:

  • Names can contain letters, numbers, and other characters
    名称可以含有字母,数字,和其他字符。
  • Names must not start with a number or punctuation character
    名称不能以数字或标点字符开头
  • Names must not start with the letters xml (or XML, or Xml, etc)
    名称不能以xml(或XML,或Xml等)字母开头
  • Names cannot contain spaces
    名称不能含有空格

Take care when you "invent" element names and follow these simple rules:
为元素命名时请注意要符合以下这些简单的规则:

Any name can be used, no words are reserved, but the idea is to make names descriptive. Names with an underscore separator are nice.
几乎所有名称都可被使用,但命名的方式得让名称具有描述性质。名称间用下划线分割的方法值得推荐,如下:

Examples: <first_name>, <last_name>.
例子:<first_name>, <last_name>.

Avoid "-" and "." in names. For example, if you name something "first-name," it could be a mess if your software tries to subtract name from first. Or if you name something "first.name," your software may think that "name" is a property of the object "first."
避免在名称中出现"-" 和"."。举个例子,如果你取名称为"first-name," 软件会试着从first中减去name,这结果会造成运行混乱。如果你取名称为“first.name”,软件可能会认为"name"是对象"first."的属性。

Element names can be as long as you like, but don't exaggerate. Names should be short and simple, like this: <book_title> not like this: <the_title_of_the_book>. 
元素名称想取多长都可以,但不要太夸张了。名称应该简短,像这样:<book_title>而不是这样:<the_title_of_the_book>

XML documents often have a corresponding database, in which fields exist corresponding to elements in the XML document. A good practice is to use the naming rules of your database for the elements in the XML documents.
XML文档常有相应的数据库,数据库是与XML文档的元素相符的。给XML文档元素取名时用上数据库里的命名规则,这是个很好的习惯。

Non-English letters like éòá are perfectly legal in XML element names, but watch out for problems if your software vendor doesn't support them.
非英文字母如éòá在XML元素名称中是完全合法的,但如果你的软件不支持它们的话,可要注意也许会出问题哦。

The ":" should not be used in element names because it is reserved to be used for something called namespaces (more later).
元素名称中不应用":"因为它已经为“名称空间(namespaces)”的使用而专门预留出来了(更多的内容在后头)。

XML 属性
w3pop.com / 2006-09-19

XML elements can have attributes in the start tag, just like HTML.
XML 元素可以像HTML一样在开始标签(start tag)里书写属性

Attributes are used to provide additional information about elements.
属性是用来提供关于元素的附加信息的。


XML Attributes
XML属性

XML elements can have attributes.
XML 元素可以有属性

From HTML you will remember this: <IMG SRC="computer.gif">. The SRC attribute provides additional information about the IMG element.
在HTML教程中,你一定还记得这个:<IMG SRC="computer.gif">, SRC 属性提供关于IMG元素的相关附加信息的。

In HTML (and in XML) attributes provide additional information about elements:
在HTML(还有XML)的属性是提供关于元素的相关附加信息的。

<img src="computer.gif">
<a href="demo.asp">

 

Attributes often provide information that is not a part of the data. In the example below, the file type is irrelevant to the data, but important to the software that wants to manipulate the element:
属性常提供数据部分以外的信息,在下面的例子里,文本类型与数据不相关,但对处理元素的软件很重要:

<file type="gif">computer.gif</file>


Quote Styles, "female" or 'female'?
引号类型,"female" 还是 'female'?

Attribute values must always be enclosed in quotes, but either single or double quotes can be used. For a person's sex, the person tag can be written like this:
属性值必须附在引号里表示,单引和双引都可以用 。以某人性别而言,人物标签可以这样书写

<person sex="female">

or like this:
或者这样

<person sex='female'>

Note: If the attribute value itself contains double quotes it is necessary to use single quotes, like in this example:
注意:
如果属性值本身含有双引号,那就必须用上单引号,比如在下面的例子里:

<gangster name='George "Shotgun" Ziegler'>

Note: If the attribute value itself contains single quotes it is necessary to use double quotes, like in this example:
注意:
如果属性值本身含有单引号,那就必须用上双引号,比如在下面的例子里:

<gangster name="George 'Shotgun' Ziegler">


Use of Elements vs. Attributes
元素和属性的使用比较

Data can be stored in child elements or in attributes.
数据能以子元素或属性的形式储存起来

Take a look at these examples:
请看以下的一些例子:

<person sex="female">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>

<person>
<sex>female</sex>
<firstname>Anna</firstname>
<lastname>Smith</lastname>

</person>

In the first example sex is an attribute. In the last, sex is a child element. Both examples provide the same information.
第一个例子里性别是一个属性。在最后的例子里,性别是一个子元素。两个例子表达的意思一样

There are no rules about when to use attributes, and when to use child elements. My experience is that attributes are handy in HTML, but in XML you should try to avoid them. Use child elements if the information feels like data.
何时用属性或何时用子元素没有规定。我的经验是在HTML里属性容易找到,但在XML里你应该用属性。如果信息象数据,就用子元素


My Favorite Way
我最喜欢的方法

I like to store data in child elements.
我喜欢用子元素形式储存数据

The following three XML documents contain exactly the same information:
以下三份XML文件都含有相同的信息:

A date attribute is used in the first example:
第一个例子里用到了日期属性

<note date="12/11/2002">
<to>Tove</to>
<from>Jani</from>

<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

A date element is used in the second example:
第二个例子里用到了日期元素

<note>
<date>12/11/2002</date>

<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

An expanded date element is used in the third: (THIS IS MY FAVORITE):
第三个例子用了扩展日期元素(这是我们常用的方法):

<note>
<date>
<day>12</day>
<month>11</month>

<year>2002</year>
</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>
</note>


Avoid using attributes?
避免使用属性?

Should you avoid using attributes?
你应该避免使用属性吗?

Some of the problems with using attributes are:
使用属性的一些问题有:

  • attributes cannot contain multiple values (child elements can)
    属性不能含有多重值(子元素可以)
  • attributes are not easily expandable (for future changes)
    属性不易扩展(这里说的可扩展性是针对将来的属性变化来讲的)
  • attributes cannot describe structures (child elements can)
    属性不能描述结构(如果是子元素的话可以描述结构)
  • attributes are more difficult to manipulate by program code
    属性比较难被程序码快速处理
  • attribute values are not easy to test against a Document Type Definition (DTD) - which is used to define the legal elements of an XML document
    属性值易被文本类型定义[Document Type Definition (DTD)]测试,DTD是用来详细说明XML文件合法元素的

If you use attributes as containers for data, you end up with documents that are difficult to read and maintain. Try to use elements to describe data. Use attributes only to provide information that is not relevant to the data.
如果你用属性储存数据,最后文件将难于阅读和持续的使用。试用元素描述数据。只有在提供与数据无关的信息时才使用该项属性

Don't end up like this (this is not how XML should be used):
不要向这样结尾(XML不应该这么用)

<note day="12" month="11" year="2002"

to="Tove" from="Jani" heading="Reminder"
body="Don't forget me this weekend!">
</note>


An Exception to my Attribute Rule
我的属性使用规则之例外

Rules always have exceptions.
规则常有例外

My rule about attributes has one exception:
我的属性使用规则有一例外

Sometimes I assign ID references to elements. These ID references can be used to access XML elements in much the same way as the NAME or ID attributes in HTML. This example demonstrates this:
有时我把ID参数 赋值给元素。这些ID参数可用来访问接近XML元素,和HTML的NAME 或ID 属性存取XML元素的方式大致相同。以下的例子可以说明:

<messages>

<note id="p501">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>
</note>

<note id="p502">
<to>Jani</to>

<from>Tove</from>
<heading>Re: Reminder</heading>
<body>I will not!</body>
</note>

</messages>

The ID in these examples is just a counter, or a unique identifier, to identify the different notes in the XML file, and not a part of the note data.
这些例子中ID只是一个“储存器”,或一个特别的“识别员”,识别XML文件中的不同信息,它们不是信息数据的一部分

What I am trying to say here is that metadata (data about data) should be stored as attributes, and that data itself should be stored as elements.
我在这想着重强调的是metadata(元数据)(data about data)应该以属性方式储存起来,而数据本身应以元素储存。

XML 的有效性验证
w3pop.com / 2006-09-19

XML with correct syntax is Well Formed XML.
正确的语法使XML格式标准

XML validated against a DTD is Valid XML.
经过DTD验证的XML是有效的XML


Well Formed XML Documents
格式标准的XML文件

A "Well Formed" XML document has correct XML syntax.
使用正确语法规则的XML文档可以称其格式标准

A "Well Formed" XML document is a document that conforms to the XML syntax rules that were described in the previous chapters:
XML文件应遵照前几章里提过的XML语法规则才能算得上格式标准

  • XML documents must have a root element
    XML文件必须有根元素
  • XML elements must have a closing tag
    XML元素必须有结束标签
  • XML tags are case sensitive
    XML标签区分大小写
  • XML elements must be properly nested
    XML元素必须嵌套合理
  • XML attribute values must always be quoted
    XML属性值必须被标上引号
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>

<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


Valid XML Documents
有效的XML文件

A "Valid" XML document also conforms to a DTD.
一份“有效”的XML文件还得符合DTD规则

A "Valid" XML document is a "Well Formed" XML document, which also conforms to the rules of a Document Type Definition (DTD):
一份“有效”的XML文件是格式标准的,同时还要符合DTD的规则

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE note SYSTEM "InternalNote.dtd">
<note>
<to>Tove</to>

<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


XML DTD


A DTD defines the legal elements of an XML document.
DTD详细定义了XML文件的合法元素。


The purpose of a DTD is to define the legal building blocks of an XML document. It defines the document structure with a list of legal elements. You can read more about DTD, and how to validate your XML documents in our DTD tutorial.
DTD的目的就是详细定义一份XML文件的合法组成区。它定义了一系列合法元素。在我们的DTD教程中,你可以了解更多关于DTD的内容,以及怎样验证你的XML文件。


XML Schema 
XML计划(Schema)

XML Schema is an XML based alternative to DTD.
XML计划(Schema)是对于DTD的XML基础备份选择 

W3C supports an alternative to DTD called XML Schema. You can read more about XML Schema in our Schema tutorial.
W3C支持对于DTD的XML计划选择。在计划指南中你能阅读到关于XML计划的更多内容。


A General XML Validator
通用型校验器

To help you check the syntax of your xml files, we have created this link so that you can syntax-check any XML file.
为了帮你检查XML文件的语法,我们设了这个链接可能对你有所帮助。

XML 校验器
w3pop.com / 2006-09-19

XML Errors will Stop you
XML错误会阻挠你

Errors in XML documents will stop your XML program.
XML文档中的错误会阻挠你的XML程序

The W3C XML specification states that a program should not continue to process an XML document if it finds an error. The reason is that XML software should be easy to write, and that all XML documents should be compatible.
W3C XML 说明中提到:如果发现错误,程序应停止处理XML文件。这是因为XML软件应易于书写,并且所有的XML文件都应是兼容的。

With HTML it was possible to create documents with lots of errors (like when you forget an end tag). One of the main reasons that HTML browsers are so big and incompatible, is that they have their own ways to figure out what a document should look like when they encounter an HTML error.
用HTML形式时,在文件中很容易产生大量错误(比如你忘了写结束标记)。HTML浏览器太庞大而且不兼容的主要原因之一,是当它们遇到HTML错误时,它们会以自己的方式断定文件该是什么样子。

With XML this should not be possible.
用XML形式时,就不可能出现这种情况了。


Validate your XML - IE Only
校验你的XML

To help you validate your xml, we have used Microsoft's XML parser to create an XML validator.
为了帮助你校验XML,我们通过Microsoft's XML 解析器创建了一个关于XML有效性的“校验器”。

Paste your XML in the text area below, and syntax-check it by pressing the "Validate" button.
把XML数据粘贴到下面的文本区域内,点击“提交”按钮进行校验。(IE6)



Validate your XML File
校验你的XML文件

You can also validate your XML files by typing the URL of your file into the input field below, and then press the "Validate" button
你也可以先在下面的输入域内输入文件的URL地址,再点击“提交”按钮,进行检测。

文件地址:

If you want to syntax-check (validate) an error-free XML file, you can paste the following address into the filename field:
如果你想对一份XML文件是否存在错误进行语句检测,你可以把下面的地址粘贴到文件名区域里
http://www.w3pop.com/tech/school/xml/cd_catalog.xml

Note: If you get the error "Access denied" when accessing this file, it is because your Internet Explorer security settings do not allow access across domains!
Note: 如果访问文件时出现“拒绝访问”的错误提示信息,这说明你的IE安全设置不允许跨域访问。

支持 XML 的浏览器
w3pop.com / 2006-09-19

大多数英特网浏览器支持XML形式。但是,不是所有浏览器完全支持XML,XML Namespaces,和 XSLT.


Internet Explorer 6

Internet Explorer 6完全支持XML,包括Namespaces, Style sheets in CSS, and XSLT 1.0. 

Internet Explorer 6.0 和 Windows XP中的built-in XML Parser 3.0是以W3C XSLT 1.0 和 W3C XPath 1.0 推荐为基础的。


Internet Explorer 5

Internet Explorer 5 也支持XML,但XSL部分和官方的 W3C XSL 推荐格式(Recommendation)不兼容。


Firefox 1.0.2

The Firefox browser 也支持 XML 和 XSLT (还有 XML + CSS).


Mozilla 1.7.8

Mozilla支持XML的分析和XML + CSS的展示。Mozilla也部分支持Namespaces

Mozilla支持XSLT的操作


Opera 8

Opera 支持 XML 和XML + CSS


Netscape 6

Netscape 支持 XML.


Internet Explorer 6.0

Internet Explorer 6.0完全支持 XML, 还支持 Namespaces. Style sheets in CSS 以及 XSLT 1.0 

Internet Explorer 提供以下 XML 支持:

  • 浏览XML文件
  • 完全支持W3C DTD standards
  • XML作为“数据岛”植入HTML
  • 把XML数据捆绑到HTML元素里
  • 用XSL传输和展示XML形式的数据
  • 用CSS展示XML形式的数据
  • 访问XML DOM

Internet Explorer还支持Behaviors:

  • 仅支持微软提供的Behaviors
  • Behaviors 可以将脚本从HTML页中分离出来
  • Behaviors 可以在客户端磁盘上存储XML数据

关于这些特征的例子都将在此教程中一一列出。

浏览 XML 文件
w3pop.com / 2006-09-19

Raw XML files can be viewed in Mozilla, Firefox, Opera, Internet Explorer, and Netscape 6+.
原始的XML的文件可以在Mozilla, Firefox, Opera, Internet Explorer, 和 Netscape 6+.中被浏览。

However, to make XML documents display as nice web pages, you will have to add some display information.
然而,为了让XML文档显示出来是漂亮的网页,你必须添加一些显示信息。


Viewing XML Files
浏览XML 文件

In Firefox and Internet Explorer:
在Firefox 和Internet Explorer浏览:

Open the XML file (typically by clicking on a link) - The XML document will be displayed with color-coded root and child elements. A plus (+) or minus sign (-) to the left of the elements can be clicked to expand or collapse the element structure. To view the raw XML source (without the + and - signs), select "View Page Source" or "View Source" from the browser menu.
打开XML文件(一般就是点击一下链接)——XML文档中显示的根目录和子目录是 由彩色标记的,点击目录左边的加(+)减(-)号,可以展开收回目录结构。选择浏览器菜单上的“浏览页面资源”("View Page Source")或“浏览资源”( "View Source")

In Netscape 6:
在Netscape 6浏览:

Open the XML file, then right-click in XML file and select "View Page Source". The XML document will then be displayed with color-coded root and child elements.
打开XML文件,右击XML文件,选择“浏览页面资源”。出现的将是有显示彩色标记的根子目录的XML文档。

In Opera 7 and 8:
在Opera 7 and 8浏览:

In Opera 7: Open the XML file, then right-click in XML file and select "Frame" / "View Source". The XML document will be displayed as plain text. In Opera 8: Open the XML file, then right-click in XML file and select "Source". The XML document will be displayed as plain text.
在Opera 7上: 打开XML文件,右击XML文件,选择“框架”/“浏览资源”("Frame" / "View Source")。XML文档将以普通文本显示。在Opera 8: 打开XML文件,右击XML文件,选择“资源”("Source”)。XML文档将以普通文本显示

Look at this XML file: note.xml
看这份XML文件:: note.xml

Note: Do not expect XML files to be formatted like HTML documents!
Note: 可别指望XML文件会像HTML文件一样布局。


Viewing an Invalid XML File
浏览一份残缺的XML文件

If an erroneous XML file is opened, the browser will report the error.
如果打开一份有错的XML文件,浏览器会报告错误

Look at this XML file: note_error.xml
看这份: note_error.xml


Other XML Examples
别的XML例子

Viewing some XML documents will help you get the XML feeling.
浏览一些XML文档可以帮你找到点“XML感觉”

An XML CD catalog
This is my father's CD collection, stored as XML data (old and boring titles I guess... :-)).
一份CD名录的XML文件
这是以XML形式储存数据的我父亲的CD集(我想这些歌名又老又乏味)

An XML plant catalog
This is a plant catalog from a plant shop, stored as XML data.
一份植物名录的XML文件
这是以XML形式储存数据的来自花草店的植物名录

A Simple Food Menu
This is a breakfast food menu from a restaurant, stored as XML data.
简单的食物菜单
这是以XML形式储存数据的来自餐馆的早点菜单


Why Does XML Display Like This?
为什么XML文件显示是这样的?

XML documents do not carry information about how to display the data.
XML文档并没包含关于怎样显示数据的信息

Since XML tags are "invented" by the author of the XML document, browsers do not know if a tag like <table> describes an HTML table or a dining table.
因为XML标签是作者自定义的,浏览器并不清楚一标签如<table>描述的是“HTML 表格”(HTML table),还是“餐桌”(dining table)

Without any information about how to display the data, most browsers will just display the XML document as it is.
没有关于数据显示的信息,大多数浏览器只会显示原来的XML文档

In the next chapters, we will take a look at different solutions to the display problem, using CSS, XSL, JavaScript, and XML Data Islands.
在下几章里,我们将看看解决显示问题的一些不同方法,如用到CSS, XSL, JavaScript, 和 XML Data Islands

用 CSS 显示 XML
w3pop.com / 2006-09-19

With CSS (Cascading Style Sheets) you can add display information to an XML document.
利用CSS(层叠式样式表Cascading Style Sheets),你可以给XML文件添加显示信息


Displaying your XML Files with CSS?
用CSS显示XML?

It is possible to use CSS to format an XML document.
可以用CSS处理XML文件布局

Below is an example of how to use a CSS style sheet to format an XML document:
以下是如何用CSS式样表处理XML文件布局的:

Take a look at this XML file: The CD catalog
请看这XML文件: CD目录

Then look at this style sheet: The CSS file
再看样式表: CSS文件

Finally, view: The CD catalog formatted with the CSS file
最后,看到的是 通过CSS布局后的CD目录

Below is a fraction of the XML file. The second line, <?xml-stylesheet type="text/css" href="cd_catalog.css"?>, links the XML file to the CSS file:
以下是这份XML文件的一小部分: <?xml-stylesheet type="text/css" href="cd_catalog.css"?>,第二行把XML文件链接到CSS文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/css" href="cd_catalog.css"?>

<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>

<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>

<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>

<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>

.
.
.
.
</CATALOG>

Note: Formatting XML with CSS is NOT the future of how to style XML documents. XML document should be styled by using the W3C's XSL standard!
Note:
用CSS格式化XML并不是对XML文件进行布局的永久之计。XML文件应用W3C's XSL 标准进行定义及布局。

用 XSL 显示 XML
w3pop.com / 2006-09-19

With XSL you can add display information to your XML document.
用XSL你可以给你的XML文本添加显示信息

Displaying XML with XSL
用XSL显示XML

XSL is the preferred style sheet language of XML.
XSL是XML的较好的样式表语言

XSL (the eXtensible Stylesheet Language) is far more sophisticated than CSS. One way to use XSL is to transform XML into HTML before it is displayed by the browser as demonstrated in these examples:
XSL(可扩展式样表语言)远比CSS成熟,用XSL的一种方式是:在XML被浏览器显示之前,转换成HTML以下的例子说明了这点:

查看XML文件, XSL样式表结果.

Below is a fraction of the XML file. The second line, <?xml-stylesheet type="text/xsl" href="simple.xsl"?>, links the XML file to the XSL file:
下面是XML文件的一小部分,第二行, <?xml-stylesheet type="text/xsl" href="simple.xsl"?>,把XML文件链接到XSL文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="simple.xsl"?>
<breakfast_menu>

<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>
two of our famous Belgian Waffles
</description>

<calories>650</calories>
</food>
</breakfast_menu>

If you want to learn more about XSL, please visit our XSL tutorial.
如果你想了解更多关于XSL,请看我们的 XSL 教程

XML 数据岛
w3pop.com / 2006-09-19

With Internet Explorer, the unofficial <xml> tag can be used to create an XML data island.
用IE浏览器,非正式的<xml>标记可用于创建XML数据岛

XML Data Embedded in HTML
嵌入HTML的XML数据

An XML data island is XML data embedded into an HTML page.
XML数据岛是嵌入HTML页面的XML数据

Here is how it works; assume we have the following XML document ("note.xml"):
接下来讲讲它的作用原理,假设我们有下面这份XML文档("note.xml"):

<?xml version="1.0" encoding="ISO-8859-1"?>

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>
</note>

Then, in an HTML document, you can embed the XML file above with the <xml> tag. The id attribute of the <xml> tag defines an ID for the data island, and the src attribute points to the XML file to embed:
接着,在一份HTML文档里,你可以嵌入一份XML文档,XML文档最顶端有个<xml>标记。<xml>标记的ID属性定义了数据岛的ID,src属性指明了嵌入的XML文件。

<html>
<body>
<xml id="note" src="note.xml"></xml>
</body>
</html>

However, the embedded XML data is, up to this point, not visible for the user.
但是,嵌入的XML数据对使用者来讲是不可见的。

The next step is to format and display the data in the data island by binding it to HTML elements.
下一步就是把数据岛的数据捆绑到HTML元素中来给数据定义格式以及显示数据。


Bind Data Island to HTML Elements
把数据岛捆绑给HTML元素

In the next example, we will embed an XML file called "cd_catalog.xml" into an HTML file.
下面这个例子里,我们把一份"cd_catalog.xml"XML文件嵌入到HTML文件里

查看"cd_catalog.xml".

The HTML file looks like this:
HTML文件是这样的:

<html>

<body>

<xml id="cdcat" src="cd_catalog.xml"></xml>

<table border="1" datasrc="#cdcat">

<tr>
<td><span datafld="ARTIST"></span></td>
<td><span datafld="TITLE"></span></td>
</tr>
</table>

</body>
</html>

Example explained:
例子说明:

The datasrc attribute of the <table> tag binds the HTML table element to the XML data island. The datasrc attribute refers to the id attribute of the data island.
<table>标签中的datasrc属性把HTML 表格(table)元素捆绑到XML数据岛。Datasrc属性指的是数据岛的ID属性

<td> tags cannot be bound to data, so we are using <span> tags. The <span> tag allows the datafld attribute to refer to the XML element to be displayed. In this case, it is datafld="ARTIST" for the <ARTIST> element and datafld="TITLE" for the <TITLE> element in the XML file. As the XML is read, additional rows are created for each <CD> element.
<td>标签不能被绑到数据上,所以我们用<span>标签,<span>标签允许datafld属性指代要被显示的XML元件。这种情况下,在XML文件里,datafld="ARTIST"代表了<ARTIST>元素,datafld="TITLE" 代表了<TITLE>元素,XML被读时,每个<CD>元件都会重新换行。

If you are running IE 5.0 or higher, you can try it yourself.
如果你使用IE 5.0或更高版本的浏览器, 动手试一下.

Also try this example, demonstrating <thead>, <tbody>, and <tfoot>.
也可以试下 这个举例, 验证<thead>, <tbody>, 和 <tfoot>.

XML 解析器
w3pop.com / 2006-09-19

To read and update - create and manipulate - an XML document, you will need an XML parser.
想要阅读、更新、创建、操作一份XML文档,你需要一个XML解析器


Microsoft's XML Parser
微软XML解析器

Microsoft's XML parser is a COM component that comes with Internet Explorer 5 and higher. Once you have installed Internet Explorer, the parser is available to scripts.
微软的XML解析器是与IE5及其更高版的浏览器整合在一起的COM组件,一旦你安装了IE浏览器,就可以使用解析器解析脚本了。

Microsoft's XML parser supports all the necessary functions to traverse the node tree, access the nodes and their attribute values, insert and delete nodes, and convert the node tree back to XML.
微软的XML解析器具有所有的必备功能,可以穿过节点树,访问节点和其属性值,插入或删除节点,把节点树转换成XML形式。

The following table lists the most commonly used node types supported by Microsoft's XML parser:
下面的表格列出了微软的XML解析器支持的最常用的节点类型。

Node Type
Node 类型
Example
举例
Processing instruction[指令] <?xml version="1.0"?>
Element[元素] <drink type="beer">Carlsberg</drink>
Attribute[属性] type="beer"
Text[文字] Carlsberg

MSXML Parser 2.5 is the XML parser that is shipped with Windows 2000 and IE 5.5.
MSXML Parser 2.5是在Windows 2000 和 IE 5.5的安装而安装的XML解析器。

MSXML Parser 3.0 is the XML parser that is shipped with IE 6.0 and Windows XP.
MSXML Parser 3.0是在IE 6.0 和 Windows XP的安装而安装的XML解析器。

The MSXML 3.0 parser features:
MSXML 3.0解析器的具体特征:

  • JavaScript, VBScript, Perl, VB, Java, C++, etc. support
    支持JavaScript, VBScript, Perl, VB, Java, C++,等语言。
  • Complete XML support
    完全支持XML。
  • Full DOM and Namespace support
    完全支持DOM 和 Namespace
  • DTD and validation
    DTD和有效性验证
  • Complete XSLT and XPath support
    完全支持XSLT 和 XPath
  • SAX2 support
    支持SAX2
  • Server-safe HTTP
    服务器安全协议

To create an instance of Microsoft's XML parser with JavaScript, use the following code:
用JavaScript语言编一个微软XML解析器的例子,请用下面的代码:

var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")

To create an instance of Microsoft's XML parser with VBScript, use the following code:
用VBScript编写一个微软XML解析器的例子,请用下面的代码:

set xmlDoc=CreateObject("Microsoft.XMLDOM")

To create an instance of Microsoft's XML parser in an ASP page (using VBScript), use the following code:
在ASP(用VBScript)页面内编一个微软XML解析器的例子,请用下面的代码:

set xmlDoc=Server.CreateObject("Microsoft.XMLDOM")

The following code loads an existing XML document ("note.xml") into Microsoft's XML parser:
下面的代码把存在的XML文档("note.xml")装载到现存的微软XML解析器里:

<script type="text/javascript">
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
...
...
...
</script>

 

The first line of the script above creates an instance of the Microsoft XML parser. The third line tells the parser to load an XML document called "note.xml". The second line turns off asynchronized loading, to make sure that the parser will not continue execution of the script before the document is fully loaded.
以上语言的第一行创建了一个微软XML解析器的例子。第三行告诉解析器装载一份名为"note.xml"的XML文档。第二行关闭了异时的装载,以确保解析器在文档彻底装载好之前不会继续执行语句。


XML Parser in Mozilla Browsers
Mozilla浏览器上的XML解析器

Plain XML documents are displayed in a tree-like structure in Mozilla (just like IE).
在Mozilla(类似于IE一样的浏览器)上,普通的XML文档将以树状结构显示出来

Mozilla also supports parsing of XML data using JavaScript. The parsed data can be displayed as HTML.
Mozilla也支持用JavaScript解析XML数据。被解析的数据可以以HTML形式显示出来。

To create an instance of the XML parser with JavaScript in Mozilla browsers, use the following code:
在Mozilla浏览器上用JavaScript编一个微软XML解析器的例子,请用下面的代码:

var xmlDoc=document.implementation.createDocument("ns","root",null)

The first parameter, ns, defines the namespace used for the XML document. The second parameter, root, is the XML root element in the XML file. The third parameter, null, is always null because it is not implemented yet.
第一个参数,ns,定义了XML文档所使用的名称空间。第二个参数,root,是XML文件里的XML根目录元件,第三个参数,null,是无效用的,因为它未被执行。

The following code loads an existing XML document ("note.xml") into Mozillas' XML parser:
下面的代码把一份存在的XML文档("note.xml")装载给Mozillas的XML解析器:

<script type="text/javascript">

var xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("note.xml");
...
...
...
</script>

The first line of the script above creates an instance of the XML parser. The second line tells the parser to load an XML document called "note.xml".
语句第一行创建了XML解析器的例子,第二行告诉解析器装载一份名为"note.xml"的XML文档。


Loading an XML File - A Cross browser Example
装载一份XML文档,交互式浏览器具体情况举例

The following example is a cross browser example that loads an existing XML document ("note.xml") into the XML parser:
以下例子讲的是在交叉浏览器状况下把存在的XML文档("note.xml")载入XML解析器的例子

<html>
<head>
<script type="text/javascript">
var xmlDoc
function loadXML()
{
//load xml file
// code for IE
if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("note.xml");
getmessage()
}
// code for Mozilla, etc.
else if (document.implementation &&

document.implementation.createDocument)
{
xmlDoc= document.implementation.createDocument("","",null);
xmlDoc.load("note.xml");
xmlDoc.onload=getmessage
}
else
{
alert('Your browser cannot handle this script');
}
}
function getmessage()
{
document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")[0].firstChild.nodeValue
document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")[0].firstChild.nodeValue
document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")[0].firstChild.nodeValue
}

</script>
</head>
<body onload="loadXML()" bgcolor="yellow">
<h1>W3Schools Internal Note</h1>
<p><b>To:</b> <span id="to"></span><br />

<b>From:</b> <span id="from"></span>
<hr />
<b>Message:</b> <span id="message"></span>

</p>
</body>
</html>

Try it yourself
自己尝试下

There are more examples of this in our XML DOM tutorial.
还有更多的举例在我们的 XML DOM 教程.


Loading XML Text Into the Parser
把XML 文本载入解析器

Internet Explorer supports two ways of loading XML into a document object: the load() method and the loadXML() method. The load() method loads an XML file and the loadXML() method loads a text string that contains XML code.
IE浏览器支持两种把XML载入文本对象的方法:“load()” 方法和“loadXML()”方法。“load()”方法载入一份XML文件,“loadXML()”方法载入含有XML编码的文本字符串。

The following code loads a text string into Microsoft's XML parser:
下面的代码把一段文本字符串载入微软的XML解析器:

<script type="text/javascript">
var txt="<note>"
txt=txt+"<to>Tove</to><from>Jani</from>"
txt=txt+"<heading>Reminder</heading>"
txt=txt+"<body>Don't forget me this weekend!</body>"

txt=txt+"</note>"
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.loadXML(txt)
...
...
...
</script>

If you have Internet Explorer, you can try it yourself.
如果你有IE, 你可以 自己尝试下.

现实生活中的 XML
w3pop.com / 2006-09-19

Some real-life examples of how XML can be used to carry information.
举一些现实生活中的例子来指明XML可以被用来携带信息。

Example: XML News
XML新闻(XML News)

XMLNews is a specification for exchanging news and other information.
XML新闻是用来交换新闻或是其他信息的一种规格

Using such a standard makes it easier for both news producers and news consumers to produce, receive, and archive any kind of news information across different hardware, software, and programming languages.
通过使用这种标准,新闻制造者和新闻消费者可以更轻松地用不同的硬件,软件和程序语言,来制造,接收,取得任何种类的新闻信息。


An example XML News document:
XML NEWS 文件的例子

<?xml version="1.0" encoding="ISO-8859-1"?>

<nitf>

<head>
<title>Colombia Earthquake</title>

</head>

<body>

<headline>
<hl1>143 Dead in Colombia Earthquake</hl1>
</headline>
<byline>

<bytag>By Jared Kotler, Associated Press Writer</bytag>
</byline>
<dateline>
<location>Bogota, Colombia</location>
<date>Monday January 25 1999 7:28 ET</date>

</dateline>

</body>

</nitf>

XML 命名空间
w3pop.com / 2006-09-19

XML Namespaces provide a method to avoid element name conflicts.
XML命名空间提供了避免元素名称矛盾的方法


Name Conflicts
名称矛盾

Since element names in XML are not predefined, a name conflict will occur when two different documents use the same element names.
因为XML的元素名称没有预先定义,如果两份不同文档的元素名称相同,就会产生名称矛盾

This XML document carries information in a table:
这份XML文档含有表格里的信息:

<table>
<tr>
<td>Apples</td>

<td>Bananas</td>
</tr>
</table>

This XML document carries information about a table (a piece of furniture):
这份XML文档含有关于一张桌子的信息(一件家具):

<table>
<name>African Coffee Table</name>

<width>80</width>
<length>120</length>
</table>

If these two XML documents were added together, there would be an element name conflict because both documents contain a <table> element with different content and definition.
如果两份XML文档合起来,会出现元素名称矛盾,因为两份文件都有一个<table>元素,但其所含内容和定义是不一样的。


Solving Name Conflicts Using a Prefix
用前缀法解决命名矛盾问题

This XML document carries information in a table:
这份XML文档含有表格里的信息:

<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>

</h:tr>
</h:table>

This XML document carries information about a piece of furniture:
这份XML文档含有关于一张桌子的信息(一件家具):

<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>

<f:length>120</f:length>
</f:table>

Now there will be no name conflict because the two documents use a different name for their <table> element (<h:table> and <f:table>).
现在就不会有命名矛盾了,因为两分文档对他们的<table>元素用了不同的名称(<h:table> 和<f:table>).

By using a prefix, we have created two different types of <table> elements.
通过使用前缀,我们创建了两种不同类型的<table>元素


Using Namespaces
用命名空间

This XML document carries information in a table:
这份XML文档含有表格里的信息:

<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>

<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>

This XML document carries information about a piece of furniture:
这份XML文档含有关于一张桌子的信息(一件家具)

<f:table xmlns:f="http://www.w3schools.com/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>

</f:table>

Instead of using only prefixes, we have added an xmlns attribute to the <table> tag to give the prefix a qualified name associated with a namespace.
我们没有只使用前缀,还给<table>标签添加了xmlns属性,使得前缀有一个与命名空间相联系的合法名称。


The XML Namespace (xmlns) Attribute
XML名称空间(xmlns)属性

The XML namespace attribute is placed in the start tag of an element and has the following syntax:
XML名称空间属性安排在一个元素的始标签里,有如下的语法:

xmlns:namespace-prefix="namespaceURI"

When a namespace is defined in the start tag of an element, all child elements with the same prefix are associated with the same namespace.
当一个命名空间在一个元素的始标签里被定义,有相同前缀的所有子元素都与相同的名称空间相关联;

Note that the address used to identify the namespace is not used by the parser to look up information. The only purpose is to give the namespace a unique name. However, very often companies use the namespace as a pointer to a real Web page containing information about the namespace.
要注意的是:用于定义名称空间的地址,并不被解析器用来寻找信息。它的唯一作用是给名称空间一个特别的名称。然而,许多公司经常把名称空间用作为实际存在的网页的指示器,这些网页含有关于名称空间的信息。
尝试到达W3.org: http://www.w3.org/TR/html4/.


Uniform Resource Identifier (URI)
统一资源识别器(URI)

A Uniform Resource Identifier (URI) is a string of characters which identifies an Internet Resource. The most common URI is the Uniform Resource Locator (URL) which identifies an Internet domain address. Another, not so common type of URI is the Universal Resource Name (URN). In our examples we will only use URLs.
URI是识别网络资源的一串字符。最普通的URI是Uniform Resource Locator URL,它识别英特网的域地址。另一种不太普遍的URI是Universal Resource Name (URN).在我们的例子里我们只用URL。


Default Namespaces
默认的名称空间(Namespaces)

Defining a default namespace for an element saves us from using prefixes in all the child elements. It has the following syntax:
给元素定义默认的名称空间,可以帮我们省去给所有子元素用前缀的麻烦。它有如下的语法:

xmlns="namespaceURI"

This XML document carries information in a table:
这份XML文档含有表格里的信息:

<table xmlns="http://www.w3.org/TR/html4/">

<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>

This XML document carries information about a piece of furniture:
这份XML文档含有关于一张桌子的信息(一件家具)

<table xmlns="http://www.w3schools.com/furniture">
<name>African Coffee Table</name>
<width>80</width>

<length>120</length>
</table>


Namespaces in Real Use
实际使用中的名称空间

When you start using XSL, you will soon see namespaces in real use. XSL style sheets are used to transform XML documents into other formats, like HTML.
一旦开始使用XSL,你会很快看到实际使用中的名称空间。XSL式样表用于把XML转换成其他形式,例如HTML。

If you take a close look at the XSL document below, you will see that most of the tags are HTML tags. The tags that are not HTML tags have the prefix xsl, identified by the namespace "http://www.w3.org/1999/XSL/Transform":
如果你仔细看下面的例子,你会看到大部分标签都是HTML型的标签。非HTML的标签含有“xsl”的前缀,可以被名称空间识别。 "http://www.w3.org/1999/XSL/Transform":

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">

<tr>
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>

<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>

</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

XML CDATA
w3pop.com / 2006-09-19

All text in an XML document will be parsed by the parser.
XML文档中的所有文本都会被解析器解析。

Only text inside a CDATA section will be ignored by the parser.
只有CDATA部分的文本会被解析器忽略


Parsed Data
被解析的数据

XML parsers normally parse all the text in an XML document.
XML解析器通常会解析XML文档里的所有文本

When an XML element is parsed, the text between the XML tags is also parsed:
XML元素被解析时,XML标签里的文本也被解析:

<message>This text is also parsed</message>

The parser does this because XML elements can contain other elements, as in this example, where the <name> element contains two other elements (first and last):
解析器这样做是因为XML元素可能含有别的元素,象在这个例子里,<name>元素含有其他两个元素(第一个和最后一个)

<name><first>Bill</first><last>Gates</last></name>

and the parser will break it up into sub-elements like this:
解析器会把它分解成象这样的子元素:

<name>
<first>Bill</first>
<last>Gates</last>
</name>


Escape Characters
换码字符

Illegal XML characters have to be replaced by entity references.
非法XML字符必须由实体说明替代。

If you place a character like "<" inside an XML element, it will generate an error because the parser interprets it as the start of a new element. You cannot write something like this:
如果你在一份XML元素中放一个"<"字符,就会产生错误,因为解析器会认为它是新元素的开始,你不可以写成这样:

<message>if salary < 1000 then</message>

 

To avoid this, you have to replace the "<" character with an entity reference, like this:
为了避免这个,你必须用一个实体说明来代替,就象这样:

<message>if salary &lt; 1000 then</message>

There are 5 predefined entity references in XML:
在XML里,有5个预先定义的实体说明:

&lt; < less than[大于]
&gt; > greater than[小于]
&amp; & ampersand 
&apos; ' apostrophe[省略号]
&quot; " quotation mark[引号]


注意:
只有字符"<" 和 "&"在XML里是严格意义上非法的。省略符,引号和更高级的符号是合法的,用"<" 和 "&"代替它们是更为简便的方法。


CDATA

Everything inside a CDATA section is ignored by the parser.
在CDATA部分的一切内容都会被解析器忽略。

If your text contains a lot of "<" or "&" characters - as program code often does - the XML element can be defined as a CDATA section.
如果你的文本含有大量的"<" 或"&"字符—就象程序码中经常的那样 —   XML元素可以被定义为一个CDATA部分。

A CDATA section starts with "<![CDATA[" and ends with "]]>":
一个CDATA部分常由"<![CDATA["开始,由"]]>"结束。

<script>
<![CDATA[
function matchwo(a,b)
{
if (a < b && a < 0) then
{
return 1
}
else
{
return 0
}
}
]]>

</script>

In the example above, everything inside the CDATA section is ignored by the parser.
在上面的例子里,CDATA部分的所有内容都被解析器省略了。

Notes on CDATA sections:
CDATA部分的注意事项:

A CDATA section cannot contain the string "]]>", therefore, nested CDATA sections are not allowed.
CDATA部分不能含有字符串"]]>",因此,不允许嵌入CDATA部分

Also make sure there are no spaces or line breaks inside the "]]>" string.
还要注意在字符串里不应含有空格或断句。

XML字符编码
w3pop.com / 2006-10-19

XML documents may contain foreign characters, like Norwegian ???, or French ???
XML文档中可以使用外来字符,比如挪威语或法语

To let your XML parser understand these characters, you should save your XML documents as Unicode.
如要使你的XML解析器能够读懂这些字符,你就应该将XML文档保存为统一代码(Unicode)


Windows 2000 写字板

Windows 2000 Notepad can save files as Unicode.
使用WIN2000的写字板就可以将文件保存为Unicode

Save the XML file below as Unicode (note that the document does not contain any encoding attribute):
将以下XML文件保存为Unicode编码(可以注意下问档没有包含任何编码属性):

<?xml version="1.0"?>
<note>
<from>Jani</from>
<to>Tove</to>
<message>Norwegian: 骧? French: 觇?lt;/message>
</note>

The file above, note_encode_none_u.xml will NOT generate an error in IE 5+, Firefox, or Opera, but it WILL generate an error in Netscape 6.2.
上面这个文件在IE5+,Firefox, 或Opera中不会产生任何错误,但它在Netscape 6.2里会有一个错误。


Windows 2000 写字板附带编码

Windows 2000 Notepad files saved as Unicode use "UTF-16" encoding.
将文件保存为"UTF-16"编码格式

If you add an encoding attribute to XML files saved as Unicode, windows encoding values will generate an error.
如果你给XML文件加上编码属性,wendows的编码值会产生错误。

The following encoding (open it), will NOT give an error message:
使用下面的编码,不会给出任何错误。

<?xml version="1.0" encoding="windows-1252"?>

The following encoding (open it), will NOT give an error message:
使用下面的编码,不会产生任何错误

<?xml version="1.0" encoding="ISO-8859-1"?>

The following encoding (open it), will NOT give an error message:
使用下面的编码,不会产生任何错误

<?xml version="1.0" encoding="UTF-8"?>

The following encoding (open it), will NOT generate an error in IE 5+, Firefox, or Opera, but it WILL generate an error in Netscape 6.2.
使用下面的编码在IE 5+, Firefox, 或 Opera 中不会产生任何错误,但在Netscape 6.2里会有错误

<?xml version="1.0" encoding="UTF-16"?>


错误信息

If you try to load an XML document into Internet Explorer, you can get two different errors indicating encoding problems:
如果你试着 把XML文档载入Internet Explorer,你会得到关于编码问题的两个不同的错误信息

An invalid character was found in text content.
在文本内容中发现一个无效字符

You will get this error message if a character in the XML document does not match the encoding attribute. Normally you will get this error message if your XML document contains "foreign" characters, and the file was saved with a single-byte encoding editor like Notepad, and no encoding attribute was specified.
如果XML文档中有一个字符与编码属性不匹配,你就会得到这个错误信息。一般来讲,如果你的XML的文档包含外文字符,或文件是以记事本单字节编辑器来保存的,或编码属性没有被指定的,你就会得到这个错误信息。

Switch from current encoding to specified encoding not supported.
从现在的编码切换到非支持的指定编码

You will get this error message if your file was saved as Unicode/UTF-16 but the encoding attribute specified a single-byte encoding like Windows-1252, ISO-8859-1 or  UTF-8. You can also get this error message if your document was saved with single-byte encoding, but the encoding attribute specified a double-byte encoding like UTF-16.
如果编码属性指定为像Windows-1252, ISO-8859-1 或 UTF-8的单字节编码,而你的文件却是以Unicode/UTF-16保存的,你就会得到这个出错信息


结论

The conclusion is that the encoding attribute has to specify the encoding used when the document was saved. My best advice to avoid errors is:
结论就是在文件保存时,编码属性应指定被用的编码,我对避免错误的最好建议是:

  • Use an editor that supports encoding
    • 使用支持编码的编辑器
  • Make sure you know what encoding it uses
    • 你得弄清楚在使用的是什么编码
  • Use the same encoding attribute in your XML documents
    • 在你的XML文档中使用相同的编码属性

XML 在服务器
w3pop.com / 2006-09-19

XML can be generated on a server without installing any XML controls.
不用安装任何XML控件就可以在服务器上生成XML文件

Storing XML on the Server
在服务器上储存xml

XML files can be stored on an Internet server exactly the same way as HTML files.
XML文件在Internet服务器上的储存方式与HTML文件几乎相同。

Start Windows Notepad and write the following lines:
打开Windows 记事本(Notepad)并写出下面这几行:

<?xml version="1.0" encoding="ISO-8859-1"?>

<note>
<from>Jani</from>
<to>Tove</to>
<message>Remember me this weekend</message>

</note>

Save the file on your web server with a proper name like "note.xml".
给文件起个恰当的名字(如:note.xml)并将其保存在你的网络服务器上。


Generating XML with ASP
利用ASP生成XML

XML can be generated on a server without any installed XML software.
不用安装任何XML软件就可以在服务器上生成XML。

To generate an XML response from the server - simply write the following code and save it as an ASP file on the web server:
为了在服务器上产生一个XML回应—只须简单写下如下的代码,并在网络服务器上把它保存为一份ASP文件

<%
response.ContentType="text/xml"

response.Write("<?xml version='1.0' encoding='ISO-8859-1'?>")
response.Write("<note>")
response.Write("<from>Jani</from>")
response.Write("<to>Tove</to>")
response.Write("<message>Remember me this weekend</message>")
response.Write("</note>")
%>

Note that the content type of the response must be set to "text/xml".
注意回复的内容类型必须设置为"text/xml"。

See how the ASP file will be returned from the server.
看看从服务器返回的ASP文件里有些什么.

If you don't know how to write ASP, please visit our ASP tutorial
如果你不知道怎么写ASP,可以访问我们的 ASP教程


Getting XML From a Database
从数据库得到XML形式数据

XML can be generated from a database without any installed XML software.
没有安装任何XML软件就可以从数据库中产生XML。

To generate an XML database response from the server, simply write the following code and save it as an ASP file on the web server:
为了从服务器得到XML数据库响应,只须简单写下如下的代码,并在网络服务器上把它保存为一份ASP文件

<%
response.ContentType = "text/xml"
set conn=Server.CreateObject("ADODB.Connection")
conn.provider="Microsoft.Jet.OLEDB.4.0;"
conn.open server.mappath("/db/database.mdb")
sql="select fname,lname from tblGuestBook"
set rs=Conn.Execute(sql)
rs.MoveFirst()
response.write("<?xml version='1.0' encoding='ISO-8859-1'?>")
response.write("<guestbook>")
while (not rs.EOF)
response.write("<guest>")
response.write("<fname>" & rs("fname") & "</fname>")
response.write("<lname>" & rs("lname") & "</lname>")
response.write("</guest>")
rs.MoveNext()
wend
rs.close()
conn.close()
response.write("</guestbook>")
%>

See the real life database output from the ASP file above.
看看上面的ASP文件中数据库的实际输出情况.

The example above uses ASP with ADO. If you don't know how to use ADO, please visit our ADO tutorial.
上面的举例中ASP动用了ADO.你如果不知道怎么使用ADO的话,可以访问我们的 ADO 教程

XML 应用
w3pop.com / 2006-09-20

This chapter demonstrates a small framework for an XML application.
这章讲了写XML应用程序的一个小框架。

Note: This example uses a Data Island, which only works in Internet Explorer.
注意:这个例子用到了数据岛,这只有在IE中才能使用。


The XML Example Document
XML样本文件

Look at the following XML document ("cd_catalog.xml"), that represents a CD catalog:
请看下面的XML文件("cd_catalog.xml"),它代表着一个CD名录:

<?xml version="1.0" encoding="ISO-8859-1"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
.
.
... more ...
.

View the full "cd_catalog.xml" file in your browser.
在浏览器里查看完整的"cd_catalog.xml"文件


Load the XML Document Into a Data Island
把XML文档载入数据岛

A Data Island can be used to access the XML file.
数据岛可用于访问XML文件。

To get your XML document "inside" an HTML page, add an XML Data Island to the HTML page:
要在HTML页面中得到XML文档,得在HTML页面中添加一个XML数据岛:

<xml src="cd_catalog.xml" id="xmldso" async="false">
</xml>

With the example code above, the XML file "cd_catalog.xml" will be loaded into an "invisible" Data Island called "xmldso". The async="false" attribute is added to make sure that all the XML data is loaded before any other HTML processing takes place
有了上面例子中的代码,XML文件"cd_catalog.xml"会被载入一个“可见”的数据岛,数据岛叫做“xmldso”。The async="false"属性的设置是为了确保在HTML处理过程发生之前,所有的XML数据都被载入了。


Bind the Data Island to an HTML Table
把数据岛绑定到HTML表格

To make the XML data visible on the HTML page, you must "bind" the Data Island to an HTML element.
为了使XML数据在HTML页面上可见,你必须把数据岛绑定到HTML的元素里

To bind the XML data to an HTML table, add a datasrc attribute to the table element, and add datafld attributes to the span elements inside the table data:
为了把数据岛绑定到HTML表格,给表格元素添加datasrc属性,并给表格数据的span 元素添加datafld属性

<table datasrc="#xmldso" width="100%" border="1">
<thead>
<th>Title</th>
<th>Artist</th>
<th>Year</th>
</thead>
<tr align="left">
<td><span datafld="TITLE"></span></td>
<td><span datafld="ARTIST"></span></td>
<td><span datafld="YEAR"></span></td>
</tr>
</table>

If you have IE 5.0 or higher: See how the XML data is displayed inside an HTML table.
如你有IE5以上版本的浏览器: 看看XML数据是如何在HTML表格中显示的.


Bind the Data Island to <span> or <div> Elements
把数据岛植入<span>或<div>元素

<span> or <div> elements can be used to display XML data.
<span>或<div>元素可用于显示XML数据

You don't have to use the HTML table element to display XML data. Data from a Data Island can be displayed anywhere on an HTML page.
你不必非得用HTML表格元素来显示XML数据。来自数据岛的数据可以在HTML页面的任何地方显示。

All you have to do is to add some <span> or <div> elements to your page. Use the datasrc attribute to bind the elements to the Data Island, and the datafld attribute to bind each element to an XML element, like this:
你所须做的只是给你的页面添加一些<span>或<div>元素。用datasrc属性把元素绑定数据岛,用datafld属性把每个元素绑定到一个XML元素上,就像这样:

<br />Title:
<span datasrc="#xmldso" datafld="TITLE"></span>
<br />Artist:
<span datasrc="#xmldso" datafld="ARTIST"></span>
<br />Year:
<span datasrc="#xmldso" datafld="YEAR"></span>

or like this:
或像这样:

<br />Title:
<div datasrc="#xmldso" datafld="TITLE"></div>
<br />Artist:
<div datasrc="#xmldso" datafld="ARTIST"></div>
<br />Year:
<div datasrc="#xmldso" datafld="YEAR"></div>

If you have IE 5.0 or higher: See how the XML data is displayed inside the HTML elements.
如你有IE5以上版本的浏览器: 可以看下XML数据是怎样在HTML元素里显示的.

Note that if you use an HTML <div> element, the data will be displayed on a new line.
注意如果你用了HTML元素<div>,数据将会在新的一行显示

With the examples above, you will only see one line of your XML data. To navigate to the next line of data, you have to add some scripting to your code.
上面的例子里,你将看到的只是你的XML数据中的一行,为了操控下行数据,你得给你的代码添加点脚本。


Add a Navigation Script
添加一个导航脚本程序

Navigation has to be performed by a script
通过脚本程序可以实现数据的导航过程

To add navigation to the XML Data Island, create a script that calls the movenext() and moveprevious() methods of the Data Island.
为了给数据岛添加导航,创立一个脚本,名为数据岛的“下一步”movenext()和“上一步“moveprevious()方法。

<script type="text/javascript">
function movenext()
{
x=xmldso.recordset
if (x.absoluteposition < x.recordcount)
{
x.movenext()
}
}
function moveprevious()
{
x=xmldso.recordset
if (x.absoluteposition > 1)
{
x.moveprevious()
}
}
</script>

If you have IE 5.0 or higher: See how you can navigate through the XML records
如你有IE5以上的浏览器: 看下XML记录是怎样进行导航的.


All Together Now
整合在一起

With a little creativity you can create a full application.
现在合在一起,用上一点点创造力你可以创造出完整的应用程序。

If you use what you have learned on this page, and a little imagination, you can easily develop this into a full application.
学完本页的知识后,你可以在此基础上加上一点想象力,从而制作出更完整的程序来。

If you have IE 5.0 or higher: See how you can add a little fancy to this application.
使用IE5以上的版本看看我们所做的一些改进.

XMLHttpRequest 对象
w3pop.com / 2006-09-20

The XMLHttpRequest object is supported in Internet Explorer 5.0+, Safari 1.2, Mozilla 1.0 / Firefox, Opera 9, and Netscape 7.
XMLHttpRequest对象可被Internet Explorer 5.0+, Safari 1.2, Mozilla 1.0 / Firefox, Opera 9, and Netscape 7.支持

What is an HTTP Request?
什么是HTTP Request?

With an HTTP request, a web page can make a request to, and get a response from a web server - without reloading the page. The user will stay on the same page, and he or she will not notice that scripts might request pages, or send data to a server in the background.
有了HTTP请求,页面可以直接向服务器提请要求,或接受服务器回应,而不必再重新加载页面。使用者仍停留在原来页面上,而不会注意到暗地里脚本已经向服务器提出请求或传送数据了。

By using the XMLHttpRequest object, a web developer can change a page with data from the server after the page has loaded.
通过使用XMLHttpRequest 对象,网络开发者在页面下载之后,可以根据从服务器那里获取的数据来更改页面。

Google Suggest is using the XMLHttpRequest object to create a very dynamic web interface: When you start typing in Google's search box, a JavaScript sends the letters off to a server and the server returns a list of suggestions.
Google Suggest 是使用XMLHttpRequest对象来创建一个极具动态效果的网页分界页面:当你开始在Google的搜索框内打入字母时,一份JAVA脚本会将系列字母送给服务器,而服务器则返回一系列建议。


 

Is the XMLHttpRequest Object a W3C Standard?
XMLHttpRequest对象是W3C的使用标准吗?

The XMLHttpRequest object is not a W3C standard.
XMLHttpRequest对象不是W3C的使用标准

The W3C DOM Level 3 "Load and Save" specification contains some similar functionality, but these are not implemented in any browsers yet. So, at the moment, if you need to send an HTTP request from a browser, you will have to use the XMLHttpRequest object.
The W3C DOM Level 3的“下载和保存”(Load and Save)规格包含着一些相似的功能,但这些没有在任何浏览器中被执行。所以,现在,如果你想要从浏览器发一份HTTP要求,你必须得用XMLHttpRequest对象。


Creating an XMLHttpRequest Object
创建一个XMLHttpRequest对象

For Mozilla, Firefox, Safari, Opera, and Netscape:
为 Mozilla, Firefox, Safari, Opera,和Netscape创建一个XMLHttpRequest对象

var xmlhttp=new XMLHttpRequest()

For Internet Explorer:
为IE创建:

var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")

Example
例子

<script type="text/javascript">
var xmlhttp
function loadXMLDoc(url)
{
xmlhttp=null
// code for Mozilla, etc.
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest()
}
// code for IE
else if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=state_Change
xmlhttp.open("GET",url,true)
xmlhttp.send(null)
}
else
{
alert("Your browser does not support XMLHTTP.")
}
}
function state_Change()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
{
// if "OK"
if (xmlhttp.status==200)
{
// ...some code here...
}
else
{
alert("Problem retrieving XML data")
}
}
}
</script>

Try it yourself using JavaScript
尝试用JavaScript创建

The syntax is a little bit different in VBScript: Try it yourself using VBScript
在语法上VBscript会有点不同 使用VBScript再试一次

Note: An important property in the example above is the onreadystatechange property. This property is an event handler which is triggered each time the state of the request changes. The states run from 0 (uninitialized) to 4 (complete). By having the function xmlhttpChange() check for the state changing, we can tell when the process is complete and continue only if it has been successful.
注意:
以上例子的重要特性是“随时可变状态(onreadystatechange)”特性。这个特性是“事件处理者”,每当请求状态改变时,它都会被触发。状态从0(初始化)到4(完成)。通过xmlhttpChange()功能对状态改变的检查,我们能够区分程序是否结束,或将继续。


 

Why are we Using Async in our Examples?
为何我们在例子里用到Async?

All the examples here use the async mode (the third parameter of open() set to true).
这儿所有例子都用了async模式(open()元素的第三个参数设置为“真”)

The async parameter specifies whether the request should be handled asynchronously or not. True means that script continues to run after the send() method, without waiting for a response from the server. false means that the script waits for a response before continuing script processing. By setting this parameter to false, you run the risk of having your script hang if there is a network or server problem, or if the request is long (the UI locks while the request is being made) a user may even see the "Not Responding" message. It is safer to send asynchronously and design your code around the onreadystatechange event!
Async 参数指定了请求是否被同时处理。“真(TRUE)”意味着在send()方法后脚本继续运行,不用等服务器作出回应。“假(FALSE)”意味着脚本继续运行前要等待回应。通过设置参数为假,如果网络或服务器有问题,你的脚本就可能被悬置,或者如果请求太长(发出请求时UI锁定),使用者可能会看到“没有回应”的信息。在“随时可变状态(onreadystatechange)”事件附近设计代码,不同时发出请求,这样会更安全。


 

More Examples
更多举例

通过XML HTTP(JavaScript)将文本文件载入一个div元素

通过XML HTTP(JavaScript)发出一个Head请求

通过XML HTTP(JavaScript)发出一个特定的Head请求

通过XML HTTP(JavaScript)列出一个XML文件中的数据


The XMLHttpRequest Object Reference
XMLHttpRequest对象参数

Methods
方法

方法 描述
abort() Cancels the current request
放弃当前请求
getAllResponseHeaders() Returns the complete set of http headers as a string
以字符串形式返回完整的http headers属性
getResponseHeader("headername") Returns the value of the specified http header
返回制定的http header值
open("method","URL",async,"uname","pswd") Specifies the method, URL, and other optional attributes of a request
指定方法、URL(超链接)以及其它有关请求的附加信息

 

The method parameter can have a value of "GET", "POST", or "PUT" (use "GET" when requesting data and use "POST" when sending data (especially if the length of the data is greater than 512 bytes.
当对需要的数据发出请求以及通过“POST”方式发送数据时(特别是发送的数据长度超过512个字节时),这个方法参数可以获取有关“GET”、“POST”或“PUT(使用“POST”方法)”的值。

The URL parameter may be either a relative or complete URL.
URL参数可以是整条URL的信息或者是其中相关的一部份

The async parameter specifies whether the request should be handled asynchronously or not. true means that script processing carries on after the send() method, without waiting for a response. false means that the script waits for a response before continuing script processing
“Async”参数指定了发送的请求是否应该通过asynchronously进行处理。“True真”表示脚本程序不间断的处理通过send()方法发送的信息而不等待任何响应;而“False假”则表示脚本程序在处理过程中等待响应

send(content) Sends the request
用于发送请求
setRequestHeader("label","value") Adds a label/value pair to the http header to be sent
把标签(label)/值(value)这个组合对加入http header后发送

Properties
属性

属性 描述
onreadystatechange An event handler for an event that fires at every state change
一个能够激发所有状态改变的事件处理器
readyState Returns the state of the object:
返回对象的具体状态

0 = uninitialized
0 = 未被初始化的
1 = loading
1 = 正在装载的
2 = loaded
2 = 已被装载的
3 = interactive
3 = 交互式的
4 = complete
4 = 完成的

responseText Returns the response as a string
以字符串的形式响应
responseXML Returns the response as XML. This property returns an XML document object, which can be examined and parsed using W3C DOM node tree methods and properties
以XML的形式响应。这个属性返回了一个XML文件对象,这个文件对象可以通过使用W3C DOM网络节点树的方式和属性来检查和解析。
status Returns the status as a number (e.g. 404 for "Not Found" or 200 for "OK")
返回以数字来表示的状态。 (e.g. 404 为"Not Found" 或 200 为 "OK")
statusText Returns the status as a string (e.g. "Not Found" or "OK")
直接返回字符串来表示当前状态(e.g. "Not Found" 或 "OK")

XML 保存数据
w3pop.com / 2006-09-20

Usually, we save data in databases. However, if we want to make the data more portable, we can store the data in an XML file.
通常,我们把数据保存在数据库里。然而,如果想让数据更易于传送,可以把数据存在XML文件里。

Create and Save an XML File
创建和保存XML文件

Storing data in XML files is useful if the data is to be sent to applications on non-Windows platforms. Remember that XML is portable across all platforms and the data will not need to be converted!
如果数据要被发送到非Windows平台上,那么把数据存在XML文件里是非常有用的。要记得XML在所有平台上都可以传送数据,而且数据是不需要转换的。

First we will learn how to create and save an XML file. The XML file below will be named "test.xml" and will be stored in the c directory on the server. We will use ASP and Microsoft's XMLDOM object to create and save the XML file:
首先我们要学习如何创建和保存一份XML文件。下面的XML文件将被命名为"test.xml",并被存在服务器的C目录区域下,我们将用ASP 和Microsoft's XMLDOM来创建和保存XML文件:

<%
Dim xmlDoc, rootEl, child1, child2, p
'Create an XML document
Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
'Create a root element and append it to the document
Set rootEl = xmlDoc.createElement("root")
xmlDoc.appendChild rootEl
'Create and append child elements
Set child1 = xmlDoc.createElement("child1")
Set child2 = xmlDoc.createElement("child2")
rootEl.appendChild child1
rootEl.appendChild child2
'Add an XML processing instruction
'and insert it before the root element
Set p=xmlDoc.createProcessingInstruction("xml","version='1.0'")
xmlDoc.insertBefore p,xmlDoc.childNodes(0)
'Save the XML file to the c directory
xmlDoc.Save "c:\test.xml"

%>

If you open the saved XML file it will look something like this ("test.xml"):
如果你打开保存好的XML文件会看到象这样的东西:

<?xml version="1.0"?>
<root>
<child1 />
<child2 />
</root>


Real Form Example
实际表单举例

Now, we will look at a real HTML form example.
现在,我们看一下一个实际的HTML表单的例子。

We will first look at the HTML form that will be used in this example: The HTML form below asks for the user's name, country, and e-mail address. This information will then be written to an XML file for storage.
首先,我们看看这个例子中HTML表单的使用方法:下面的HTML表单要用户的姓名,国籍和e-mail地址,这个信息会写到XML文件中并被储存起来。

"customers.htm":

<html>
<body>
<form action="saveForm.asp" method="post">
<p><b>Enter your contact information</b></p>

First Name: <input type="text" id="fname" name="fname"><br />
Last Name: <input type="text" id="lname" name="lname"><br />

Country: <input type="text" id="country" name="country"><br />
Email: <input type="text" id="email" name="email"><br />

<input type="submit" id="btn_sub" name="btn_sub" value="Submit">
<input type="reset" id="btn_res" name="btn_res" value="Reset">

</form>
</body>
</html>

The action for the HTML form above is set to "saveForm.asp". The "saveForm.asp" file is an ASP page that will loop through the form fields and store their values in an XML file:
对上面HTML表单的action已经设置为"saveForm.asp"。"saveForm.asp"文件是一份ASP页面,会在整个表格区域进行循环,并把它们的值存入XML文件里:

<%
dim xmlDoc
dim rootEl,fieldName,fieldValue,attID
dim p,i
'Do not stop if an error occurs
On Error Resume Next
Set xmlDoc = server.CreateObject("Microsoft.XMLDOM")
xmlDoc.preserveWhiteSpace=true
'Create a root element and append it to the document
Set rootEl = xmlDoc.createElement("customer")
xmlDoc.appendChild rootEl
'Loop through the form collection
for i = 1 To Request.Form.Count
'Eliminate button elements in the form
if instr(1,Request.Form.Key(i),"btn_")=0 then
'Create a field and a value element, and an id attribute
Set fieldName = xmlDoc.createElement("field")
Set fieldValue = xmlDoc.createElement("value")
Set attID = xmlDoc.createAttribute("id")
'Set the value of the id attribute equal to the name of
'the current form field
attID.Text = Request.Form.Key(i)
'Append the id attribute to the field element
fieldName.setAttributeNode attID
'Set the value of the value element equal to
'the value of the current form field
fieldValue.Text = Request.Form(i)
'Append the field element as a child of the root element
rootEl.appendChild fieldName
'Append the value element as a child of the field element
fieldName.appendChild fieldValue
end if
next
'Add an XML processing instruction
'and insert it before the root element
Set p = xmlDoc.createProcessingInstruction("xml","version='1.0'")
xmlDoc.insertBefore p,xmlDoc.childNodes(0)
'Save the XML file
xmlDoc.save "c:\Customer.xml"
'Release all object references
set xmlDoc=nothing
set rootEl=nothing
set fieldName=nothing
set fieldValue=nothing
set attID=nothing
set p=nothing
'Test to see if an error occurred
if err.number<>0 then
response.write("Error: No information saved.")
else
response.write("Your information has been saved.")
end if
%>

Note: If the XML file name specified already exists, it will be overwritten!
注意: 如果指定的XML文件名已经存在,它将会被覆盖。

The XML file that will be produced by the code above will look something like this ("Customer.xml"):
通过上述代码书写的XML文件,看起来就像这样("Customer.xml"):

<?xml version="1.0" ?>
<customer>
<field id="firstName">
<value>Hege</value>
</field>

<field id="lastName">
<value>Refsnes</value>
</field>
<field id="country">
<value>Norway</value>
</field>

<field id="email">
<value>mymail@myaddress.com</value>
</field>
</customer>

XML DHTML行为
w3pop.com / 2006-09-20

Internet Explorer 5 introduced DHTML behaviors. Behaviors are a way to add DHTML functionality to HTML elements with the ease of CSS.
IE5介绍了DHTML行为。行为是减少使用层叠式样式表(CSS),而将DHTML功能性添加给HTML元素的一种方法。

Behaviors - What are They?
什么是行为?

IE5 introduced DHTML behaviors. Behaviors are a way to add DHTML functionality to HTML elements with the ease of CSS.
IE5介绍了DHTML行为。行为是减少使用层叠式样式表(CSS),而将DHTML功能性添加给HTML元素的一种方法。

How do behaviors work? By using XML we can link behaviors to any element in a web page and manipulate that element.
行为是怎样工作的?通过XML我们可以把行为链接给任何页面里的元素,并操纵元素

DHTML behaviors do not use a <script> tag. Instead, they are using a CSS attribute called "behavior". This "behavior" specifies a URL to an HTC file which contains the actual behavior (The HTC file is written in XML).
DHTML行为不用<script>标签。他们用的是名为"behavior".的CSS属性。这个行为为包含着实际行为的HTC文件指定了URL(HTC是用XML写的)

Syntax
语法

behavior: url(some_filename.htc)

Note: The behavior attribute is only supported by IE 5 and higher, all other browsers will ignore it. This means that Mozilla, Firefox, Netscape and other browsers will only see the regular content and IE 5+ can see the DHTML behaviors.
注意:
行为属性只被IE5及其更高版本的浏览器支持,所有其他的浏览器都会忽略它。着意味着在Mozilla, Firefox, Netscape和其他浏览器上只能看到常规内容,而IE5还可以看到DHTML行为。


 

Example
例子

The following HTML file has a <style> element that defines a behavior for the <h1> element:
下面的HTML文件有一<style>元素,它定义了<h1>元素的行为

<html>

<head>
<style type="text/css">
h1 { behavior: url(behave.htc) }
</style>
</head>

<body>
<h1>Mouse over me!!!</h1>

</body>
</html>

The XML document "behave.htc" is shown below:
下面显示的是XML文档"behave.htc"

<attach for="element" event="onmouseover" handler="hig_lite" />

<attach for="element" event="onmouseout" handler="low_lite" />

<script type="text/javascript">
function hig_lite()
{
element.style.color='red'
}
function low_lite()
{
element.style.color='blue'
}
</script>

The behavior file contains a JavaScript and the event handlers for the script.
行为文件包含了Java描述语言脚本和对脚本的事件处理器

Try it yourself (mouse over the text in the example).
尝试一下 (把鼠标移动到文字上方).

The following HTML file has a <style> element that defines a behavior for elements with an id of "typing":
下面的HTML文件有一个<style>元素,它定义了ID为"typing"的元素的行为:

<html>
<head>
<style type="text/css">
#typing
{
behavior:url(typing.htc);
font-family:'courier new';
}
</style>
</head>

<body>

<span id="typing" speed="100">IE5 introduced DHTML behaviors.
Behaviors are a way to add DHTML functionality to HTML elements
with the ease of CSS.<br /><br />How do behaviors work?<br />
By using XML we can link behaviors to any element in a web page
and manipulate that element.</p>
</span>
</body>

</html>

The XML document "typing.htc" is shown below:
下面显示的是XML文档"typing.htc"

<attach for="window" event="onload" handler="beginTyping" />

<method name="type" />
<script type="text/javascript">
var i,text1,text2,textLength,t
function beginTyping()
{
i=0
text1=element.innerText
textLength=text1.length
element.innerText=""
text2=""
t=window.setInterval(element.id+".type()",speed)
}
function type()
{
text2=text2+text1.substring(i,i+1)
element.innerText=text2
i=i+1
if (i==textLength){clearInterval(t)}
}
</script>

尝试一下

XML 相关技术
w3pop.com / 2006-09-20

Below is a list of XML-related technologies.
下面是一系列XML相关技术

XHTML (Extensible HTML)
is a stricter and cleaner version of HTML.
是比HTML更严格更简洁的版本

XML DOM (XML Document Object Model)
defines a standard way for accessing and manipulating XML documents.
定义了访问和操作XML文档的一种标准方式.

XSL (Extensible Style Sheet Language)
- XSL consists of three parts: XSLT - a language for transforming XML documents, XPath - a language for navigating in XML documents, and XSL-FO - a language for formatting XML documents.
XSL包含三个部分:XSLT-转换XML文档的一种语言,XPath—在XML文档中进行操作的一种语言,XSL-FO –将XML文档格式化的语言

XSLT (XSL Transformations)
is used to transform XML documents into other XML formats, like XHTML.
用于把XML文档转换成其他XML格式,比如XHTML。

XPath
is a language for navigating in XML documents.
XPath—在XML文档中进行操作的一种语言

XSL-FO (Extensible Style Sheet Language Formatting Objects)
is an XML based markup language describing the formatting of XML data for output to screen, paper or other media.
一种在XML基础上的标记语言,它描述用于输出在屏幕,纸张,或其他媒体的XML数据进行的格式化

XLink (XML Linking Language)
is a language for creating hyperlinks in XML documents.
用于创造XML文档超链接的一种语言

XPointer (XML Pointer Language)
allows the XLink hyperlinks to point to more specific parts in the XML document.
(XML指示语言)允许可扩展超链接(XLink hyperlinks)指示XML文档中的特殊部分。

DTD (Document Type Definition)
 is used to define the legal elements in an XML document.
用于定义一份XML文档中的合法元素。

XSD (XML Schema) 
 is an XML-based alternative to DTDs.
是以XML为基础的可代替DTDS作用的标准

XForms (XML Forms)
uses XML to define form data.
用XML从数据中定义

XQuery (XML Query Language)
is designed to query XML data.
(XML查询语言)用于查询XML数据

SOAP (Simple Object Access Protocol)
is an XML-based protocol to let applications exchange information over HTTP.
(简单对象访问协议)以XML为基础的协议,可以让应用软件在HTTP协议下交换信息

WSDL (Web Services Description Language)
is an XML-based language for describing web services.
(网络服务描述语言)以XML为基础,用语描述网络服务的语言

RDF (Resource Description Framework)
 is an XML-based language for describing web resources.
(资源描述框架)以XML为基础的描述网络资源的语言

RSS (Really Simple Syndication)
is a format for syndicating news and the content of news-like sites.
组织新闻和类似新闻地址内容的一种格式

WAP (Wireless Application Protocol) 
 was designed to show internet contents on wireless clients, like mobile phones.
(无线应用协议)用于在无线用户终端上显示英特网内容,例如手机。

SMIL (Synchronized Multimedia Integration Language)
is a language for describing audiovisual presentations.
(同步多媒体综合语言)用于描述视听陈述的语言

SVG (Scalable Vector Graphics)
defines graphics in XML format.
(可升级矢量图)定义了XML格式的图形

XML 编辑器
w3pop.com / 2006-09-20

If you are serious about XML, you will benefit from using a professional XML Editor.
如果你对XML的使用非常在乎和认真,专业的XML编辑器可以帮你大忙。

XML is Text-based
XML是以文本为基础的

XML is a text-based markup language
XML是以文本为基础的标记语言

One great thing about XML is that XML files can be created and edited using a simple text-editor like Notepad.
XML挺不错的一点是用象Notepad.这样的简易的文本编辑器就可以创建和编辑XML文件。

However, when you start working with XML, you will soon find that it is better to edit XML documents using a professional XML editor.
然而,开始编辑XML后,你会很快发现用上专业XML编辑器来编辑XML文档会更好。


Why Not Notepad?
为什么不用记事本(Notepad)?

Many web developers use Notepad to edit both HTML and XML documents because Notepad is included with the most common OS and it is simple to use. Personally I often use Notepad for quick editing of simple HTML, CSS, and XML files.
许多网络开发者都用记事本(Notepad)编辑HTML和XML文档,因为Notepad有最普通的操作系统(Operation System—OS),且易于使用。我本人经常用记事本(Notepad)做些关于HTML,CSS,XML文件的简单编辑。

But, if you use Notepad for XML editing, you will soon run into problems.
但是,用记事本(Notepad)编辑XML很容易出问题。

Notepad does not know that you are writing XML, so it will not be able to assist you.
记事本(Notepad)不知道你在编写XML,所以它不会帮助你。


Why an XML Editor?
为什么需要一个XML编辑器?

Today XML is an important technology, and development projects use XML-based technologies like:
今天XML已经成长为一个非常重要的技术了,发展项目使用基于XML的技术,如:

  • XML Schema to define XML structures and data types
    XML计划去定义一个XML结构和数据形式
  • XSLT to transform XML data
    XSLT转换XML数据
  • SOAP to exchange XML data between applications
    SOAP在应用程序间交换XML数据
  • WSDL to describe web services
    WSDL描述具体的网络服务
  • RDF to describe web resources
    RDF描述具体的网络资源
  • XPath and XQuery to access XML data
    XPath和XQuery访问XML数据
  • SMIL to define graphics
    SMIL定义图像

To be able to write error-free XML documents, you will need an intelligent XML editor!
为了实现XML文件书写上的“零错误”,我们需要一个非常聪明的XML程序编辑器。


XML Editors
XML程序编辑器

Professional XML editors will help you to write error-free XML documents, validate your XML against a DTD or a schema, and force you to stick to a valid XML structure.
专业的XML程序编辑器将会帮助你实现XML文件书写的“零错误”,通过DTD或一个有效的格式实现XML的有效性,使你根据一个正确有效的XML结构去书写。

An XML editor should be able to:
一个XML编辑器应该具备:

  • Add closing tags to your opening tags automatically
    存在开始标签的,可以自动生成结束标签
  • Force you to write valid XML
    强制你用有效的XML格式书写
  • Verify your XML against a DTD
    通过DTD验证你书写的XML
  • Verify your XML against a Schema
    通过一个有效的格式验证你的XML
  • Color code your XML syntax
    根据XML不同的语法给不同的代码标记不同的颜色

Altova's XMLSpy

At W3Schools we have been using XMLSpy for many years. XMLSpy is our favorite XML editor. These are some of the features we especially like:
我们已经使用XMLSpy多年。XMLSpy是我们最喜欢的XML编辑器。下面是关于XMLSpy的一些具体优点:Easy to use

  • Easy to use
    使用方便
  • Syntax coloring
    用不同的颜色标记代码
  • Automatic tag completion
    自动生成结束标签
  • Automatic well-formed check
    自动检查格式证物
  • Easy switching between text view and grid view
    实现文本浏览和格子浏览的
  • Built in DTD and / or Schema validation
    内置DTD或固定格式验证有效性
  • Built in graphical XML Schema designer
    内置XML图像格式设计器
  • Powerful conversion utilities
    强大的转换元件
  • Database import and export
    数据库输入和输出
  • Built in templates for most XML document types
    内置大量的XML文件类型模板
  • Built in XPath analyzer
    内置XPath分析器
  • Full SOAP and WSDL capabilities
    具备SOAP和WSDL的全部功能
  • Powerful project management
    强大的项目管理器

XML 摘要
w3pop.com / 2006-09-20

This tutorial has taught you how to use XML to describe data.
这份教程教你如何用XML描述数据

You have learned that XML should be used to separate the data from the HTML code.
你已经知道XML应该用于从HTML代码中分离数据

You have also learned that XML can be used to exchange, share, and store data.
你也知道了XML可用于交换,共享,储存数据

For more information on XML, please look at our XML examples.
想知道更多关于XML的信息, 请看我们的 XML 举例.


Now You Know XML, What's Next?
现在你知道了XML,下步干什么呢?

The next step is to learn about the XML DOM and XSLT.
下步是了解有关XML DOM和XSLT

If you want to learn about validating XML, the next step is learning about DTD and XML Schema.
如果你想了解有关检验XML,下步是了解有关DTD和XML 格式(Schema).

XML DOM

The XML DOM defines a standard way for accessing and manipulating XML documents.
XML DOM定义了访问和操控XML文档的标准方式

The XML DOM is platform and language independent and can be used by any programming language like Java, JavaScript, and VBScript.
XML DOM是平台,且是一种独立语言,能被任何语言象Java, JavaScript, and VBScript.等被使用。

If you want to learn more about the DOM, please visit our XML DOM tutorial.
如果你想学习有关XML DOM方面的知识,请访问我们的 XML DOM 教程.

XSLT

XSLT is the style sheet language for XML files.
XSLT是XML文件的样式表语言

With XSLT you can transform XML documents into other formats, like XHTML.
通过XSLT你可以把XML文档转换成其他格式,例如XHML

If you want to learn more about XSLT, please visit our XSLT tutorial.
学习更多有关XSLT的知识,请访问我们的 XSLT教程 .

DTD 与 XML Schema

The purpose of a DTD is to define what elements, attributes and entities is legal in an XML document.
DTD 的目的是定义一份XML文档里哪些元素,属性和实体是合法的

With DTD, each of your XML files can carry a description of its own format with it.
通过DTD,每份XML文件都可以携带一份自己的格式说明

DTD can be used to verify that the data you receive, and your own data, is valid.
DTD可用于检验你所接受到的数据和你自己的数据是否有效

If you want to learn more about DTD, please visit our DTD tutorial.
学习更多DTD的知识,访问我们的 DTD 教程.

XML Schema will replace DTD.
XML格式(Schema)将会代替DTD

XML Schema is an XML based alternative to DTD.
XML格式(Schema)是以XML为基础的对DTD的“后备”

Unlike DTD, XML Schemas has support for datatypes, and XML Schema use XML Syntax.
DTD不同,XML格式(Schema)支持数据类型,并且XML格式(Schema)使用XML语法

If you want to learn more about XML Schema, please visit our XML Schema tutorial.
想要学习XML Schema,访问我们的 XML Schema教程 .

XML 实例
w3pop.com / 2006-09-20

Viewing XML Files
查看 XML 文件

In Firefox and Internet Explorer:
在 Firefox 和 Internet Explorer浏览器:

Open the XML file (typically by clicking on a link) - The XML document will be displayed with color-coded root and child elements. A plus (+) or minus sign (-) to the left of the elements can be clicked to expand or collapse the element structure. To view the raw XML source (without the + and - signs), select "View Page Source" or "View Source" from the browser menu.
打开XML文件(单击链接)。XML文件将会将由有颜色代码的根目录和子目录显示出来。单击左边列栏的加号或者减号可以展开或者缩回元素列表展示。如果要浏览原始的XML源代码(带加号和减号的),在浏览菜单上选择View Page Source"(浏览页面源代码)或"View Source"(浏览源代码)

In Netscape 6:
在 Netscape 6:

Open the XML file, then right-click in XML file and select "View Page Source". The XML document will then be displayed with color-coded root and child elements.
打开XML文件,然后在XML文件上鼠标右键单击,选择"View Page Source"(浏览页面源代码)。XML文件通过含有颜色代码根目录和子目录显示出来。

In Opera 7 and 8:
在 Opera 7 和 8:

In Opera 7: Open the XML file, then right-click in XML file and select "Frame" / "View Source". The XML document will be displayed as plain text. In Opera 8: Open the XML file, then right-click in XML file and select "Source". The XML document will be displayed as plain text.
在Opera 7里,打开XML文件,在XML文件上鼠标右键单击,选择"Frame" / "View Source"(框架/浏览源文件)。XML文件会以普通文本的形式展现出来。在Opera 8里,打开XML文件,在XML文件上鼠标右键单击,选择"Source"(源文件)。XML文件会以普通文本的形式展现出来。


Viewing XML Files
查看 XML文件

查看XML文件 (note.xml)
查看带有错误的XML文件
查看XML记录的CD目录
XML记录的植物分类
查看XML记录的伙食清单 

Viewing XML files with a dtd
查看使用了DTD的XML文件

使用了internet DTD的note.xml
使用了external DTD的note.xml 

The Microsoft XML parser
 Microsoft XML 解析器

查看一个简单的XML文件(xml_note.xml)
将相同的文件装在到解析器中
穿越文件的节点树
将相同的文件装在到HTML中

Displaying using JavaScript
使用JS显示XML数据

查看一个简单的XML文件 (xml_note.xml)
通过JavaScript对相同的文件格式化

XML and CSS
XML 应用 CSS

查看一个XML CD目录
查看相应的CSS文件
显示通过CSS样式表文件格式化过的CD目录

XML and XSL
XML 应用 XSL

查看XML food菜单
查看相应的XSL样式表
显示由XSL 样式表定义的food菜单 (IE6)
显示由XSL 样式表定义的food菜单 (IE5)

Data Binding
数据绑定

查看一个XML CD目录
将这个CD目录绑定到一张HTML表格中
添加 <thead>、 <tfoot>、 <tbody> 元素

Database Output
数据库输出

查看来自于数据库的XML输出结果

Displayed as HTML
显示为HTML格式

查看一个XML CD目录
展示CD目录如何在HTML元素中显示
展示CD目录如何在HTML表格中显示
展示如何对CD目录进行操作
一个简单的CD目录应用程序

Requesting XML data from a server
从服务器请求XML数据

向服务器发送一个请求
使用XML方式与服务器进行交流

Using the XMLHttpRequest Object
使用 XMLHttpRequest 对象

通过XML HTTP方式装载一个XML文件 (使用JavaScript方法)
通过XML HTTP方式装载一个XML文件  (使用VBScript方法)
通过XML HTTP方式将一个文本文件装载到一个div元素中 (使用JavaScript方法)
通过XML HTTP方式书写一个HEAD请求 (使用JavaScript方法)
通过XML HTTP方式书写一个指定的HEAD请求 (使用JavaScript方法)

XML Behaviors
XML 行为

XML行为