DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> XML學習教程 >> XML詳解
XML 簡易教程(二):文件格式的除錯與 DTD 驗證
編輯:XML詳解     

先學會除錯

想像一份數十條甚至數百條食譜的文件,一旦漏掉一個標記符,人工逐行比對將非常痛苦;若又牽涉多層嵌套,要找出問題所在更是難上加難。

好消息是你不必硬撐。分析器(parser)——能檢查 XML 碼並回報格式錯誤的應用程式——在網路上可以免費取得。其中最有名的是 Lark,作者 Tim Bray 正是 XML 規範的技術編輯與主要推手之一。

以下這段程式碼有一處錯誤:「chocolate chips」的關閉標記位置放錯了,被寫到了 </ingredients> 之後:

<?XML version="1.0"?>
<list>
<recipe>
<author>Carol Schmidt</author>
<recipe_name>Chocolate Chip Bars</recipe_name>
<meal>Dinner
<course>Dessert</course>
</meal>
<ingredients>
<item>2/3 C butter</item>
<item>2 C brown sugar</item>
<item>1 tsp vanilla</item>
<item>1 3/4 C unsifted all-purpose flour</item>
<item>1 1/2 tsp baking powder</item>
<item>1/2 tsp salt</item>
<item>3 eggs</item>
<item>1/2 C chopped nuts</item>
<item>
</ingredients>2 cups (12-oz pkg.) semi-sweet choc.
chips</item>
<directions>
Preheat oven to 350 degrees. Melt butter;
combine with brown sugar and vanilla in large mixing bowl.
Set aside to cool. Combine flour, baking powder, and salt; set aside.
Add eggs to cooled sugar mixture; beat well. Stir in reserved dry
ingredients, nuts, and chips.
Spread in greased 13-by-9-inch pan. Bake for 25 to 30 minutes
until golden brown; cool. Cut into squares.
</directions>
</recipe>
</list>

分析器回報的結果大致如下:

Error Report

Line 17, column 22: Encountered </ingredients> expected </item>
... assumed </item>

Line 18, column 36: Encountered </item> with no start-tag.

有了行號與欄位,定位錯誤就不再是難事。

格式良好不等於內容正確

但即使格式完全無誤,仍有危機潛伏。看看這份文件:

<recipe>
<author>Carol Schmidt</author>
<recipe_name>Chocolate Chip Bars</recipe_name>
<meal>Dinner <course>Dessert</course> </meal>
<ingredients> </ingredients>
<directions>Melt butter; combine with, etc. ... </directions>
</recipe>

這份食譜完全沒有列出任何食材,但因為格式良好,Lark 不會報錯。管理過任何資料庫的人都知道人類常犯的毛病:只要有機會,我們就會丟掉關鍵資訊、塞進無用的廢話。這正是 XML 的發明者引入 DTD(Document Type Definition,文件類型定義)的原因——它提供一種機制,確保 XML 大致上是你想要的樣子。

為食譜寫一份 DTD

