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

时间:2015-08-02 07:23:13

标签: 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>

和各自的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)*>

我希望“sample.xml”通过使用python脚本来验证“sample.dtd”。我将如何实现这一目标?请帮助。

1 个答案:

答案 0 :(得分:1)

lxml lib非常适合这个:

在当前工作目录中使用sample.txtsample.dtd,您只需运行:

from lxml import etree
parser = etree.XMLParser(dtd_validation=True)
tree = etree.parse("sample.xml", parser)

结果:

XMLSyntaxError: root and DTD name do not match 'node_description' and 'nodedescription', line 3, column 18

有关详细信息,请参阅here。另外,a related question