验证XML和DTD时出现问题

时间:2017-01-22 15:30:15

标签: xml dtd

我已经制作了这个食谱列表,但由于某些原因,当我尝试在Oxygen XML Editor中验证文档时,我收到了大量的错误消息。 氧气给了我以下错误:

元素类型"成分"不得多次申报 元素类型"名称"不得多次申报 元素类型"数量"不得多次申报 意想不到的元素"成分"。父元素的内容必须匹配: (名称+,成分+,数量+,装饰,说明,必要性,准备)

这是我的代码:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE recipes SYSTEM "recipes.dtd">

<recipes>
    <cocktail>

      <name>Hells Bells</name>

      <ingredients>Batida de Coco</ingredients>
      <quantity> 4cl </quantity>

      <ingredients>caramel sirup</ingredients>
      <quantity> 2 cl </quantity>

      <ingredients>Milk</ingredients>
      <quantity> 8 cl </quantity>

      <ingredients>Strawberry puree</ingredients>
      <quantity> 4 cl </quantity>

      <decoration>Decoration: Strawberry blood with spiders.</decoration>
      <instructions>Instructions: All ingredients should be shaken.</instuctions>

      </cocktail> 

      <cocktail>

        <name>Feuerzangenbowle</name>

        <ingredients>red wine</ingredients>
        <quantity> 1000 ml </quantity>

        <ingredients>cinnamon</ingredients>
        <quantity> 1 </quantity>

        <ingredients>Carnations</ingredients>
        <quantity> 3 </quantity>

        <necessity>500 ml Pott</necessity>

        <preparation>Season with cinnamon stick and cloves.</preparation>

    </cocktail> 

    <appetizer>

      <name>Fresh glass</name>

      <ingredients>avocado</ingredients>
      <quantity> 1 </quantity>

      <ingredients>salmon</ingredients>
      <quantity> 100 g </quantity> 

      <ingredients>chives</ingredients>
      <quantity> stems </quantity>

      <ingredients>lemon</ingredients>
      <quantity> 1 </quantity>

    </appetizer>

    <appetizer>

      <name>Merceiles Hummus</name>

      <ingredients>Chickpeas</ingredients>
      <quantity> 400 g </quantity>

      <ingredients>olive oil</ingredients>
      <quantity> 2 TS </quantity>

      <ingredients>salt</ingredients>
      <quantity> 1 TS </quantity>

      <ingredients>Pepper powder</ingredients>
      <quantity> 1 TS </quantity>

   </appetizer>

   <maindish>

      <name>Pickled salmon</name>

      <ingredients>salmon</ingredients>
      <quantity> 2,5 kg </quantity>

      <ingredients>pepper</ingredients>
      <quantity> 1 TS </quantity>

      <ingredients>salt</ingredients>
      <quantity> 4 TS </quantity>

      <ingredients>honey</ingredients>
      <quantity> 4 TS </quantity>

   </maindish>

   <maindish>

      <name>Beef fillet</name>

      <ingredients>Beef fillet</ingredients>
      <quantity> 1,5 kg </quantity>

      <ingredients>Butterfelt</ingredients>
      <quantity> 20 g </quantity>

      <ingredients>thyme</ingredients>
      <quantity> 4 branches </quantity>

   </maindish>

   <dessert>

      <name>Chocolate Muffins</name>

      <ingredients>chocolate</ingredients>
      <quantity> 225 g </quantity>

      <ingredients>butter</ingredients>
      <quantity> 115 g </quantity>

      <ingredients>vanillasuger</ingredients>
      <quantity> 1 TS </quantity>

      <ingredients>sugar</ingredients>
      <quantity> 100 g </quantity>

   </dessert>

   <dessert>

     <name>Tiramisu</name>

     <ingredients>sugar</ingredients>
     <quantity> 100 g </quantity>

     <ingredients>yolk</ingredients>
     <quantity> 4 </quantity>

     <ingredients>egg whites</ingredients>
     <quantity> 2 </quantity>

     <ingredients>Amaretto</ingredients>
     <quantity> 2 cl </quantity>

   </dessert>

</recipes>

这是DTD:

<!-- DTD document recipes.xml  -->

<!ELEMENT recipes (cocktail+, appetizer+, maindish+, dessert+)>

