我几周来一直遇到这项任务有问题。每次我改变一些东西来修复问题时,都会弹出另一个问题。任何帮助,将不胜感激。在我的DTD文档中,我收到错误:
根元素之前的文档中的标记必须格式正确。
在我的XML文档中,我得到:
元素类型“viewer_rating”的声明中需要元素类型。
但是,我认为这两个错误都来自我的DTD文档。任何帮助将不胜感激。
DTD:
<?xml version="1.0" encoding="UTF-8"?>
<!-- New document created with EditiX at Sun Jul 06 07:25:48 AST 2014 -->
<!ELEMENT movies (movie*)>
<!ELEMENT movie (title, genre, movie_rating, viewer_rating, summary, year, director+, runtime, studio, actors+)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT genre (#PCDATA|action|comedy|drama|family|foreign|horror|musical|other)*>
<!ELEMENT movie_rating (#PCDATA|G|PG|PG-13|R|NC-17)*>
<!ELEMENT viewer_rating (#PCDATA|0 | 1 | 2 | 3 | 4 | 5)*>
<!ELEMENT summary (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT director (#PCDATA)>
<!ELEMENT runtime (#PCDATA)>
<!ELEMENT studio (#PCDATA)>
<!ELEMENT actors (#PCDATA)>
<!ATTLIST actors id CDATA #REQUIRED>
XML:
<?xml version="1.0" encoding="UTF-8"?>
<!-- New document created with EditiX at Sun Jul 13 10:41:29 AST 2014 -->
<!DOCTYPE movies SYSTEM "MoviesDTD.dtd">
<movies>
<movie>
<title>Transcendence</title>
<genre>Action</genre>
<movie_rating>PG-13</movie_rating>
<viewer_rating>4</viewer_rating>
<summary>In Transcendence, Dr. Will Carter is an expert in Artificial Intelligence, and initially is excited by the promises it offers the world at large. Unfortunately, when he is diagnosed with a terminal disease, his motives change and he becomes focused on his own transcendence. Now he is racing against his own mortality as extremists attempt to stop him. Max Waters is his best friend and a researcher as well, and is torn between helping his friend and what that will mean for society at large.</summary>
<year>2014</year>
<director>Wall Pfistery</director>
<runtime>119</runtime>
<studio>Warner Bros.</studio>
<actors id="1000">Johnny Depp</actors>
<actors id="1001">Paul Bettany</actors>
<actors id="1002">Rebecca Hall</actors>
</movie>
</movies>
再次感谢您的帮助。
答案 0 :(得分:2)
来自Attribute is required and must be specified的评论......
错误&#34; 元素类型声明中需要元素类型&#34; viewer_rating&#34;。&#34;是因为(#PCDATA | 0 | 1 | 2 | 3 | 4 | 5)*中的数字。当仅允许元素名称时,您尝试指定原子值。由于元素名称不能以数字开头,因此您会收到错误。您应该回到使用我在答案中提到的那些属性。
以下是使用上述示例我所谈论的内容的一个示例。 (XML和DTD都已被修改。)
此外,您试图将#PCDATA(属性中的CDATA)与枚举值混合使用。这是不可能的。它看起来像你试图允许多个枚举值;这也没有奏效。如果您需要多个值,例如多个genre
值,则可能需要删除枚举并改为使用CDATA。
<强> DTD 强>
<!ELEMENT movies (movie*)>
<!ELEMENT movie (title, summary, year, director+, runtime, studio, actors+)>
<!ATTLIST movie
genre (action|comedy|drama|family|foreign|horror|musical|other) #IMPLIED
movie_rating (G|PG|PG-13|R|NC-17) #IMPLIED
viewer_rating (0|1|2|3|4|5) #IMPLIED>
<!ELEMENT title (#PCDATA)>
<!ELEMENT summary (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT director (#PCDATA)>
<!ELEMENT runtime (#PCDATA)>
<!ELEMENT studio (#PCDATA)>
<!ELEMENT actors (#PCDATA)>
<!ATTLIST actors id CDATA #REQUIRED>
<强> XML 强>
<!DOCTYPE movies SYSTEM "MoviesDTD.dtd">
<movies>
<movie genre="action" movie_rating="PG-13" viewer_rating="4">
<title>Transcendence</title>
<summary>In Transcendence, Dr. Will Carter is an expert in Artificial Intelligence, and initially is excited by the promises it offers the world at large. Unfortunately, when he is diagnosed with a terminal disease, his motives change and he becomes focused on his own transcendence. Now he is racing against his own mortality as extremists attempt to stop him. Max Waters is his best friend and a researcher as well, and is torn between helping his friend and what that will mean for society at large.</summary>
<year>2014</year>
<director>Wall Pfistery</director>
<runtime>119</runtime>
<studio>Warner Bros.</studio>
<actors id="1000">Johnny Depp</actors>
<actors id="1001">Paul Bettany</actors>
<actors id="1002">Rebecca Hall</actors>
</movie>
</movies>