为什么在省略可选子元素时 DTD 验证会失败?
Why does DTD validation fail when optional child element is omitted?
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 个或更多。
你很好地解释了你的结果和你的期望。
你是对的 genreList*
意味着 genreList
可能不存在。但是,如果 genreList
不存在,则其子项不能存在。 movie
的内容模型没有为没有 genreList
的 genre
提供内容,因此根据 DTD,您的 XML 是无效的,如所报告的那样。
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 个或更多。
你很好地解释了你的结果和你的期望。
你是对的 genreList*
意味着 genreList
可能不存在。但是,如果 genreList
不存在,则其子项不能存在。 movie
的内容模型没有为没有 genreList
的 genre
提供内容,因此根据 DTD,您的 XML 是无效的,如所报告的那样。