【问题标题】:The root element must be well-formed根元素必须格式正确
【发布时间】:2014-08-05 11:57:21
【问题描述】:

几周以来,我一直在处理这项任务时遇到问题。每次我更改某些内容以解决问题时,都会弹出另一个问题。任何帮助,将不胜感激。在我的 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>

再次提前感谢您的帮助。

【问题讨论】:

  • 尝试删除顶部的&lt;!-- New document created with EditiX at Sun Jul 13 10:41:29 AST 2014 --&gt; 评论。
  • 我创建了一个新的 DTD 类型并复制并粘贴了我的代码,但我仍然收到:元素类型“viewer_rating”的声明中需要元素类型。
  • 我建议在这个 XML 中只放一个 以减少人们需要查看的代码量并缩小问题范围。

标签: xml dtd


【解决方案1】:

来自我在Attribute is required and must be specified的评论...

错误“在元素类型“viewer_rating”的声明中需要元素类型。”是因为 (#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>

【讨论】:

  • 非常感谢它验证并运行良好。你帮我减轻了很多压力。距离到期还有 3 天。
  • @randygilman - 不客气。如果您对答案有任何不明白的地方,请随时提问。 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多