要談 XML 文件,我們不妨先看一個最簡單的例子:
<person>
Alan Turing
</person>
這就是一份格式正確的 XML 文件,其中 <person> 與 </person> 分別是起始標記與結束標記。
要特別注意的是,起始與結束標記的名稱必須一致,但名稱本身則完全自由,這與 HTML 大不相同。HTML 的標記名稱是固定有限的,而 XML 允許你自訂語意,你可以用 person 標記一個人,也能用 cat 標記一隻貓。
標記之間的 Alan Turing 就是資料內容,其中 Alan 與 Turing 之間的空格同樣屬於資料,也就是說標記內的空白不會被忽略。
有時我們需要一個不含任何資料的元素(所謂「元素」,指的是包含起始與結束標記在內的整段內容),例如:
<person></person>
這是空標記的一種寫法,但 XML 提供了更簡潔的表示方式:
<person/>
另外要牢記:XML 是區分大小寫的,這也和 HTML 不同。<Person> 與 <PERSON> 是兩個完全不同的標記,如果你的元素以 <person> 開始,就不能用 </Person> 作為結束標記。
前面的例子只含一個元素,接著我們看一個更複雜的例子,並借此理解 XML 的樹狀概念:
<person>
<name>
<first_name>Alan</first_name>
<last_name>Turing</last_name>
</name>
<profession>computer scientist</profession>
<profession>mathematician</profession>
<profession>cryptographer</profession>
</person>
最外層仍然是一個 person 元素,但這次它含有 4 個子元素:1 個 name 與 3 個 profession。我們稱 person 是 name 與 profession 的父元素,而 name 又是 first_name 與 last_name 的父元素。
從這個例子可以看到,標記的嵌套是被允許的。但「重疊標記」則是非法的,例如:
<strong><em>this common example from Html</strong></em>
正確的寫法應該是:
<strong><em>this common example from Html</em></strong>
由於父子元素的關係,再加上任何 XML 文件都只能有一個根元素(也就是沒有父元素的元素),整個結構看起來就像一棵樹。
接著看一個標記與文字混排的例子:
<biography>
<name><first_name>Alan</first_name> <last_name>Turing</last_name>
</name> was one of the first people to truly deserve the name
<emphasize>computer scientist</emphasize>. Although his contributions
to the field are too numerous to list, his best-known are the
eponymous <emphasize>Turing Test</emphasize> and
<emphasize>Turing Machine</emphasize>.
<definition>The <term>Turing Test</term> is to this day the standard
test for determining whether a computer is truly intelligent. This
test has yet to be passed. </definition>
<definition>The <term>Turing Machine</term> is an abstract finite
state automaton with infinite memory that can be proven equivalent
to any other finite state automaton with arbitrarily large memory.
Thus what is true for a Turing Machine is true for all equivalent
Machines no matter how implemented.
</definition>
</biography>
這是一份合法的 XML 文件,代表標記與內容可以混排。不過這種格式在程式處理上相當麻煩,因此並不建議採用。
屬性能為元素提供額外的資訊,看例子:
<person born="1912-06-23" died="1954-06-07">
Alan Turing
</person>
其中 born 與 died 就是屬性,born 是屬性名,1912-06-23 是屬性值。屬性值必須用雙引號括起來,當然也可以用單引號:
<person died = '1954-06-07' born = '1912-06-23' >
Alan Turing
</person>
使用單引號的好處是,你可以在屬性值裡放入雙引號。
那麼,資料該放在屬性裡,還是直接放在標記之間呢?看看這個對照的例子:
<person>
<name first="Alan" last="Turing"/>
<profession value="computer scientist"/>
<profession value="mathematician"/>
<profession value="cryptographer"/>
</person>
這是一個各方都說得通的問題,實務上可依需求自行決定。但要注意:同一個元素不能擁有兩個相同名稱的屬性,例如下面就是非法的:
<person born="1912-06-23" born="1954-06-07">
Alan Turing
</person>
由於 < 和 > 已被用作標記符號,一般情況下資料中不能直接出現它們,而要用 < 和 > 代替。這其實和 HTML 的處理方式相同,例如 & 要寫成 &,雙引號則用 "。
至於註解,寫法與 HTML 一致:
<!-- 左邊是註解開始標記,右邊是結束標記 -->
但要留意:註解內容不能包含 --,其中的任何標記都會被忽略,而且註解不能出現在元素的標記內部。
所有的 XML 文件都可能(也應該)以一段 XML 聲明開頭:
<?XML version="1.0" encoding="ASCII" standalone="yes"?>
<person>
Alan Turing
</person>
如果包含 XML 聲明,它必須位於文件最前面,前面不能有任何空白或註解。嚴格來說這段聲明並非必要,但它在文件處理過程中確實能帶來一些優化。其屬性說明如下: