简体   繁体   English

DTD验证失败(Python)

[英]DTD Validation Failing (Python)

I am doing a Python script which generates files from a XML + DTD passed as inputs, but it fails because the DTD cannot be validated, while I do not see any problem "visually". 我正在做一个Python脚本,它从作为输入传递的XML + DTD生成文件,但它失败,因为无法验证DTD,而我没有看到“视觉上”的任何问题。

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 the XML String I pass as a parameter (xmlDesc) 所以这是DTD,我传递的XML String作为参数(xmlDesc)

Original DTD (scenario.dtd) 原始DTD(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>

Value of the dtd variable (1st line of the function) dtd变量的值(函数的第1行)

<!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 is on the same line for me, but for readability I break lines) xml (对我来说,一切都在同一行,但为了便于阅读,我打破了行)

<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 ! 我终于得到了这个错误 - > ERROR : DTD Validation failed !

Plus, 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 declared... Or maybe is it because I left all mark-ups empty ? 但据我所知,他们被宣布......或许是因为我把所有标记都留空了?

Any ideas ? 有任何想法吗 ?

Best regards and thank you 最好的问候,谢谢

I'm not sure if there is anything wrong with the Python code, but I can tell you what's wrong with your DTD. 我不确定Python代码是否有任何问题,但我可以告诉你DTD有什么问题。

First your doctype declaration should match the name of your root element. 首先,您的doctype声明应与根元素的名称匹配。 You have none but your root element is config_scripts . none但你的根元素是config_scripts

You're loading referring to scenario.dtd from within "scenario.dtd". 你加载指scenario.dtd从“scenario.dtd”内。 You should remove the system identifier. 您应该删除系统标识符。

In your xml you have a script element which is not defined. 在您的xml中,您有一个未定义的script元素。 You do have a config_script defined though so either the XML or the DTD needs to be changed. 您确实定义了config_script因此需要更改XML或DTD。 I changed the DTD in my example. 我在我的例子中更改了DTD。 (I also combined the ATTLIST declarations.) (我还结合了ATTLIST声明。)

You also didn't have these elements defined: user , full_name , password , and groups . 您还没有定义这些元素: userfull_namepasswordgroups

Here's what the DTD should look like (without any modifications to the XML): 这是DTD应该是什么样的(没有对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">
]>

The XML validates against this DTD in oXygen, so if any other changes need to be made, they will most likely need to be made in the Python code. XML在oXygen中对此DTD进行验证,因此如果需要进行任何其他更改,则很可能需要在Python代码中进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM