为什么在省略可选的子元素时DTD验证失败?

时间:2016-02-03 01:56:38

标签: xml dtd xml-validation xml-dtd

XML文档嵌入了DTD声明,但失败了。

这是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)>
]>

这是XML元素:

<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>

它失败了,我只有一个genre而没有用genreList包裹元素。它应该有效,因为在movie元素声明中genreList*表示genreList将显示为0或更多。

1 个答案:

答案 0 :(得分:4)

您可以很好地解释您的结果和期望。

你是对的,genreList*表示genreList可能缺席。但是,如果genreList缺席,则其子女不能出席。 movie的内容模型没有genre没有genreList的规定,因此根据DTD,您的XML无效,正如报道的那样。