我如何使用python验证xml对dtd?

ana*_*ail 6 python xml

我有一个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".我将如何实现这一目标?善意的帮助.

lem*_*ead 5

lxml库非常适合于此:

使用sample.txtsample.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)

有关更多详细信息,请参见此处。另外,一个相关的问题