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