XML 语法
编写 XML 必须遵循一些简单的语法规则,主要列示如下:
- 每个开始标记 (opening tag) 必须有结束标记 (closing tag)
- 一个 XML 文档只能有一个根元素 (root element)
- 所有 XML 元素必须正确嵌套
- 属性值必须加引号
- 一个元素不能有相同的属性 (attribute)
XML 标记 (tag) <script type=text/javascript>arw("]pi", ark5);</script> ,元素 (element) 和属性 (attribute)
> 结束,在 XML。
在解释上面的语法规则之前,我们先解释一些 XML 最常用的术语:
- 标记 (开始标记 opening tag <script type=text/javascript>arw("&cz", ark7);</script> ,结束标记 closing tag)
- 元素 (element)
- 属性 (attribute)
<books ISBN ="9787544238212">
<title>The book thief</title>
<price <script type=text/javascript>arw(">Pe4q", ark2);</script> >25</price>
<quantity>10</quantity>
</books>
上面是一个简单的 XML 文档,我们看到有很多这样的相同特征的字符串,<title>,<price>,<quantity>,</books> 等等。
它们都是由小于号 < 开始,由大于号 <script type=text/javascript>arw("=2Utj", ark3);</script> > 结束 <script type=text/javascript>arw("]52-Za", ark4);</script> ,在 XML 文档里,我们将其称为 XML 标记 (tag)。
标记又有开始标记和结束标记之分 <script type=text/javascript>arw("|^7", ark0);</script> 。
开始标记由 < 开始,由 > 结束 <script type=text/javascript>arw("*Il", ark7);</script> 。比如 <script type=text/javascript>arw("=2xtj", ark3);</script> <title>,<price>,<quantity>。
结束标记由 <script type=text/javascript>arw("=2xtj", ark3);</script> </ 开始,由 > 结束 <script type=text/javascript>arw("*aa", ark6);</script> 。比如</title>,</price>,</quantity>。
比如上面的例子中, 开始标记 <script type=text/javascript>arw(">0JUh", ark6);</script> <price?
而 XML 元素 (element) 指的是从一个开始标记到它的结束标记的一段内容。
比如 <title <script type=text/javascript>arw("=jCV2", ark5);</script> >The book thief <script type=text/javascript>arw("?M93p", ark1);</script> </title>
就是一个元素。
元素是 XML 文档的基本单位 <script type=text/javascript>arw("}M0", ark3);</script> ,一个 XML 文档可以由一个或者多个元素构成。
一个元素可以带有属性 (attribute) <script type=text/javascript>arw("}M0", ark3);</script> ,属性写在开始标记里 <script type=text/javascript>arw("[q9", ark0);</script> ,写在元素名称的后面。比如
<books ISBN ="9787544238212">
其中 ISBN ="9787544238212"
就 <script type=text/javascript>arw("|FB", ark5);</script> 是 books 元素的一个属性, ISBN <script type=text/javascript>arw("|VV", ark4);</script> 是属性的名称,9787544238212 是属性值 <script type=text/javascript>arw("%2o", ark1);</script> ,属性值必须加引号 <script type=text/javascript>arw("*Il", ark7);</script> 。
XML 的语法规则
接下来,我们逐个解释 XML 文档的语法规则 <script type=text/javascript>arw("+tA", ark4);</script> 。
每个开始标记 (opening tag) 必须有结束标记 (closing tag)
比如上面的例子中, 开始标记 <price <script type=text/javascript>arw(">mTUT", ark7);</script> > 必须有相应的结束标记 </price <script type=text/javascript>arw("=2Utj}0a", ark3);</script> >。
一个 XML 文档只能有一个根元素 (root element)
quantity 则 <script type=text/javascript>arw(",aL", ark1);</script> 是 books.
XML 文档是树状结构的,象一棵节点树。比如上面例子中,books 就 <script type=text/javascript>arw("|FB", ark5);</script> 是根元素,而 title,price,quantity 则是 books 的子节点。
如果写成下面这样,一个文档有两个 books 根元素,就出错了。
<?xml version="1.0" encoding="UTF-8"? <script type=text/javascript>arw(">mTUT", ark7);</script> >
<books ISBN ="9787544238212">
<script type=text/javascript>arw("=TzFm", ark4);</script> <title>偷书贼</title>
<price>25</price>
<script type=text/javascript>arw(">goo1", ark0);</script> <quantity>10 <script type=text/javascript>arw("=TzFm", ark4);</script> </quantity>
</books>
<books ISBN ="978758225">
<title>BlaBlar.com</title>
<price>100</price>
<script type=text/javascript>arw(">m2UT5Xm0Uu^b=JphsMK@zZ^G=QjiJehRUe@zs^G", ark7);</script> <quantity>12</quantity>
</books>
所有 XML 元素必须正确嵌套
正确的嵌套:
<books><title>blablar.com</title></books>
错误的嵌套:
<books <script type=text/javascript>arw(">mTUT@zZ^G", ark7);</script> ><title <script type=text/javascript>arw("=jCV2", ark5);</script> >blablar <script type=text/javascript>arw(">H", ark2);</script> .com <script type=text/javascript>arw(">Pr4q", ark2);</script> </books></title>
属性值必须加引号
一个 XML 元素可以带有属性 (attribute),属性写在开始标记里,写 <script type=text/javascript>arw("}WE", ark1);</script> 在元素名称的后面。比如
<books ISBN ="9787544238212">
其中 ISBN ="9787544238212"
就 <script type=text/javascript>arw(",aL", ark1);</script> 是 books 元素的一个属性, ISBN <script type=text/javascript>arw("|FB", ark5);</script> 是属性的名称,9787544238212 是属性值,属性值必须加引号。
一个元素 <script type=text/javascript>arw("|6p", ark1);</script> 不能有相同的属性 (attribute)
下面的写法是错误的,因为一个 XML 元素不能有两个相同的属性名称,虽然属性值不同。
<books ISBN ="9787544238212" ISBN ="97875442dr">
<script type=text/javascript>arw("-gy", ark7);</script> 不过 <script type=text/javascript>arw("%2o", ark1);</script> ,如果你将 ISBN 小写,写成如下,就 <script type=text/javascript>arw("&wX", ark3);</script> 是对的。
因为 XML 文档 <script type=text/javascript>arw("}AQ", ark7);</script> 是区分大小写的 (case sensitve) <script type=text/javascript>arw("+9U", ark5);</script> 。ISBN 和 isbn 表示两个不同的属性。
<books ISBN ="9787544238212" isbn ="97875442dr">