DTD validation failure (Python)

I am making a Python script that generates XML + DTD files passed as inputs, but it fails because the DTD cannot be verified while I do not see any problems β€œvisually”.

Here is my code:

DTD = 'scenario.dtd' def OpenXML(xmlDesc): dtd = libxml2.parseDTD(None,DTD) ctxt = libxml2.newValidCtxt() doc = libxml2.parseDoc(xmlDesc) frags = doc.xpathEval('/scenario/config_script/param/*') for frag in frags: frag.unlinkNode() # We remove children of param for validation if doc.validateDtd(ctxt, dtd) != 1: print "ERROR : DTD Validation failed ! " sys.exit() doc.freeDoc() dtd.freeDtd() return libxml2.parseFile(xmlDesc) 

So, here is the DTD, and XML String I am passed as a parameter (xmlDesc)

Original DTD (.dtd script)

  <!ELEMENT scenario (name, description, config_script*)> <!ELEMENT name (#PCDATA)> <!ELEMENT description (#PCDATA)> <!ELEMENT config_script (param)> <!ELEMENT param ANY> <!ATTLIST scenario target (win32|win64|linux32|linux64) "win32"> <!ATTLIST config_script name CDATA #REQUIRED> <!ATTLIST config_script repository CDATA #REQUIRED> 

Dtd variable value (1st line of the function)

 <!DOCTYPE none SYSTEM "scenario.dtd" [ <!ELEMENT scenario (name, description, config_script*)> <!ELEMENT name (#PCDATA)> <!ELEMENT description (#PCDATA)> <!ELEMENT config_script (param)> <!ELEMENT param ANY> <!ATTLIST scenario target (win32|win64|linux32|linux64) "win32"> <!ATTLIST config_script name CDATA #REQUIRED> <!ATTLIST config_script repository CDATA #REQUIRED> ]> 

xml (everything for me on one line, but for reading I break lines)

 <config_scripts> <script name="reset" repository="config_os"> <param> <user> <name/> <full_name/> <password/> <groups/> </user> </param> </script> </config_scripts> 

And I finally get this error -> ERROR : DTD Validation failed !

In addition, I can read this in the console:

 No declaration for element config_script No declaration for element script No declaration for attribute name of element script No declaration for attribute repository of element script No declaration for element user No declaration for element full_name No declaration for element password No declaration for element groups 

But as far as I know, they are announced ... Or maybe it is because I left all the allowances empty?

Any ideas?

Best regards and thank you

+4
source share
1 answer

I'm not sure if something is wrong with the Python code, but I can tell you what is wrong with your DTD.

First, your doctype declaration must match the name of your root element. You have none , but your root element is config_scripts .

You download the scenario.dtd link from within the .dtd script. You must remove the system identifier.

In your xml, you have a script element that is not defined. You have config_script defined so that either XML or DTD needs to be changed. I changed the DTD in my example. (I also combined ATTLIST ads.)

You also did not define these elements: user , full_name , password and groups .

This should look like a DTD (without any changes to the XML):

 <!DOCTYPE config_scripts [ <!ELEMENT scenario (name, description, config_script*)> <!ELEMENT name (#PCDATA)> <!ELEMENT description (#PCDATA)> <!ELEMENT config_scripts (script)> <!ELEMENT script (param)> <!ATTLIST script name CDATA #REQUIRED repository CDATA #REQUIRED> <!ELEMENT param ANY> <!ELEMENT user (name,full_name,password,groups)> <!ELEMENT full_name (#PCDATA)> <!ELEMENT password (#PCDATA)> <!ELEMENT groups (#PCDATA)> <!ATTLIST scenario target (win32|win64|linux32|linux64) "win32"> ]> 

XML checks this DTD in oXygen, so if you need to make any other changes, you will most likely need to make them in Python code.

+1
source

Source: https://habr.com/ru/post/1446264/


All Articles