XML DTD 无法验证

发布于 2025-01-05 18:37:51 字数 862 浏览 0 评论 0原文

我有一个带有外部 DTD 的 XML 文件...这是 DTD:

<!ELEMENT slideshow (slide+)>
<!ELEMENT slide (image,date,time,narrative*,character*)>
<!ELEMENT image (#PCDATA)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT time (#PCDATA)>
<!ELEMENT narrative (#PCDATA)>
<!ELEMENT character (name,dialogue*)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT dialogue (#PCDATA)>
<!ATTLIST dialogue name CDATA #IMPLIED>

这是我的 XML 文件的头部:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE slideshow SYSTEM "slideshow.dtd">

XML 可以根据 DTD 进行良好的验证,但是当我在 Dreamweaver 中验证 DTD 时,它无法通过以下错误:

line 1: Expected comment or CDATA[xml] 
line 3: An exception occurred! Type:EmptyStackException, Message:The element stack is empty[xml]

我不明白为什么会发生这种情况,有人知道吗?

I have an XML file with an external DTD... here is the DTD:

<!ELEMENT slideshow (slide+)>
<!ELEMENT slide (image,date,time,narrative*,character*)>
<!ELEMENT image (#PCDATA)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT time (#PCDATA)>
<!ELEMENT narrative (#PCDATA)>
<!ELEMENT character (name,dialogue*)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT dialogue (#PCDATA)>
<!ATTLIST dialogue name CDATA #IMPLIED>

And here is the head of my XML file:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE slideshow SYSTEM "slideshow.dtd">

The XML validates fine against the DTD, however when I validate the DTD in dreamweaver, it is failing to validate with the following errors:

line 1: Expected comment or CDATA[xml] 
line 3: An exception occurred! Type:EmptyStackException, Message:The element stack is empty[xml]

I can't see why this is happening, does anyone know?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许久 2025-01-12 18:37:51

我没有发现 dtd 的定义方式有任何问题。这已通过 Kate 的 XML 验证器成功验证:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE slideshow SYSTEM "slideshow.dtd">
<slideshow>
  <slide>
    <image>dsds</image>
    <date>dsads</date>
    <time>dsads</time>
    <narrative>fdfd</narrative>
    <narrative>fdfd</narrative>
    <character>
      <name>dasdsa</name>
      <dialogue name="dsds">dsads</dialogue>
    </character>
  </slide>
</slideshow>

[编辑] 我拥有一台装有 Dreamweaver 的机器,我相信我发现了问题。当没有定义 dtd 时,dreamweaver 会尝试回退到已知架构并验证文档。默认后备可以在编辑->首选项->验证器中设置,默认情况下它设置为检查 HTML 4.0。由于 dtd 不是有效的 XML 或 HTML 文件,因此验证器会正确显示错误。我认为 Dreamweaver 无法正确验证 dtd,因此我建议您忽略错误或使用 XML 架构而不是 dtd,因为它是正确的 XML 文档。您的 dtd 有效。

I don't see any problems with the way the dtd is defined. This validated successfully with Kate's XML Validator:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE slideshow SYSTEM "slideshow.dtd">
<slideshow>
  <slide>
    <image>dsds</image>
    <date>dsads</date>
    <time>dsads</time>
    <narrative>fdfd</narrative>
    <narrative>fdfd</narrative>
    <character>
      <name>dasdsa</name>
      <dialogue name="dsds">dsads</dialogue>
    </character>
  </slide>
</slideshow>

[edit] i got a hold of a machine with dreamweaver and I believe I found out the problem. When there is no dtd defined, dreamweaver tries to fallback against a known schema and validate the document. The default fallback can be set in edit->preferences->validator and by default it is set to check against HTML 4.0. Since dtd's are not valid XML or HTML files, the validator properly displays errors. I don't think dreamweaver can properly validate dtd's so I suggest you either ignore the errors or use an XML schema instead of dtd's since it is a proper XML document. Your dtd is valid.

流年里的时光 2025-01-12 18:37:51

对于评论来说有点太多了,所以我将其添加为答案。

在对 @Rado 的评论中,您说“这是当我尝试验证文档类型声明的实际内容时”...这是否意味着您试图仅验证 doctype 声明?像这样吗?:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE slideshow SYSTEM "slideshow.dtd">

如果是这样,那就行不通了。您必须有一个根元素,这可以解释“元素堆栈为空”错误。

当您验证 XML 文件时,将会验证您的文档类型声明。此时 DTD 也应该得到验证。

如果您想要独立于 XML 来验证 DTD,则必须验证 DTD 文件本身。我也没有 Dreamweaver,但您的 DTD 在 oXygen 中验证良好。

This is a little much for a comment, so I'm adding it as an answer.

In the comment to @Rado, you said "it is when I attempt to validate the actual contents of the document type declaration"...does this mean you're trying to validate only the doctype declaration? Like this?:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE slideshow SYSTEM "slideshow.dtd">

If so, that won't work. You have to have a root element and that would explain the "The element stack is empty" error.

The validation of your doctype declaration will happen when you validate your XML file. The DTD should also be validated at that time.

If you want to validate your DTD separate from the XML, you will have to validate the DTD file itself. I also don't have Dreamweaver, but your DTD validates fine in oXygen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文