XML/DTD: 属性验证错误

XML/DTD: Validation error for attributes

非常感谢你对这个 xml 代码的帮助。 我对所有这一切都是新手......所以非常感谢你! 它是这样的: 对于我的 class,我写了一个 xml 的食谱,然后包含了 dtd:

`<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE recipe
 [
 <!ENTITY writer "Matt Tebutt">
 <!ENTITY copyright "BBC">
 <!ELEMENT recipe (title,preptime,cooktime,serves,comment,ingredients,(burgers,(listitem*,(name,quantity)*),salad,(listitem*,(name,quantity)*),toserve,(listitem*,(name,quantity)*)),method,(step)*)>
 <!ELEMENT title (#PCDATA)>
 <!ATTLIST title xml:lang NMTOKEN #FIXED "en">
 <!ELEMENT preptime (#PCDATA)>
 <!ELEMENT cooktime (#PCDATA)>
 <!ELEMENT serves (#PCDATA)>
 <!ELEMENT comment (#PCDATA)>
 <!ELEMENT ingredients (#PCDATA)>
 <!ELEMENT burgers (#PCDATA)>
 <!ELEMENT listitem (#PCDATA)>
 <!ELEMENT name (#PCDATA)>
 <!ELEMENT quantity (#PCDATA)>
 <!ATTLIST quantity unit (tablespoon|clove|pinch|bunch|gram) #IMPLIED>
 <!ELEMENT salad (#PCDATA)>
 <!ELEMENT toserve (#PCDATA)>
 <!ELEMENT method (#PCDATA)>
 <!ELEMENT step (#PCDATA)>
 ]>
 <recipe>
    <title xml:lang="en">Chickpea burgers</title>
    <preptime>&lt;30 min</preptime>
    <cooktime>10 to 30 min</cooktime>
    <serves>serves 4</serves>
    <comment>
        These are delicious vegan burgers with a chilli kick. They can be prepared in advance and left in the fridge until you're ready to cook.
    </comment>
    <ingredients>
        <burgers>
            <listitem>
                <name>olive oil</name>
                <quantity unit="tablespoon">2</quantity>
            </listitem>
            <listitem>
                <name>red onions, diced</name>
                <quantity>2</quantity>
            </listitem>
            <listitem>
                <name>garlic, finely chopped</name>
                <quantity unit="clove">3</quantity>
            </listitem>
            <listitem>
                <name>ground coriander</name>
                <quantity unit="tablespoon">1</quantity>
            </listitem>
            <listitem> 
                <name>ground cumin</name>
                <quantity unit="tablespoon">0.5</quantity>
            </listitem>
            <listitem>
                <name>ground turmeric</name>
                <quantity unit="pinch">1</quantity>
            </listitem>
            <listitem>
                <name>hot red chilli, finely chopped</name>
                <quantity>1</quantity>
            </listitem>
            <listitem>
                <name>fresh coriander and the stalks, finely chopped</name>
                <quantity unit="bunch">1</quantity>
            </listitem>
            <listitem>
                <name>cooked chickpeas, drained</name>
                <quantity unit="gram">2x400</quantity>
            </listitem>
            <listitem>
                <name>lemon, juice only</name>
                <quantity> 1</quantity>
            </listitem>
            <listitem>
                <name>ground polenta</name>
                <quantity unit="gram">250</quantity>
            </listitem>
            <listitem>
                <name>salt</name>
            </listitem>
            <listitem>
                <name>freshly ground pepper</name>
            </listitem>
        </burgers>
        <salad>
            <listitem>
                <name>red onion, sliced</name>
                <quantity>0.5</quantity>
            </listitem>
            <listitem>
                <name>red pepper, sliced</name>
                <quantity>0.5</quantity>
            </listitem>
            <listitem>
                <name>salt</name>
                <quantity>to taste</quantity>
            </listitem>
            <listitem>
                <name>ground sumac"</name>
                <quantity unit="pinch">1</quantity>
            </listitem>
            <listitem>
                <name>tomatoes, sliced</name>
                <quantity>2</quantity>
            </listitem>
            <listitem>
                <name>large handful Cos lettuce leaves</name>
                <quantity>1</quantity>
            </listitem>
        </salad>
        <toserve>
            <listitem>
                <name>Sriracha chilli sauce</name>
                <quantity>to taste</quantity>
            </listitem>
            <listitem>
                <name>vegan mayonnaise</name>
                <quantity>to taste</quantity>
            </listitem>
            <listitem>
                <name>burger buns</name>
                <quantity>4</quantity>
            </listitem>
        </toserve>
    </ingredients>
    <method>
        <step>
            1. Heat the olive oil in a pan and fry the onion and garlic for ten minutes until soft. Add the spices and coriander and cook for another minute. Remove from the heat.
        </step>
        <step>
            2. Pulse the chickpeas in a blender with the onion spice mix. Taste, season with salt and pepper, and add a squeeze of lemon juice. Form the mixture into stiff patties and set aside in the fridge until you are ready to cook.
        </step>
        <step>
            3. For the salad, salt the red onion and red pepper on a plate and leave for 20 minutes.
        </step>
        <step>
            4. When ready to cook the burgers, sprinkle the polenta onto a plate and press the burgers onto the polenta. Heat the sunflower oil in a pan and fry the burgers on both sides until cooked through. (Alternatively, you can bake these on an oiled tray for 15-20 minutes at 200C/180C Fan/Gas 6.)
        </step>
        <step>
            5. To finish the salad, wash the onion and pepper under cold, running water and drain. Sprinkle the sumac over the tomatoes. Combine the tomatoes with the Cos lettuce leaves, red onion and red pepper. Mix together the chilli sauce and mayonnaise in a small dish.
        </step>
        <step>
            6. Serve the burgers in the buns with the salad and chilli dip alongside.
        </step>
    </method>
 </recipe>`

