Which XML DTD is Valid

159 Views Asked by At

I have this question in a worksheet, but I've gotten it wrong about 5 times now, can someone please help?

Here is an XML DTD:

<!DOCTYPE meal [
    <!ELEMENT meal (person*,food*,eats*)>
    <!ELEMENT person EMPTY>
    <!ELEMENT food EMPTY>
    <!ELEMENT eats EMPTY>
    <!ATTLIST person name ID #REQUIRED>
    <!ATTLIST food name ID #REQUIRED>
   <!ATTLIST eats diner IDREF #REQUIRED dish IDREF #REQUIRED>
]>

Which of the following documents match the DTD?

<meal>
    <person name="Alice"/>
    <food name="salad"/>
    <eats diner="Alice" dish="salad"/>
    <person name="Bob"/>
    <food name="salad"/>
    <eats diner="Bob" dish="salad"/>
    <person name="Carol"/>
    <food name="sandwich"/>
    <eats diner="Carol" dish="sandwich"/>
</meal>

<meal>
    <person name="Alice"/>
    <person name="Bob"/>
    <person name="Carol"/>
    <person name="Dave"/>
    <food name="salad"/>
    <food name="turkey"/>
    <food name="sandwich"/>
    <eats diner="Alice" dish="turkey"/>
    <eats diner="Bob" dish="salad"/>
    <eats diner="turkey" dish="Dave"/>
</meal>

<meal>
    <person name="Alice"/>
    <person name="Bob"/>
    <food name="salad"/>
    <eats diner="Alice" dish="food"/>
    <eats diner="Bob" dish="food"/>
</meal>
  • Only the second
  • Only the second and third
  • Only the first and third
  • Only the first

Thank you for your help

2

There are 2 best solutions below

1
On

The first example has two elements with the same value for an id attribute. <food name="salad"/> is repeated so this is not valid. (And as mentioned in the other answer, order is important)

The third example has a value for an idref attribute that is not an id attribute of any element <eats diner="Bob" dish="food"/> - "food" is not an id of any element.

The second, although it looks a little strange to us as humans interpreting it with <eats diner="turkey" dish="Dave"/> is valid, since those attributes are both idref attributes elsewhere. The dtd doesn't specify that an idref has to be an id attribute that a particular type of element has, even though that's the only way it would make sense to us.

0
On

Only the second.

The first is invalid because meal must contain zero or more person followed by zero or more food followed by zero or more eats.

The third is invalid because there are no ID type attributes with the value food. (dish is an attribute type IDREF)