【问题标题】:XML/DTD: Validation error for attributesXML/DTD:属性验证错误
【发布时间】:2018-02-14 03:29:06
【问题描述】:

非常感谢您对这个 xml 代码的帮助。 我是这一切的新手......所以提前非常感谢你! 它是这样的: 在我的课堂上,我写了一个 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>`

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

元素类型“listitem”的内容必须匹配“null”。

如果你能帮帮我好吗?!

谢谢!!!

【问题讨论】:

    标签: xml dtd xml-validation


    【解决方案1】:

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

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

    我认为问题都始于recipe 的声明。

    要让您的 XML 按原样验证,请将您的 doctype 声明更改为:

    <!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)>
    ]>
    

    虽然有几个 cmets。

    您的文档中确实有两个食谱;一种用于汉堡,一种用于沙拉。我认为让这些成为他们自己的recipe 更有意义。这可以位于单独的文档中,也可以将根元素更改为 recipes 并拥有多个 recipe 子元素。

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

    不必对内容中的步骤进行编号。 “1”。在&lt;step&gt;1. Heat the olive oil...&lt;/step&gt; 中是隐含的,因为stepmethod 的第一个孩子。 (类似于 HTML 中的 ol 中的 li。)

    最好将unit 属性添加到preptimecooktime,并将值保留为数字/数字范围。这样,无论使用您的 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>
    

    【讨论】:

    • 非常感谢您的时间和解释!我会马上解决的!
    • @Monic - 不客气!请考虑通过单击左侧的复选标记来接受此答案。请参阅stackoverflow.com/help/someone-answers 了解更多信息。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 2010-11-22
    • 1970-01-01
    • 2016-09-18
    相关资源
    最近更新 更多