<!ELEMENT cocktail (name+, ingredients+, quantity+, decoration, instructions, neccessity, preparation)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT ingredients (#PCDATA)>
<!ELEMENT quantity (#PCDATA)>
<!ELEMENT decoration (#PCDATA)>
<!ELEMENT instructions (#PCDATA)>
<!ELEMENT neccessity (#PCDATA)>
<!ELEMENT preparation (#PCDATA)>

<!ELEMENT appetizer (name+, ingredients+, quantity+)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT ingredients (#PCDATA)>
<!ELEMENT quantity (#PCDATA)>

<!ELEMENT maindish (name+, ingredients+, quantity+)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT ingredients (#PCDATA)>
<!ELEMENT quantity (#PCDATA)>

<!ELEMENT dessert (name+, ingredients+, quantity+)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT ingredients (#PCDATA)>
<!ELEMENT quantity (#PCDATA)>

我真的不知道该怎么做了,非常感谢你的帮助! 提前感谢您的回答!

P.S - XML新手

1 个答案:

答案 0 :(得分:1)

element type "ingredients" must not be declared more than once
element type "name" must not be declared more than once
element type "quantity" must not be declared more than once

这些错误非常自我解释;你只能在DTD中声明一次元素(<!ELEMENT声明)。

unexpected element "ingredients". The content of the parent element must match:
(name+, ingredients+, quantity+, decoration, instructions, neccessity, preparation)

此错误表示ingredients元素不合适。 ingredientscocktail)的父级必须与DTD中的模型匹配。在这种情况下,cocktail必须具有:

  • 一个或多个name元素,后跟
  • 一个或多个ingredients元素,后跟
  • 一个或多个quantity元素,后跟
  • 只有一个decoration元素,后跟
  • 只有一个instructions元素,后跟
  • 只有一个neccessity元素,后跟
  • 只有一个preparation元素

查看您的XML,这看起来并不像您想要的那样。你可能想要看起来更像这样的东西:

(name+, (ingredients, quantity)+, decoration?, instructions?, necessity?, preparation?)

这是:

  • 一个或多个name元素,后跟
  • 一个或多个
    • 只有一个ingredients元素,后跟
    • 只有一个quantity元素
  • 后跟
  • 零或一个decoration元素后跟
  • 零或一个instructions元素后跟
  • 零或一个necessity元素后跟
  • 零或一个preparation元素

您在DTD和XML中拼写necessity的方式不同;他们需要匹配。

您还拼错了第一个instructions中的cocktail结束标记。

ingredientsquantityappetizer的模型中还应更新maindish后跟dessert一次或多次的部分。< / p>

这是一个更新的DTD:

<!ELEMENT recipes (cocktail+, appetizer+, maindish+, dessert+)>

<!ELEMENT cocktail (name+, (ingredients, quantity)+, decoration?, instructions?, necessity?, preparation?)>
<!ELEMENT appetizer (name+, (ingredients, quantity)+)>
<!ELEMENT maindish (name+, (ingredients, quantity)+)>
<!ELEMENT dessert (name+, (ingredients, quantity)+)>

<!ELEMENT name (#PCDATA)>
<!ELEMENT ingredients (#PCDATA)>
<!ELEMENT quantity (#PCDATA)>
<!ELEMENT decoration (#PCDATA)>
<!ELEMENT instructions (#PCDATA)>
<!ELEMENT necessity (#PCDATA)>
<!ELEMENT preparation (#PCDATA)>

和XML文件:

<!DOCTYPE recipes SYSTEM "recipes.dtd">
<recipes>
    <cocktail>

        <name>Hells Bells</name>

        <ingredients>Batida de Coco</ingredients>
        <quantity> 4cl </quantity>

        <ingredients>caramel sirup</ingredients>
        <quantity> 2 cl </quantity>

        <ingredients>Milk</ingredients>
        <quantity> 8 cl </quantity>

        <ingredients>Strawberry puree</ingredients>
        <quantity> 4 cl </quantity>

        <decoration>Decoration: Strawberry blood with spiders.</decoration>
        <instructions>Instructions: All ingredients should be shaken.</instructions>

    </cocktail> 

    <cocktail>

        <name>Feuerzangenbowle</name>

        <ingredients>red wine</ingredients>
        <quantity> 1000 ml </quantity>

        <ingredients>cinnamon</ingredients>
        <quantity> 1 </quantity>

        <ingredients>Carnations</ingredients>
        <quantity> 3 </quantity>

        <necessity>500 ml Pott</necessity>

        <preparation>Season with cinnamon stick and cloves.</preparation>

    </cocktail> 

    <appetizer>

        <name>Fresh glass</name>

        <ingredients>avocado</ingredients>
        <quantity> 1 </quantity>

        <ingredients>salmon</ingredients>
        <quantity> 100 g </quantity> 

        <ingredients>chives</ingredients>
        <quantity> stems </quantity>

        <ingredients>lemon</ingredients>
        <quantity> 1 </quantity>

    </appetizer>

    <appetizer>

        <name>Merceiles Hummus</name>

        <ingredients>Chickpeas</ingredients>
        <quantity> 400 g </quantity>

        <ingredients>olive oil</ingredients>
        <quantity> 2 TS </quantity>

        <ingredients>salt</ingredients>
        <quantity> 1 TS </quantity>

        <ingredients>Pepper powder</ingredients>
        <quantity> 1 TS </quantity>

    </appetizer>

    <maindish>

        <name>Pickled salmon</name>

        <ingredients>salmon</ingredients>
        <quantity> 2,5 kg </quantity>

        <ingredients>pepper</ingredients>
        <quantity> 1 TS </quantity>

        <ingredients>salt</ingredients>
        <quantity> 4 TS </quantity>

        <ingredients>honey</ingredients>
        <quantity> 4 TS </quantity>

    </maindish>

    <maindish>

        <name>Beef fillet</name>

        <ingredients>Beef fillet</ingredients>
        <quantity> 1,5 kg </quantity>

        <ingredients>Butterfelt</ingredients>
        <quantity> 20 g </quantity>

        <ingredients>thyme</ingredients>
        <quantity> 4 branches </quantity>

    </maindish>

    <dessert>

        <name>Chocolate Muffins</name>

        <ingredients>chocolate</ingredients>
        <quantity> 225 g </quantity>

        <ingredients>butter</ingredients>
        <quantity> 115 g </quantity>

        <ingredients>vanillasuger</ingredients>
        <quantity> 1 TS </quantity>

        <ingredients>sugar</ingredients>
        <quantity> 100 g </quantity>

    </dessert>

    <dessert>

        <name>Tiramisu</name>

        <ingredients>sugar</ingredients>
        <quantity> 100 g </quantity>

        <ingredients>yolk</ingredients>
        <quantity> 4 </quantity>

        <ingredients>egg whites</ingredients>
        <quantity> 2 </quantity>

        <ingredients>Amaretto</ingredients>
        <quantity> 2 cl </quantity>

    </dessert>

</recipes>