当前位置: 首页 > 网络学院 > XML相关教程 > XML > XML 语法规则

XML
XML 介绍
XML 的用途
XML 语法规则
XML 元素
XML 属性
XML 的有效性验证
XML 校验器
支持 XML 的浏览器
浏览 XML 文件
用 CSS 显示 XML
用 XSL 显示 XML
XML 数据岛
XML 解析器
现实中的 XML
XML 命名空间
XML CDATA
XML 服务器
XML 应用程序
XMLHttpRequest 对象
XML 保存数据

XML 语法规则


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 1088 ::
收藏到网摘: n/a

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 的信息吗? 难道你不赞同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>是不同的。

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:
如同HTML一样,XML元素可以使用包含 “名称 / 值” 这样成对出现的属性值。在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.
第一份文档的错误是note元素里的日期属性并未被写在引号里。

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标签是可以被特殊处理的。标签可以是可见的,也可以是不可见的;或者包含一种功能性的函义,这得取决于应用软件本身的特性。 

评论 (2) 1 All

登陆 | 还没注册?