文檔格式的排錯
我媽媽_的清單中有數(shù)十條菜譜,甚至數(shù)百條。如果產(chǎn)生一個致
命錯誤,排錯將非常困難 - 你將一行一行地尋找丟失的標(biāo)記
符。如果使用幾層嵌套,發(fā)現(xiàn)錯誤將很困難。
但是可以找到很好的幫助。分析器 - XML代碼和報告格式錯誤
的應(yīng)用程序可以在網(wǎng)上免費得到。其中最好的是Lark,它的作
者是由Tim Bray - XML規(guī)范的技術(shù)編輯和極力鼓吹者,地球上最
聰明的人之一。
我用Lark分析下面的代碼。注意"chocolate chips"和它的關(guān)閉
標(biāo)記符出現(xiàn)在</ingredients> 標(biāo)記符中的位置有錯誤:
<?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 overn 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>
下面是分析器返回的結(jié)果:
Error Report
Line 17, column 22: Encountered </ingredients> expected </item>
... assumed </item>
Line 18, column 36: Encountered </item> with no start-tag.
有了這種信息,找到錯誤將不會成為問題。那么XML文件的有效性
是指什么呢?
實現(xiàn)有效性
最終我們將在組織良好的XML文檔中加入信息。實際上,我們
有很多事要做 - 仍然有危機潛伏 - 雖然XML文件組織良好,
但還可能丟失關(guān)鍵信息。看看下面的例子:
<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>
這份菜譜中沒有包含ingredient,而且因為它組織良好,所以
Lark分析器也不會發(fā)現(xiàn)問題。管理過哪怕是最和善的數(shù)據(jù)庫的
人都知道我們?nèi)祟惓7傅腻e誤:如果有機會,我們會丟掉關(guān)鍵
信息并加入無用的廢話。這就是為什么XML的發(fā)明者引入DTD -
文檔類型定義(Document Type Definition)。DTD提供了一種保
證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 item (#PCDATA)>
<!ELEMENT subitem (#PCDATA)>
<!ELEMENT directions (#PCDATA)>
]>
這些代碼起初看起來不夠友好,但當(dāng)把它分解時卻能看出其中
的意義。讓我們詳細(xì)解釋之:
<!DOCTYPE list [
這行是說,包含在方括號中的是具有根元素<list>的某個文檔的
DTD。如我們以前提到的,根元素包含所有其它元素。
<!ELEMENT recipe (recipe_name, meal, ingredients, directions)>
這行定義了<recipe>標(biāo)記符。圓括號是說其中的四種標(biāo)記符必
須按照順序出現(xiàn)在<recipe>標(biāo)記符中。
<!ELEMENT meal (#PCDATA, course?)>
這行需要詳細(xì)的解釋。我定義了以下的結(jié)構(gòu):
<meal>Here the meal name is mandatory
<course>One course name may appear, but it is not
mandatory</course>
</meal>
我這樣做是因為,按照我的想法,午餐不一定特定某道菜,但
是晚餐可能要指出開胃食品、主菜和餐后甜點。通過指定
#PCDATA - 表示經(jīng)過分析的字符數(shù)據(jù)(即非二進制數(shù)據(jù))來
實現(xiàn)這個功能。這里,#PCDATA是文本 - 例如,“dinner”。
"course"后面的問號表示0或1對<course>標(biāo)記符將出現(xiàn)在<meal>
標(biāo)記符內(nèi)。
現(xiàn)在讓我們看看下一行:
<!ELEMENT ingredients (item )>
這里的加號表示至少有一對<item>標(biāo)記符應(yīng)出現(xiàn)在<ingredients>
標(biāo)記符內(nèi)。
我們感興趣的最后一行是:
<!ELEMENT item (#PCDATA, sub_item*)>
我把sub_item*作為一項安全措施。除了要求每個item的文本之
外,我希望計算每個item的內(nèi)容的數(shù)量。星號是說在<item>標(biāo)記
符中可以有子條目的數(shù)目。我不需要Chocolate Chip Bars菜譜的
任何子條目,但是當(dāng)它的組成成分很復(fù)雜時就用得著。
現(xiàn)在讓我們把這些放在一起看看我們能得到什么。
DTD的完整例子
下面是一個完整的例子。我把另一個菜譜加入文件內(nèi),并為
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-sweetchoc. chips</item>
</ingredients>
<directions>
Preheat oven to 350 degrees. Melt butter;
combinewith brown sugar and vanilla in large mixing bowl.
Set aside to cool. Combine flour, baking powder, andsalt;
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 30minutes 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做出的限制。換句話
說,我們要保證文檔的有效性。
為了達(dá)到這個目的,我們需要另一個工具:有效性分析器。微軟
的MSXML,一個基于Java的程序,使用容易又工作得很好。上面的
文檔經(jīng)過這個程序的檢查后沒有發(fā)現(xiàn)錯誤。但是如果我檢查一個
ingredient標(biāo)記符中沒有包含條目的菜譜,將會返回以下信息:
ingredients is not complete. Expected elements [item].
新聞熱點
疑難解答