【问题标题】:Python - RelaxNG object model generator / parserPython - RelaxNG 对象模型生成器/解析器
【发布时间】:2012-01-25 09:33:41
【问题描述】:

假设我有这个 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>

我用不同的值替换了这些表达式(现在不反映架构,但让 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>

瞧——它起作用了,generateDS 不再抱怨并产生这个输出文件:

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

现在我必须调查那个文件,看看它是否可以帮助我(正如预期的那样,它相当大:28157 LOC...)。

【问题讨论】:

  • 您可以将 xml 粘贴到某处,或者至少在第 106 行附近粘贴一些部分吗?
  • 我在 XSD 中发现了错误,已添加到问题中。谢谢你指点我generateDS。我还不确定生成的 python 文件是否能解决我的问题,但它似乎很有希望(尽管由于它的长度而令人困惑)。
  • 是的,它们是一些可以在输出中被截断的东西。我在一个大的xsd上使用它,结果超过30KB。我知道那是什么感觉 :)
  • 生成的一部分正在输出“exportLiteral()”。它使您可以将当前 xml 实例的“快照”保存为 python 类。根据您的需要调整 generateDS,这可以减少(> 减少 20%)
  • 我也有同样的问题。实际上,我正在尝试在 java 中生成对象模型表示,并且没有工具可以直接做到这一点。所以我使用相同的方法:RelaxNG --> XSD,然后是 XSD --JAXB--> java 对象模型。但是,我在第一次转换时遇到相同的错误,在第二次转换时遇到类似的错误。如果您找到了解决问题的方法,请分享。谢谢。

标签: python xsd lxml relaxng


【解决方案1】:

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

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

【讨论】:

  • 感谢您的建议!不幸的是,转换到 xsd 和使用 generateDS 的组合似乎有点脆弱。我猜它只适用于简单的 RelaxNG 模式,但不适用于 Libvirt 模式。请参阅我对问题的补充。
  • 从 RelaxNG 转换到 XSD 的一个挑战是,RelaxNG 支持使用 non-deterministic patterns,而 XSD 需要确定性模式。
【解决方案2】:

您可以只使用同时具有 RelaxNG 验证和 DOM 对象接口的 LXML,这样您就可以像显示的那样访问您的 XML。不过,它不会为您生成课程。

【讨论】:

    猜你喜欢
    • 2011-11-28
    • 1970-01-01
    • 2012-07-25
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    • 2012-09-13
    • 2010-11-14
    • 1970-01-01
    相关资源
    最近更新 更多