XML / DTD:属性的验证错误

时间:2018-02-12 23:13:29

标签: xml dtd xml-validation

我非常感谢您对此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>`

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

  

元素类型的内容&#34; listitem&#34;必须匹配&#34; null&#34;。

如果你能帮帮我吗?!

谢谢!!!

1 个答案:

答案 0 :(得分:1)

问题不在于属性声明。如果# this is copy/pasted SPSS code from file "ES3191" txt <- "ESTU 1-4 CUES 5-9 CCAA 10-11 PROV 12-13 MUN 14-16 TAMUNI 17 CAPITAL 18 DISTR 19-20 SECCION 21-23 ENTREV 24-27 P0 28 P0A 29-31 P1 32 P2 33 P3 34 P4 35 P5 36 P6 37 P701 38-39 P702 40-41 P703 42-43 P801 44-45 P802 46-47 P803 48-49 P901 50-51 P902 52-53 P903 54-55 P904 56-57 P905 58-59 P906 60-61 P907 62-63 P1001 64 P1002 65 P1003 66 P1101 67 P1102 68 P1103 69 P1104 70 P1201 71 P1202 72 P1203 73 P1204 74 P1205 75 P1206 76 P1207 77 P1208 78 P1209 79 P13 80-81 P13A 82-83 P1401 84-85 P1402 86-87 P1403 88-89 P1404 90-91 P1405 92-93 P1406 94-95 P1407 96-97 P1408 98-99 P1409 100-101 P1410 102-103 P1411 104-105 P1412 106-107 P1413 108-109 P1414 110-111 P1415 112-113 P1416 114-115 P1501 116-117 P1502 118-119 P1503 120-121 P1504 122-123 P1505 124-125 P1506 126-127 P1507 128-129 P1508 130-131 P1509 132-133 P1510 134-135 P1511 136-137 P1512 138-139 P1513 140-141 P1514 142-143 P1515 144-145 P1516 146-147 P16 148 P17 149 P1801 150-151 P1802 152-153 P1803 154-155 P1804 156-157 P1805 158-159 P1806 160-161 P1807 162-163 P1808 164-165 P1809 166-167 P1810 168-169 P1811 170-171 P1812 172-173 P1813 174-175 P19 176 P20 177 P21 178-179 P22 180-181 P23 182-183 P2401 184-185 P2402 186-187 P2403 188-189 P2404 190-191 P2405 192-193 P2406 194-195 P2407 196-197 P2408 198-199 P2409 200-201 P2410 202-203 P2411 204-205 P2412 206-207 P2413 208-209 P2414 210-211 P2415 212-213 P2416 214-215 P25 216 P26 217 P27 218 P27A 219-220 P28 221-222 P29 223 P30 224-225 P31 226 P31A 227-228 P32 229 P32A 230 P33 231 P34 232 P35 233 P35A 234 P36 235 P37 236 P37A 237 P37B 238 P38 239-241 P39 242 P39A 243 P40 244-246 P41 247-248 P42 249-250 P43 251 P43A 252 P43B 253 P44 254 P4501 255 P4502 256 P4503 257 P4504 258 P4601 259-261(A) P4602 262-264(A) P4603 265-267(A) P4604 268-270(A) P4605 271-273(A) P4701 274-276(A) P4702 277-279(A) P4703 280-282(A) P4704 283-285(A) P4705 286-288(A) P48 289 P49 290 P50 291 P51 292 I1 293-295 I2 296-298 I3 299-301 I4 302-304 I5 305-307 I6 308-310 I7 311-313 I8 314-316 I9 317-319 E101 320-321 E102 322-323 E103 324-325 E2 326 E3 327-329 E4 330 C1 331 C1A 332-333 C2 334 C2A 335 C2B 336-337 C3 338 C4 339-340 P21R 341-342 P22R 343-344 VOTOSIMG 345-346 P27AR 347-348 RECUERDO 349-350 ESTUDIOS 351 OCUMAR11 352-353 RAMA09 354 CONDICION11 355-356 ESTATUS 357 " # making a 2-column matrix (name = left column, position = right column) m <- matrix(scan(text=txt, what=""), ncol=2, byrow=TRUE) m <- as.data.frame(m, stringsAsFactors=FALSE) names(m) <- c("Var", "Pos") pos <- sub("(A)", "", m$Pos, fixed = TRUE) # some entries contain '(A)' - no idea what it means so deleting it pos <- strsplit(pos, "-") starts <- as.numeric(sapply(pos, head, 1)) # get the first element from left ends <- as.numeric(sapply(pos, tail, 1)) # get the first element from right w <- ends - starts +1 MyData <- read.fwf("R/MD3191/DA3191", widths = w) names(MyData) <- m$Var head(MyData) # ESTU CUES CCAA PROV MUN TAMUNI CAPITAL DISTR SECCION ENTREV P0 P0A P1 P2 P3 P4 P5 P6 # 1 3191 1 16 1 59 5 1 0 0 0 1 0 3 2 2 5 1 2 # 2 3191 2 16 1 59 5 1 0 0 0 1 0 4 2 3 5 2 3 # 3 3191 3 16 1 59 5 1 0 0 0 1 0 4 2 2 4 2 2 看起来应该声明为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)>
]>

但有几条评论。

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

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

不一定需要对内容中的步骤进行编号。 &#34; 1。&#34;隐含<step>1. Heat the olive oil...</step>,因为stepmethod的第一个孩子。 (类似于HTML中li中的ol。)

最好将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>