DTD无法验证

时间:2013-09-21 20:18:44

标签: xml dtd xml-dtd

我正在尝试学习XML和DTD,我刚刚学习了DTD的w3c教程,并尝试使用DTD在XML中实现配方场景,这是在DTD文件中写的:

<?xml version="1.0" encoding="UTF-8"?>
<!ENTITY  RECIPE (NAME,INGREDIENTS,INSTRUCTIONS) >
<!ENTITY NAME ("Baking Powder Biscuits") >
<!ENTITY  INGREDIENTS SYSTEM "Ingredients.dtd"  >
<!ENTITY  INSTRUCTIONS SYSTEM "Instructions.dtd" >

<!ATTLIST RECIPE UNITS "16 BISCUITS" #FIXED> 

当我尝试验证时,解析器在第2行中给出了一个错误,说明在decl中打开“缺失”,不能说明错误,请帮忙。

由于

这是xml文件'recipe':

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE RECIPE SYSTEM "recipe.dtd">

这是食谱dtd

<?xml version="1.0" encoding="UTF-8"?>
<!ENTITY  RECIPE NAME INGREDIENTS,INSTRUCTIONS>
<!ENTITY NAME "Baking Powder Biscuits" >
<!ENTITY  % INGREDIENTS SYSTEM "Ingredients.dtd"  >
<!ENTITY  % INSTRUCTIONS SYSTEM "Instructions.dtd" >

<!ATTLIST RECIPE UNITS CDATA #FIXED "16 BISCUITS" >

这是其他dtd ingreduents:

<?xml version="1.0" encoding="UTF-8"?>
<!ENTITY  INGREDIENTS  ("2 cups flour","1 tablespoon sugar", "1/2 teaspoon salt", "1/2 cup vegetable shortening", "4 teaspoon baking powder", "2/3 cup milk")>

2 个答案:

答案 0 :(得分:1)

你有RECIPE的ENTITY声明,但它看起来应该是ELEMENT声明(基于模型和RECIPE的ATTLIST):

<!ELEMENT RECIPE (NAME,INGREDIENTS,INSTRUCTIONS) >

NAME ENTITY声明看起来确实应该是ENTITY,但您需要删除括号:

<!ENTITY NAME "Baking Powder Biscuits">

此外,如果指向.dtd文件的两个ENTITY声明实际上包含其他声明,则它们应为parameter entities

<!ENTITY % INGREDIENTS SYSTEM "Ingredients.dtd"  >
<!ENTITY % INSTRUCTIONS SYSTEM "Instructions.dtd" >

您可以使用%INGREDIENTS;%INSTRUCTIONS;来引用它们。

RECIPE的ATTLIST也不正确。对于固定值,请尝试:

<!ATTLIST RECIPE UNITS CDATA #FIXED "16 BISCUITS">

答案 1 :(得分:1)

您的文档类型定义文件不是XML文档实例;它们不应该以XML声明开头。一旦从中删除了XML声明,就会遇到Daniel Haley发现的问题。