但是我无法正确获取单元的属性声明。当我尝试验证时,我得到了一个完整的错误列表:

The content of element type "listitem" must match "null".

如果你能帮帮我好吗?!

谢谢!!!

问题不在于属性声明。就是你将 listitem 声明为包含 #PCDATA,而它看起来应该被声明为 (name, quantity)

还有其他元素声明不正确,例如 ingredientsburgers

我认为问题都是从 recipe 的声明开始的。

要让您的 XML 验证 as-is,请将您的文档类型声明更改为:

<!DOCTYPE recipe [
<!ENTITY writer "Matt Tebutt">
<!ENTITY copyright "BBC">
<!ELEMENT recipe (title,preptime,cooktime,serves,comment,ingredients,method)>
<!ELEMENT title (#PCDATA)>
<!ATTLIST title xml:lang NMTOKEN #FIXED "en">
<!ELEMENT preptime (#PCDATA)>
<!ELEMENT cooktime (#PCDATA)>
<!ELEMENT serves (#PCDATA)>
<!ELEMENT comment (#PCDATA)>
<!ELEMENT ingredients (burgers?,salad?,toserve?)>
<!ELEMENT burgers (listitem*)>
<!ELEMENT listitem (name,quantity?)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT quantity (#PCDATA)>
<!ATTLIST quantity unit (tablespoon|clove|pinch|bunch|gram) #IMPLIED>
<!ELEMENT salad (listitem*)>
<!ELEMENT toserve (listitem*)>
<!ELEMENT method (step*)>
<!ELEMENT step (#PCDATA)>
]>

尽管有一些评论。

您的文档中确实有两个食谱;一种用于汉堡,一种用于沙拉。我认为将它们作为自己的 recipe 更有意义。这可以在单独的文档中,也可以将根元素更改为 recipes 并具有多个 recipe children.

具有像 burgersalad 这样的元素名称会使将来更难扩展。如果您真的想捕获该信息,我会向 recipe 添加一个 type 属性或其他内容(示例中未显示)。

没有必要在内容中对步骤进行编号。 “1”。 in <step>1. Heat the olive oil...</step> 是隐含的,因为 stepmethod 的第一个 child。 (类似于 HTML 中的 ol 中的 li。)

最好将 unit 属性添加到 preptimecooktime 中,并将值保留在 number/number 范围内。这样,无论消耗您的 XML 什么,都可以更轻松地使用数据;例如计算总时间(示例中未显示)。 serves 也是如此;将该值保留为数字。

例子

<!DOCTYPE recipes [
<!ENTITY writer "Matt Tebutt">
<!ENTITY copyright "BBC">
<!ELEMENT recipes (recipe+)>
<!ELEMENT recipe (title,preptime,cooktime,serves,comment?,ingredients,method)>
<!ELEMENT title (#PCDATA)>
<!ATTLIST title xml:lang NMTOKEN #FIXED "en">
<!ELEMENT preptime (#PCDATA)>
<!ELEMENT cooktime (#PCDATA)>
<!ELEMENT serves (#PCDATA)>
<!ELEMENT comment (#PCDATA)>
<!ELEMENT ingredients (listitem+,toserve?)>
<!ELEMENT listitem (name,quantity?)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT quantity (#PCDATA)>
<!ATTLIST quantity unit (tablespoon|clove|pinch|bunch|gram) #IMPLIED>
<!ELEMENT toserve (listitem*)>
<!ELEMENT method (step*)>
<!ELEMENT step (#PCDATA)>
]>
<recipes>
    <recipe>
        <title xml:lang="en">Chickpea burgers</title>
        <preptime>&lt;30 min</preptime>
        <cooktime>10 to 30 min</cooktime>
        <serves>4</serves>
        <comment>These are delicious vegan burgers with a chilli kick. They can be prepared in
            advance and left in the fridge until you're ready to cook.</comment>
        <ingredients>
            <listitem>
                <name>olive oil</name>
                <quantity unit="tablespoon">2</quantity>
            </listitem>
            <listitem>
                <name>red onions, diced</name>
                <quantity>2</quantity>
            </listitem>
            <listitem>
                <name>garlic, finely chopped</name>
                <quantity unit="clove">3</quantity>
            </listitem>
            <listitem>
                <name>ground coriander</name>
                <quantity unit="tablespoon">1</quantity>
            </listitem>
            <listitem>
                <name>ground cumin</name>
                <quantity unit="tablespoon">0.5</quantity>
            </listitem>
            <listitem>
                <name>ground turmeric</name>
                <quantity unit="pinch">1</quantity>
            </listitem>
            <listitem>
                <name>hot red chilli, finely chopped</name>
                <quantity>1</quantity>
            </listitem>
            <listitem>
                <name>fresh coriander and the stalks, finely chopped</name>
                <quantity unit="bunch">1</quantity>
            </listitem>
            <listitem>
                <name>cooked chickpeas, drained</name>
                <quantity unit="gram">2x400</quantity>
            </listitem>
            <listitem>
                <name>lemon, juice only</name>
                <quantity> 1</quantity>
            </listitem>
            <listitem>
                <name>ground polenta</name>
                <quantity unit="gram">250</quantity>
            </listitem>
            <listitem>
                <name>salt</name>
            </listitem>
            <listitem>
                <name>freshly ground pepper</name>
            </listitem>
            <toserve>
                <listitem>
                    <name>Sriracha chilli sauce</name>
                    <quantity>to taste</quantity>
                </listitem>
                <listitem>
                    <name>vegan mayonnaise</name>
                    <quantity>to taste</quantity>
                </listitem>
                <listitem>
                    <name>burger buns</name>
                    <quantity>4</quantity>
                </listitem>
            </toserve>
        </ingredients>
        <method>
            <step>Heat the olive oil in a pan and fry the onion and garlic for ten minutes until
                soft. Add the spices and coriander and cook for another minute. Remove from the
                heat.</step>
            <step>Pulse the chickpeas in a blender with the onion spice mix. Taste, season with salt
                and pepper, and add a squeeze of lemon juice. Form the mixture into stiff patties
                and set aside in the fridge until you are ready to cook.</step>
            <step>For the salad, salt the red onion and red pepper on a plate and leave for 20
                minutes.</step>
            <step>When ready to cook the burgers, sprinkle the polenta onto a plate and press the
                burgers onto the polenta. Heat the sunflower oil in a pan and fry the burgers on
                both sides until cooked through. (Alternatively, you can bake these on an oiled tray
                for 15-20 minutes at 200C/180C Fan/Gas 6.)</step>
            <step>Mix together the chilli sauce and mayonnaise in a small dish.</step>
            <step>Serve the burgers in the buns with the chilli dip alongside.</step>
        </method>
    </recipe>
    <recipe>
        <title>Salad</title>
        <preptime>10 min</preptime>
        <cooktime>0 min</cooktime>
        <serves>4</serves>
        <ingredients>
            <listitem>
                <name>red onion, sliced</name>
                <quantity>0.5</quantity>
            </listitem>
            <listitem>
                <name>red pepper, sliced</name>
                <quantity>0.5</quantity>
            </listitem>
            <listitem>
                <name>salt</name>
                <quantity>to taste</quantity>
            </listitem>
            <listitem>
                <name>ground sumac</name>
                <quantity unit="pinch">1</quantity>
            </listitem>
            <listitem>
                <name>tomatoes, sliced</name>
                <quantity>2</quantity>
            </listitem>
            <listitem>
                <name>large handful Cos lettuce leaves</name>
                <quantity>1</quantity>
            </listitem>
        </ingredients>
        <method>
            <step>Wash the onion and pepper under cold, running water and drain.</step>
            <step>Sprinkle the sumac over the tomatoes.</step>
            <step>Combine the tomatoes with the Cos lettuce leaves, red onion and red pepper.</step>
        </method>
    </recipe>
</recipes>