Maihan Nijat
Maihan Nijat

Reputation: 9344

Why does DTD validation fail when optional child element is omitted?

The XML document has embedded DTD declaration and it fails.

Here is DTD:

<!DOCTYPE movieList [

<!ELEMENT movieList (movie+)>
<!ELEMENT movie (title+, rating+, runTime+, genreList*, dateReleased+, 
          country+, review+, description+, director+)>

<!ELEMENT title (#PCDATA)>
<!ELEMENT rating (#PCDATA)>
<!ELEMENT runTime (#PCDATA)>
<!ELEMENT genreList (genre+)>
<!ELEMENT genre (#PCDATA)>
<!ELEMENT dateReleased (#PCDATA)>
<!ELEMENT country (#PCDATA)>
<!ELEMENT review (#PCDATA)>
<!ELEMENT description (#PCDATA)>
<!ELEMENT director (#PCDATA)>
]>

And here are XML elements:

<movieList>
<movie>
    <title>Jaws</title>
    <rating>PG</rating>
    <runTime>124 minutes</runTime>
    <genreList>
        <genre>Adventure</genre>
        <genre>Thriller</genre>
    </genreList>
    <dateReleased>20 June 1975</dateReleased>
    <country>USA</country>
    <review>8</review>
    <description>When a gigantic great white shark begins to menace 
    the small island community of Amity, a police chief, a marine 
    scientist and grizzled fisherman set out to stop it.</description>
    <director>Steven Spielberg</director>
</movie>

<movie>
    <title>The Good, the Bad and the Ugly</title>
    <rating>NR</rating>
    <runTime>161 minutes</runTime>
        <genre>Western</genre>
    <dateReleased>23 December 1966</dateReleased>
    <country>Italy</country>
    <review>9</review>
    <description>A bounty hunting scam joins two men in an uneasy 
    alliance against a third in a race to find a fortune in gold buried 
    in a remote cemetery.</description>
    <director>Sergio Leone</director>
</movie>
</movieList>

It fails where I have only one genre and not wrapped the elements with genreList. It should work because in the movie element declaration I have genreList* which means, the genreList will appear 0 or more.

Upvotes: 1

Views: 462

Answers (1)

kjhughes
kjhughes

Reputation: 111621

You explain your results and your expectations very well.

You're right that genreList* means that genreList may be absent. However, if genreList is absent, its children cannot be present. The content model for movie has no provision for genre without genreList, thus your XML is invalid per the DTD, as reported correctly.

Upvotes: 5

Related Questions