<!DOCTYPE list [
<!ELEMENT recipe (recipe_name, author, meal, ingredients, directions)>
<!ELEMENT ingredients (item+)>
<!ELEMENT meal (#PCDATA, course?)>
<!ELEMENT item (#PCDATA, sub_item*)>
<!ELEMENT recipe_name (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT course (#PCDATA)>
<!ELEMENT sub_item (#PCDATA)>
<!ELEMENT directions (#PCDATA)>
]>

初看不太友善,拆解之後其實很有條理:

<!DOCTYPE list [ 宣告方括號內是某個以 <list> 為根元素之文件的 DTD。根元素涵蓋其他所有元素。

<!ELEMENT recipe (recipe_name, author, meal, ingredients, directions)> 定義 recipe 元素。圓括號表示其中五個元素必須依序出現在 recipe 之中。

<!ELEMENT meal (#PCDATA, course?)> 這行需要多解釋一下。它對應的結構是:

<meal>餐別名稱為必填
<course>可出現零或一次的菜色名稱</course>
</meal>

這樣設計的理由是:午餐未必需要指定某道菜,但晚餐通常會點出開胃食品、主菜與餐後甜點。#PCDATA 代表「已解析的字元資料」(即非二進位資料),此處就是純文字,例如 "Dinner"。course 後面的問號表示 0 或 1 次。

<!ELEMENT ingredients (item+)> 加號表示 ingredients 之中至少要有一組 item 標記。

<!ELEMENT item (#PCDATA, sub_item*)> 星號是我刻意留的安全措施:除了要求每個 item 都有文字內容,也允許掛上任意數量的子條目。Chocolate Chip Bars 這份食譜用不到子條目,但遇到成分層次較複雜的食譜時就很實用。

完整範例

下面把兩個食譜與 DTD 放在一起,並加上註解。注意第二份食譜用到了子條目:

<?XML version="1.0"?>
<!--This starts the DTD. The first four lines address document structure-->
<!DOCTYPE list [
<!ELEMENT recipe (recipe_name, author, meal, ingredients, directions)>
<!ELEMENT ingredients (item+)>
<!ELEMENT meal (#PCDATA, course?)>
<!ELEMENT item (#PCDATA, sub_item*)>
<!--These are the remaining elements of the recipe tag-->
<!ELEMENT recipe_name (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT directions (#PCDATA)>
<!--The remaining element of the meal tag-->
<!ELEMENT course (#PCDATA)>
<!--The remaining element of the item tag-->
<!ELEMENT sub_item (#PCDATA)>
]>

<?XML version="1.0"?>
<list>
<recipe>
<author>Carol Schmidt</author>
<recipe_name>Chocolate Chip Bars</recipe_name>
<meal>Dinner
<course>Dessert</course>
</meal>
<ingredients>
<item>2/3 C butter</item>
<item>2 C brown sugar</item>
<item>1 tsp vanilla</item>
<item>1 3/4 C unsifted all-purpose flour</item>
<item>1 1/2 tsp baking powder</item>
<item>1/2 tsp salt</item>
<item>3 eggs</item>
<item>1/2 C chopped nuts</item>
<item>2 cups (12-oz pkg.) semi-sweet choc. chips</item>
</ingredients>
<directions>
Preheat oven to 350 degrees. Melt butter;
combine with brown sugar and vanilla in large mixing bowl.
Set aside to cool. Combine flour, baking powder, and salt; set aside.
Add eggs to cooled sugar mixture; beat well.
Stir in reserved dry ingredients, nuts, and chips.
Spread in greased 13-by-9-inch pan.
Bake for 25 to 30 minutes until golden brown; cool.
Cut into squares.
</directions>
</recipe>
<recipe>
<recipe_name>Pasta with Tomato Sauce</recipe_name>
<meal>Dinner
<course>Entree</course>
</meal>
<ingredients>
<item>1 lb spaghetti</item>
<item>1 16-oz can diced tomatoes</item>
<item>4 cloves garlic</item>
<item>1 diced onion</item>
<item>Italian seasoning
<sub_item>oregano</sub_item>
<sub_item>basil</sub_item>
<sub_item>crushed red pepper</sub_item>
</item>
</ingredients>
<directions>
Boil pasta. Sauté garlic and onion. Add tomatoes. Serve hot.
</directions>
</recipe>
</list>

有效性驗證

有了 DTD,文件就會被檢查是否符合 DTD 所設下的限制——換句話說,我們要確保文件的有效性。為此需要另一個工具:有效性分析器(Validating Parser)。微軟的 MSXML 使用容易且表現良好,上述文件通過它檢查後沒有發現錯誤。

但如果拿一份 ingredients 裡沒有任何 item 的食譜去驗證,就會得到類似這樣的訊息:

ingredients is not complete. Expected elements [item].

這正是 DTD 的價值所在:格式良好只保證語法無誤,DTD 才能保證語意完整。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved