XML验证错误

时间:2011-06-28 23:11:51

标签: xml dtd xml-validation

我想知道哪个部分不正确?我修了一个但又回来了。

 <?xml version="1.0"?>
 <!--
 Superstar Movies: Catalog of Movie and Actor Info.
 Author: Randy White
 Date:   6/28/2011

 Filename:         catalog.xml
 -->
 <!DOCTYPE catalog SYSTEM "catalog.dtd">
 <catalog>
<movie movieID="m0100" genre="drama">
    <title>Monster&apos;s Ball</title>
    <date>2001</date>
    <length>111 minutes</length>
    <topBilledActors actorIDs="a0100 a0102"/>
</movie>
<actor actorID="a0100" oscarWinner="yes">
    <name>Halle Berry</name>
    <date>August 14, 1966</date>
    <birthplace>Cleveland, Ohio</birthplace>
    <role character="Leticia Musgrove" movie="m0100"/>
    <role character="Storm" movie="m0101"/>
</actor>
<actor actorID="a0102" oscarWinner="yes">
    <name>Billy Bob Thornton</name>
    <role character="Hank Grotowski" movie="m0100"/>
</actor>
<movie movieID="m0101" genre="fantasy">
    <title>X-Men</title>
    <year>2000</year>
    <length>104 minutes</length>
    <topBilledActors actorIDs="a0100 a0103"/>
</movie>
<actor actorID="a0103" oscarWinner="no">
    <name>Ian McKellen</name>
    <role character="Magneto" movie="m0101"/>
    <role character="Gandolf" movie="m0105"/>
    <role character="Gandolf" movie="m0107"/>
</actor>
<movie movieID="m0105" genre="action" earningsRank="17">
    <title>Lord of the Rings: The Fellowship of the Ring</title>
    <date>2001</date>
    <length>178 minutes</length>
    <topBilledActors actorIDs="a0103"/>
</movie>
<movie movieID="m0107" genre="action" earningsRank="8">
    <title>Lord of the Rings: The Return of the King</title>
    <date>2003</date>
    <length>201 minutes</length>
    <topBilledActors actorIDs="a0103"/>
</movie>
<actor actorID="a0101" oscarWinner="yes">
    <name>Tom Hanks</name>
    <date>July 9, 1956</date>
    <birthplace>Concord, California</birthplace>
    <role character="Captain John H. Miller" movie="m0102"/>
    <role character="Forrest Gump" movie="m0103"/>
    <role character="Andrew Beckett" movie="m0104"/>
</actor>
<movie movieID="m0102" genre="action" earningsRank="50">
    <title>Saving Private Ryan</title>
    <date>1998</date>
    <length>170 minutes</length>
    <topBilledActors actorIDs="a0101 a0104"/>
</movie>
<actor actorID="a0104" oscarWinner="yes">
    <name>Matt Damon</name>
    <date>October 8, 1970</date>
    <birthplace>Cambridge, Massachusetts</birthplace>
    <role character="Private James Francis Ryan" movie="m0102"/>
</actor>
<movie movieID="m0103" genre="comedy" earningsRank="14">
    <title>Forrest Gump</title>
    <date>1994</date>
    <length>142 minutes</length>
    <topBilledActors actorIDs="a0101 a0105 a0106"/>
</movie>
<actor actorID="a0105" oscarWinner="yes">
    <name>Sally Field</name>
    <birthplace>Pasadena, California</birthplace>
    <role character="Mrs. Gump" movie="m0103"/>
</actor>
<actor actorID="a0106">
    <name>Gary Sinise</name>
    <role character="Lt. Dan Taylor" movie="m0103"/>
    <role character="Ken Mattingly" movie="m0106"/>
</actor>
<movie movieID="m0104" genre="drama">
    <title>Philadelphia</title>
    <date>1993</date>
    <length>125 minutes</length>
    <topBilledActors actorIDs="a0101 a0107"/>
