This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: xmlapp.py display bug when validate XML by DTD
Type: behavior Stage: resolved
Components: XML Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Spider06
Priority: normal Keywords:

Created on 2014-10-28 09:57 by Spider06, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
Tomcat.dtd Spider06, 2014-10-28 09:56
Messages (2)
msg230135 - (view) Author: Ferdinand (Spider06) * Date: 2014-10-28 09:56
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
msg230808 - (view) Author: Ferdinand (Spider06) * Date: 2014-11-07 14:54
I found a solution :

from xml.sax import make_parser
from xml.sax.handler import feature_namespaces, feature_validation
from xml.sax.handler import ContentHandler, ErrorHandler, DTDHandler

With the library above, they is no display bug !
History
Date User Action Args
2022-04-11 14:58:09adminsetgithub: 66939
2020-05-31 14:50:47serhiy.storchakasetstatus: open -> closed
resolution: not a bug
stage: resolved
2014-11-07 14:54:51Spider06setmessages: + msg230808
2014-10-28 09:57:00Spider06create