正确使用XML的外部DTD [英] Proper use of External DTD for XML

查看:123
本文介绍了正确使用XML的外部DTD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下article.dtd

I have the following article.dtd

<!ELEMENT ARTICLE (HEADLINE,BYLINE,LEAD,BODY,NOTES)>
<!ELEMENT HEADLINE (#PCDATA)>
<!ELEMENT BODY (#PCDATA)>

<!ATTLIST ARTICLE AUTHOR CDATA #REQUIRED>

和article.xml

and article.xml

<?xml version="1.0" standalone="no"?>
<!DOCTYPE ARTICLE SYSTEM "article.dtd">
<ARTICLE AUTHOR="T. Daniel">
    <HEADLINE>Title</HEADLINE>
    <BODY>Details</BODY>
</ARTICLE>

我使用Google Chrome浏览器打开xml文件,当我访问页面源代码时,我只会获取article.xml的内容.

I open the xml file using Google Chrome browser, and when I'm accessing the page source I'm only getting the contents of the article.xml.

不是页面源应该合并两个文件(同时显示文档类型定义和xml文档)吗?如果是这样,为什么在我的示例中没有发生这种情况?如果不是,我应该如何检查我的xml是否根据我想要的dtd文件编写?

Isn't the page source supposed to combine the two files (show both document type definition and the xml document) ? And if so, why isn't that happening in my example? If not, how should I check if my xml is written according to the dtd file I want?

提及:article.dtd和article.xml位于同一文件夹中.

Mention: article.dtd and article.xml are in the same folder.

推荐答案

不是页面源应该合并两个文件(同时显示文档类型定义和xml文档)吗?

不,它不应该合并两个文件.

No, it is not supposed to combine the two files.

大多数浏览器不加载外部DTD.我找不到Chrome的明确参考,但我认为它类似于Mozilla/Firefox的工作方式( https://developer.mozilla.org/zh-CN/docs/XML_in_Mozilla#DTDs_and_Other_External_Entities ).

Most browsers do not load external DTDs. I could not find a definite reference for Chrome, but I think it's similar to how it works for Mozilla/Firefox (https://developer.mozilla.org/en-US/docs/XML_in_Mozilla#DTDs_and_Other_External_Entities).

如何检查我的xml是否根据我想要的dtd文件编写?

通过适当的验证程序运行它,例如 xmllint ( libxml2 ):

Run it through a proper validator, for example xmllint (a command line tool that comes with libxml2):

$ xmllint --valid article.xml

此命令的输出:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE ARTICLE SYSTEM "article.dtd">
<ARTICLE AUTHOR="T. Daniel">
    <HEADLINE>Title</HEADLINE>
    <BODY>Details</BODY>
</ARTICLE>
article.xml:6: element ARTICLE: validity error : Element ARTICLE content does not follow the DTD, expecting (HEADLINE , BYLINE , LEAD , BODY , NOTES), got (HEADLINE BODY )
</ARTICLE>
          ^

这篇关于正确使用XML的外部DTD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