gpt4 book ai didi

Python - RelaxNG 对象模型生成器/解析器

转载 作者:行者123 更新时间:2023-11-28 18:53:17 26 4
gpt4 key购买 nike

假设我有这个 XML:

<domain type='qemu' xmlns:qemu='http://libirt.org/schemas/domain/qemu/1.0'>
<name>QEmu-fedora-i686</name>
<memory>219200</memory>
<os>
<type arch='i686' machine='pc'>hvm</type>
</os>
<devices>
<emulator>/usr/bin/qemu-system-x86_64</emulator>
</devices>
<qemu:commandline>
<qemu:arg value='-newarg'/>
<qemu:env name='QEMU_ENV' value='VAL'/>
</qemu:commandline>
</domain>

此 XML 通过位于此处的 RelaxNG 架构进行验证:
http://libvirt.org/git/?p=libvirt.git;a=tree;f=docs/schemas;hb=HEAD

现在我想从这个模式中生成类(持久源文件),使我能够以面向对象的方式使用这个模型
这样我就可以

  • 不必费心使用 XML 解析器
  • 可以使用这些对象,但它们始终符合 RelaxNG 模式
  • 获取 IDE 自动完成
  • 通过 Python 解释器进行验证

最后我希望能够做这样的事情:

d = Domain()
d.name = 'QEmu-fedora-i686'
d.memory = 219200
d.os = Os('hvm')
d.os.type.arch = 'i686'
d.os.machine = 'pc'
...

我正在考虑自己编写类似的东西(一个通用的 RelaxNG 对象模型生成器),但我想知道是否有人可以帮助我如何开始以及是否有一些 python 库可以帮助我这样做( lxml?)


方法一:将RelaxNG转换成XSD,然后用generateDS生成对象模型

正如 tito 在他的回答中所建议的,我从 here 下载了最新的 trang .然后我像这样执行 trang:java -jar trang.jar domain.rng domain.xsd。这已经给了我一些警告:

/tmp/libvirt/schemas/domaincommon.rng:531:17: warning: cannot represent an optional group of attributes; approximating 
/tmp/libvirt/schemas/domaincommon.rng:687:15: warning: choice between attributes and children cannot be represented; approximating
/tmp/libvirt/schemas/domaincommon.rng:955:15: warning: choice between attributes and children cannot be represented; approximating
/tmp/libvirt/schemas/domaincommon.rng:1041:15: warning: choice between attributes and children cannot be represented; approximating
/tmp/libvirt/schemas/domaincommon.rng:1260:15: warning: choice between attributes and children cannot be represented; approximating
/tmp/libvirt/schemas/domaincommon.rng:1817:17: warning: choice between attributes and children cannot be represented; approximating
/tmp/libvirt/schemas/domaincommon.rng:1808:15: warning: choice between attributes and children cannot be represented; approximating
/tmp/libvirt/schemas/domaincommon.rng:1924:15: warning: choice between attributes and children cannot be represented; approximating
/tmp/libvirt/schemas/domaincommon.rng:2240:15: warning: choice between attributes and children cannot be represented; approximating

不幸的是,尝试从生成的 XSD 生成对象模型失败了:

