XML 语法规则

 

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>.是不同的.

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值