xmllint:如何使用本地DTD文件验证XML [英] xmllint : how to validate an XML using a local DTD file
问题描述
我有本地DTD文件test.dtd.内容是:
I have a local DTD file test.dtd. Content is :
<!DOCTYPE coord [
<!ELEMENT coord (date)>
<!ELEMENT date (#PCDATA)>
]>
我想使用xmllint验证XML.此XML中没有DOCTYPE:
I'd like to validate an XML using xmllint. This XML has no DOCTYPE in it :
<?xml version="1.0" encoding="x-mac-roman"?>
<coord>
<date>20150312</date>
</coord>
如果我将DTD块作为第二行插入到XML文件的副本中并使用,则没问题
No problem if I insert the DTD block as a 2nd line into a copy of my XML file and use:
xmllint --valid --noout my2.xml
但是当我尝试时:
xmllint --loaddtd test.dtd --valid --noout my.xml
xmllint --dtdvalid test.dtd --noout my.xml
都不起作用.outout是:
both don't work. The outout is :
test.dtd:1: parser error : Content error in the external subset
<!DOCTYPE coord [
^
test.dtd:1: parser error : Content error in the external subset
<!DOCTYPE coord [
^
Could not parse DTD test.dtd
有什么主意吗?看来我的XML 必须包含一个DOCTYPE行(带有SYSTEM关键字),以引用外部DOCTYPE文件,这是我要避免的.参见: http://www.w3schools.com/dtd/
Any idea ? It seems that my XML MUST contain a DOCTYPE line (with SYSTEM keyword) to reference the external DOCTYPE file, that I want to avoid. See : http://www.w3schools.com/dtd/
有没有不修改XML的解决方案?
Is there any solution without modifying the XML ?
推荐答案
首先,外部DTD不需要<!DOCTYPE
前导-从DTD文件中将其删除:
First of all, external DTDs do not need the <!DOCTYPE
preamble - remove it from the DTD file:
<!ELEMENT coord (date)>
<!ELEMENT date (#PCDATA)>
然后,-loaddtd
获取一个外部DTD,这与针对外部DTD的 validation 不同.使用-dtdvalid
选项,如下所示:
Then, --loaddtd
fetches an external DTD, which is not the same as validating against an external DTD. Use the --dtdvalid
option as follows:
$ xmllint --noout --dtdvalid test.dtd test.xml
如果XML文档有效,则xmllint将不会输出任何内容(由于-noout
).如果将DTD更改为,请说:
If the XML document is valid, xmllint will not output anything (because of --noout
). If you change the DTD to, say:
<!ELEMENT coord (date,other)>
<!ELEMENT date (#PCDATA)>
输出将是
$ xmllint --noout --dtdvalid test.dtd test.xml
test.xml:2: element coord: validity error : Element coord content does not follow the DTD, expecting (date , other), got (date )
Document test.xml does not validate against test.dtd
这篇关于xmllint:如何使用本地DTD文件验证XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!