$ generateDS.py domain.xsd
Traceback (most recent call last):
File "/usr/local/bin/generateDS.py", line 5, in <module>
pkg_resources.run_script('generateDS==2.7b', 'generateDS.py')
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 467, in run_script
self.require(requires)[0].run_script(script_name, ns)
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 1200, in run_script
execfile(script_filename, namespace, namespace)
File "/usr/local/lib/python2.7/dist-packages/generateDS-2.7b-py2.7.egg/EGG-INFO/scripts/generateDS.py", line 4709, in <module>
main()
File "/usr/local/lib/python2.7/dist-packages/generateDS-2.7b-py2.7.egg/EGG-INFO/scripts/generateDS.py", line 4703, in main
processIncludes, superModule=superModule)
File "/usr/local/lib/python2.7/dist-packages/generateDS-2.7b-py2.7.egg/EGG-INFO/scripts/generateDS.py", line 4433, in parseAndGenerate
inpath=xschemaFileName)
File "/usr/local/lib/python2.7/dist-packages/generateDS-2.7b-py2.7.egg/EGG-INFO/scripts/process_includes.py", line 49, in process_include_files
prep_schema_doc(infile, outfile, inpath, options)
File "/usr/local/lib/python2.7/dist-packages/generateDS-2.7b-py2.7.egg/EGG-INFO/scripts/process_includes.py", line 197, in prep_schema_doc
collect_inserts(root1, params, inserts, options)
File "/usr/local/lib/python2.7/dist-packages/generateDS-2.7b-py2.7.egg/EGG-INFO/scripts/process_includes.py", line 158, in collect_inserts
collect_inserts_aux(child, params, inserts, options)
File "/usr/local/lib/python2.7/dist-packages/generateDS-2.7b-py2.7.egg/EGG-INFO/scripts/process_includes.py", line 175, in collect_inserts_aux
collect_inserts(root, params, inserts, options)
File "/usr/local/lib/python2.7/dist-packages/generateDS-2.7b-py2.7.egg/EGG-INFO/scripts/process_includes.py", line 158, in collect_inserts
collect_inserts_aux(child, params, inserts, options)
File "/usr/local/lib/python2.7/dist-packages/generateDS-2.7b-py2.7.egg/EGG-INFO/scripts/process_includes.py", line 165, in collect_inserts_aux
root = etree.fromstring(string_content, base_url=params.base_url)
File "lxml.etree.pyx", line 2743, in lxml.etree.fromstring (src/lxml/lxml.etree.c:52665)
File "parser.pxi", line 1573, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:79932)
File "parser.pxi", line 1452, in lxml.etree._parseDoc (src/lxml/lxml.etree.c:78774)
File "parser.pxi", line 960, in lxml.etree._BaseParser._parseDoc (src/lxml/lxml.etree.c:75389)
File "parser.pxi", line 564, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:71739)
File "parser.pxi", line 645, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:72614)
File "parser.pxi", line 585, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:71955)
lxml.etree.XMLSyntaxError: attributes construct error, line 106, column 50

这些是由 trang 生成的 XSD(并产生了该错误):

http://mackaz.de/so/basictypes.xsd
http://mackaz.de/so/domain.xsd
http://mackaz.de/so/domaincommon.xsd http://mackaz.de/so/networkcommon.xsd
http://mackaz.de/so/qemu.xsd
http://mackaz.de/so/storageencryption.xsd

通过一些调试,我找到了 generateDS 错误的来源。在文件 basictypes.xsd 中,似乎有一些错误的表达(每个元素中有三个双引号):

<xs:simpleType name="filePath">
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9_\.\+\-\\&amp;"'&lt;&gt;/%]+"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="absFilePath">
<xs:restriction base="xs:string">
<xs:pattern value="/[a-zA-Z0-9_\.\+\-\\&amp;"'&lt;&gt;/%]+"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="absDirPath">
<xs:restriction base="xs:string">
<xs:pattern value="/[a-zA-Z0-9_\.\+\-\\&amp;"'&lt;&gt;/%]*"/>
</xs:restriction>
</xs:simpleType>

我用不同的值替换了这些表达式(现在不反射(reflect)模式,但让 generateDS 高兴):

<xs:simpleType name="filePath">
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9\.\-]+"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="absFilePath">
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9\.\-]+"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="absDirPath">
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9\.\-]+"/>
</xs:restriction>
</xs:simpleType>

Et voila - 它起作用了,generateDS 不再提示并生成这个输出文件:

http://mackaz.de/so/domain.py

现在我必须调查那个文件,看看它是否能帮到我(正如预期的那样,它非常大:28157 LOC...)。

最佳答案

如果您能够将 rng 转换为 xsd(查看 conversions links on relaxng website ),您可以使用 generateDS项目。

  1. 使用 generateDS 将 xsd 文件转换为包含所有类的可导入 python 文件。
  2. 在生成的 python 文件中,您可以使用 parse(),它会为您提供一个具有与您要求的相同行为的根 python 对象。
  3. 稍后,您甚至可以使用 export() 函数将您的根对象导出为 xml

关于Python - RelaxNG 对象模型生成器/解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8592081/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com