New submission from Ferdinand: Code is working, but the TEXT variable is not showing the correct line of the XML file but the the line after correct one :
Exemple : [u6v7mr@vl-a-txx-05 Python]$ ./validateXML.py DTD/herve.xml DTD/Tomcat.dtd ERROR: 'I' is not an allowed value for the 'value' attribute at DTD/herve.xml:3:19 TEXT: ' <tomcat' XML file is KO [u6v7mr@vl-a-txx-05 Python]$ The line expected is : <env value="I"/> The corresponding line in the DTD file is this one : <!ATTLIST env value (D|F|R|P|Q|A) #REQUIRED > Here is the python's content : [u6v7mr@vl-a-txx-05 Python]$ cat validateXML.py #!/usr/bin/python from xml.parsers.xmlproc import xmlproc from xml.parsers.xmlproc import xmlval from xml.parsers.xmlproc import xmldtd def validate_xml(xml_filename, dtd_filename): """Validate a given XML file with a given external DTD. If the XML file is not valid, an exception will be printed with an error message.""" dtd = xmldtd.load_dtd(dtd_filename) parser = xmlproc.XMLProcessor() parser.set_application(xmlval.ValidatingApp(dtd, parser)) parser.dtd = dtd parser.ent = dtd try : parser.parse_resource(xml_filename) except : print "XML file is KO" return 0 print "XML file is OK" if __name__ == "__main__": import sys xml_filename, dtd_filename = sys.argv[1], sys.argv[2] validate_xml(xml_filename, dtd_filename) [u6v7mr@vl-a-txx-05 Python]$ Here is the XML file with the error for the element env : [u6v7mr@vl-a-txx-05 Python]$ cat DTD/herve.xml <?xml version='1.0' encoding='utf-8'?> <config> <env value="I"/> <tomcat count="1" start="1" step="1" adresseIP="10.108.250.72" offset="10" name="HERVE_R_NM_GEN" shutdown_port="40010" > <jvm> <memory verbosegc="true" logrotategc="true" min="512" max="1024"> </memory> <permgen max="128" min="128"> </permgen> </jvm> </tomcat> <tomcat count="1" start="2" step="1" adresseIP="10.108.250.72" offset="20" name="HERVE_R_NM_GEN" shutdown_port="40010" > <jvm> <memory verbosegc="true" logrotategc="true" min="512" max="1024"> </memory> <permgen max="128" min="128"> </permgen> </jvm> </tomcat> </config> [u6v7mr@vl-a-txx-05 Python]$ Here is the stdout expected : [u6v7mr@vl-a-txx-05 Python]$ ./validateXML.py DTD/herve.xml DTD/Tomcat.dtd ERROR: 'I' is not an allowed value for the 'value' attribute at DTD/herve.xml:3:19 TEXT: ' <env value="I"/>' XML file is KO ---------- components: XML files: Tomcat.dtd messages: 230135 nosy: Spider06 priority: normal severity: normal status: open title: xmlapp.py display bug when validate XML by DTD type: behavior versions: Python 2.7 Added file: http://bugs.python.org/file37049/Tomcat.dtd _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue22750> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com