Скажите, у меня есть этот 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?)
Подход 1: Преобразовать RelaxNG в XSD, а затем сгенерировать объектную модель с помощью generateDS
Как предложил Тито в своем ответе, я скачал последний транг с здесь . Затем я выполнил транг так: 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
Это XSD, которые были сгенерированы с помощью trang (и породили эту ошибку):
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_\.\+\-\\&"'<>/%]+"/>
</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 счастливым):
<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 вуаля - работает, generateDS больше не жалуется и выдает этот выходной файл:
http://mackaz.de/so/domain.py
Теперь мне нужно изучить этот файл и посмотреть, может ли он мне помочь (как и ожидалось, он довольно большой: 28157 LOC ...).