XML 验证错误 - 根元素必须与文档类型匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8402292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 15:03:06  来源:igfitidea点击:

XML Validation error -Root element must match doctype

xml

提问by Dymond

Im trying to validate my XML file with an external DTD. But I get this error everytime.

我正在尝试使用外部 DTD 验证我的 XML 文件。但我每次都会收到这个错误。

Document root element "A", must match DOCTYPE root "test".

i cant figure this out.

我想不通。

The idea of my xml file is that its need to be as short as possible . I thinkt its all good but like i said, i wont validate. Does someone have an idea ?

我的 xml 文件的想法是它需要尽可能短。我认为这一切都很好,但就像我说的,我不会验证。有人有想法吗?

This is my XML file

这是我的 XML 文件

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

<A>
<B>
<F>name</F>
</B>
<D>lastname</D>
<F>name</F>
</A>

And my DTD

还有我的 DTD

<!ELEMENT A (B, (C|D), E?, (F, G?)+)>
<!ELEMENT B (F|G)+>
<!ELEMENT D (#PCDATA|C)*>
<!ELEMENT F (#PCDATA)>
<!ELEMENT G (#PCDATA)>
<!ELEMENT C (#PCDATA)>
<!ELEMENT E (#PCDATA)>

Thanks

谢谢

回答by Quentin

The Doctype claims the root element is <test>but you have used <A>

Doctype 声称根元素是,<test>但您已经使用<A>

<!DOCTYPE test
          ^^^^

Either change the Doctype so it claims the root is <A>or change the XML and DTD to use <test>.

要么更改 Doctype 使其声称根为,<A>要么更改 XML 和 DTD 以使用<test>.

回答by bobince

<!DOCTYPE test SYSTEM "test.dtd">

Declares that the root ELEMENT of the DTD-conformant document is called test. You want:

声明符合 DTD 的文档的根 ELEMENT 被称为test。你要:

<!DOCTYPE A SYSTEM "test.dtd">