我正在做一个 Python 脚本,它从作为输入传递的 XML + DTD 生成文件,但它失败了,因为 DTD 无法验证,而我“视觉上”没有看到任何问题。
这是我的代码:
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)
所以这里是 DTD,以及我作为参数传递的 XML 字符串 (xmlDesc)
原始 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>
dtd 变量的值(函数的第一行)
<!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(对我来说,一切都在同一行,但为了便于阅读,我换行了)
<config_scripts>
<script name="reset" repository="config_os">
<param>
<user>
<name/>
<full_name/>
<password/>
<groups/>
</user>
</param>
</script>
</config_scripts>
我终于得到了这个错误->ERROR : DTD Validation failed !
另外,我可以在控制台中阅读:
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
但据我所知,它们是被宣布的......或者可能是因为我把所有标记都留空了?
有任何想法吗 ?
最好的问候,谢谢