我有一个xml文件"sample.xml"如下:
<?xml version="1.0" encoding="UTF8" ?>
< !DOCTYPE nodedescription SYSTEM "sample.dtd" >
<node_description>
<target id="windows 32bit">
<graphics>nvidia_970</graphics>
<power_plug_type>energenie_eu</power_plug_type>
<test>unit test</test>
</target>
<target id="windows 64bit">
<graphics>nvidia_870</graphics>
<power_plug_type>energenie_eu</power_plug_type>
<test>performance test</test>
</target>
</node_description>
Run Code Online (Sandbox Code Playgroud)
和相应的dtd为"sample.dtd":
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT node_description (target)*>
<!ATTLIST target id CDATA #REQUIRED>
<!ELEMENT target (graphics, power_plug_type, test)>
<!ELEMENT graphics (#PCDATA)*>
<!ELEMENT power_plug_type (#PCDATA)*>
<!ELEMENT test (#PCDATA)*>
Run Code Online (Sandbox Code Playgroud)
我希望"sample.xml"通过使用python脚本来验证"sample.dtd".我将如何实现这一目标?善意的帮助.
该lxml库非常适合于此:
使用sample.txt和sample.dtd在当前工作目录中,您可以简单地运行:
from lxml import etree
parser = etree.XMLParser(dtd_validation=True)
tree = etree.parse("sample.xml", parser)
Run Code Online (Sandbox Code Playgroud)
结果是:
XMLSyntaxError: root and DTD name do not match 'node_description' and 'nodedescription', line 3, column 18
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5207 次 |
| 最近记录: |