7

I have a xml file "sample.xml" as:

<?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>

and respective dtd as "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)*>

I want "sample.xml" to get validated against "sample.dtd" by making use of python script. How will i achieve this? kindly help.

anamika email
  • 327
  • 9
  • 21
  • possible duplicate of [DTD Validation With Python?](http://stackoverflow.com/questions/270460/dtd-validation-with-python) – hoijui Aug 02 '15 at 08:11
  • 1
    possible duplicate of [How do I validate xml against a DTD file in Python](http://stackoverflow.com/questions/15798/how-do-i-validate-xml-against-a-dtd-file-in-python) – har07 Aug 02 '15 at 08:16

1 Answers1

5

The lxml lib is well suited for this:

With sample.txt and sample.dtd in the current working directory, you can simply run:

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

Results in:

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

See here for more detail. Also, a related question

Community
  • 1
  • 1
lemonhead
  • 5,328
  • 1
  • 13
  • 25