xml - 根元素必须格式良好

标签 xml dtd

几周来我在完成这项作业时遇到了麻烦。每当我改变一些东西来解决一个问题时,另一个问题就会出现。任何帮助,将不胜感激。在我的 DTD 文档中,我收到错误:

文档中根元素之前的标记必须格式正确。

在我的 XML 文档中我得到:

元素类型“viewer_ rating”的声明中需要元素类型。

但是,我相信这两个错误都来 self 的 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>

再次感谢您的帮助。

最佳答案

摘 self 在 Attribute is required and must be specified 中的评论...

The error "An element type is required in the declaration of element type "viewer_rating"." is because of the numbers in (#PCDATA|0|1|2|3|4|5)*. You're trying to specify atomic values when only element names are allowed. Since an element name can't start with a number, you get the error. You should go back to using attributes for those like I mentioned in my answer.

这是我使用上面的示例所讨论的示例。 (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>

关于xml - 根元素必须格式良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25138967/

相关文章:

javascript - 在 javascript 中解析 UTF-8 XML

mysql - 将 mysql 表导入 XML 文件

xml - DocBook 5.0 发行版上 docbook.dtd 和 docbook.rng 之间的差异

sql-server - 使用 SSIS 导入列时 XML 变成中文/日文

java - android模拟器中的文档路径无效错误

xml - 在 XML 的 Doctype 声明中使用名称

ruby-on-rails - 如何通过 Rspec 测试 has_many? [Shoulda-matchers]

PHP - 根据在线 DTD 文件验证 XML

xml - 是否有程序或脚本通过基于此类 DTD 模式的表单接收用户输入来从 DTD 模式(免费)生成 XML 模型?

java - 如何在jaxb中使用继承?