</movie>
<movie movieID="m0106" genre="drama">
    <title>Apollo 13</title>
    <date>1995</date>
    <length>140 minutes</length>
    <topBilledActors actorIDs="a0101 a0106"/>
</movie>
<actor actorID="a0107" oscarWinner="yes">
    <name>Denzel Washington</name>
    <role character="Joe Miller" movie="m0104"/>
</actor>

我收到错误

File C:\Users\Randy\Documents\XML\Solution Files\catalog.xml is not valid.
Content model of element 'movie' disallows element 'length' at this position.
    Error location: catalog / movie / length
    Details
        VC: Element Valid: Content model of element 'movie' disallows element 'length' at this position.

DTD文件        

    SuperStar Movies' DTD for XML Catalog documents
    Author: Randy White
    Date:   6/28/2011

    Filename:         catalog.dtd
    -->

    <!ELEMENT catalog (movie|actor)*>

    <!ELEMENT movie (title, date,year,length,topBilledActors)>
    <!ELEMENT title (#PCDATA)>
    <!ELEMENT year (#PCDATA)>
    <!ELEMENT length (#PCDATA)>
    <!ELEMENT topBilledActors EMPTY>
     <!ATTLIST movie movieID ID #REQUIRED>
    <!ATTLIST movie genre (action|comedy|drama|fantasy) #REQUIRED>
    <!ATTLIST movie earningsRank CDATA #IMPLIED>
    <!ATTLIST topBilledActors actorIDs IDREFS #REQUIRED>

    <!ELEMENT actor (name, date?, birthplace?, role+)>
    <!ELEMENT name (#PCDATA)>
    <!ELEMENT date (#PCDATA)>
    <!ELEMENT birthplace (#PCDATA)>
     <!ELEMENT role EMPTY>
    <!ATTLIST actor actorID ID #REQUIRED>
    <!ATTLIST actor oscarWinner (yes|no) "no">
    <!ATTLIST role character CDATA #REQUIRED>
    <!ATTLIST role movie IDREF #REQUIRED>

1 个答案:

答案 0 :(得分:2)

查看DTD,我可以看到movie必须只有一个titledateyearlength和{{1} (按此顺序)。

您的某些电影元素不包含所有必需元素。您需要修复XML,或者需要修改DTD。

例如,第一个topBilledActors元素缺少movie

year

我倾向于DTD修改,因为你有多个电影的ATTLIST声明。

这就是我修改你的DTD的方法:

<movie movieID="m0100" genre="drama">
    <title>Monster&apos;s Ball</title>
    <date>2001</date>
    <length>111 minutes</length>
    <topBilledActors actorIDs="a0100 a0102"/>
</movie>

请注意,<!ELEMENT catalog (movie|actor)*> <!ELEMENT movie (title?,date?,year?,length?,topBilledActors?)> <!ATTLIST movie movieID ID #REQUIRED genre (action|comedy|drama|fantasy) #REQUIRED earningsRank CDATA #IMPLIED> <!ELEMENT topBilledActors EMPTY> <!ATTLIST topBilledActors actorIDs IDREFS #REQUIRED> <!ELEMENT actor (name, date?, birthplace?, role+)> <!ATTLIST actor actorID ID #REQUIRED oscarWinner (yes|no) "no"> <!ELEMENT role EMPTY> <!ATTLIST role character CDATA #REQUIRED movie IDREF #REQUIRED> <!ELEMENT title (#PCDATA)> <!ELEMENT year (#PCDATA)> <!ELEMENT length (#PCDATA)> <!ELEMENT name (#PCDATA)> <!ELEMENT date (#PCDATA)> <!ELEMENT birthplace (#PCDATA)> 的孩子现在是可选的(?=零或一次出现)。

还要注意我是如何合并ATTLIST声明并将它们与元素声明放在一起的。在元素声明下移动它们使DTD更容易阅读。