验证 XML 和 DTD 时出现问题

Problems when validating XML and DTD

我制作了这个食谱列表,但出于某种原因,当我尝试在 Oxygen XML 编辑器中验证文档时收到大量错误消息。 氧气给我以下错误:

元素类型 "ingredients" 不能多次声明 元素类型 "name" 不得多次声明 元素类型 "quantity" 不得多次声明 意外元素 "ingredients"。父元素的内容必须匹配: (名称+,原料+,数量+,装饰,说明,必要性,准备)

这是我的代码:

<?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

的新手
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 元素不合适。 ingredients (cocktail) 的父代必须匹配 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 拼写不同;他们需要匹配。

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

ingredients 后跟 quantity 一次或多次的部分也应在 appetizermaindishdessert 的模型中更新.

这是更新后的 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>