XML简易教程之二

2005-07-28 22:45:34  作者:  来源:中国站长学院   文字大小:】【】【

文档格式的排错:我妈妈_的清单中有数十条菜谱,甚至数百条。如果产生一个致命错误,排错将非常困难 - 你将一行一行地寻找丢失的标记符。如果使用几层嵌套,发现错误将很困难。

但是可以找到很好的帮助。分析器 - XML代码和报告格式错误的应用程序可以在网上免费得到。其中最好的是Lark,它的作者是由Tim Bray - XML规范的技术编辑和极力鼓吹者,地球上最聪明的人之一。

我用Lark分析下面的代码。注意"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 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>

下面是分析器返回的结果:

Error Report

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

... assumed </item>

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

有了这种信息,找到错误将不会成为问题。那么XML文件的有效性是指什么呢?

实现有效性最终我们将在组织良好的XML文档中加入信息。实际上,我们有很多事要做 - 仍然有危机潜伏 - 虽然XML文件组织良好,
但还可能丢失关键信息。看看下面的例子:

<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分析器也不会发现问题。管理过哪怕是最和善的数据库的人都知道我们人类常犯的错误:如果有机会,我们会丢掉关键信息并加入无用的废话。这就是为什么XML的发明者引入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)>
]>
这些代码起初看起来不够友好,但当把它分解时却能看出其中的意义。让我们详细解释之:

<!DOCTYPE list [

这行是说,包含在方括号中的是具有根元素<list>的某个文档的DTD。如我们以前提到的,根元素包含所有其它元素。

<!ELEMENT recipe (recipe_name, meal, ingredients, directions)>

这行定义了<recipe>标记符。圆括号是说其中的四种标记符必须按照顺序出现在<recipe>标记符中。

<!ELEMENT meal (#PCDATA, course?)>

这行需要详细的解释。我定义了以下的结构:

<meal>Here the meal name is mandatory
<course


相关文章