我尝试用PyXB做我的第一步但有问题,我无法创建元素paket。我已经浏览了样本,但无法找到有关如何处理此问题的更多信息。如果抽象元素更深一层似乎有一个解决方案,但这里它位于顶层。
有人可以帮我吗?
创建课程
pyxbgen -u arelda_v4.xsd -m all
WARNING:pyxb.binding.generate:Complex type {http://bar.admin.ch/arelda/v4}paket renamed to paket_
Python for http://bar.admin.ch/arelda/v4 requires 1 modules
尝试创建paket元素:
Python 2.7.5 (default, Oct 11 2015, 17:47:16)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import all
>>> paket = all.paket()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/pyxb/binding/basis.py", line 1600, in __call__
rv = self.typeDefinition().Factory(*args, **kw)
File "/usr/lib/python2.7/site-packages/pyxb/binding/basis.py", line 305, in Factory
rv = cls._DynamicCreate(*args, **kw)
File "/usr/lib/python2.7/site-packages/pyxb/binding/basis.py", line 677, in _DynamicCreate
return ctor(*args, **kw)
File "/usr/lib/python2.7/site-packages/pyxb/binding/basis.py", line 2075, in __init__
raise pyxb.AbstractInstantiationError(type(self), location, dom_node)
pyxb.exceptions_.AbstractInstantiationError: Cannot instantiate abstract type {http://bar.admin.ch/arelda/v4}paket directly
XSD
<xs:element name="paket" type="paket">
<xs:key name="ordnungssystempositionIdKey">
<xs:annotation>
<xs:documentation>Das Element id in der Entität Ordnungssystemposition muss eindeutig sein.</xs:documentation>
</xs:annotation>
<xs:selector xpath=".//arelda:ordnungssystemposition"/>
<xs:field xpath="@id"/>
</xs:key>
<xs:key name="dossierIdKey">
<xs:annotation>
<xs:documentation>Das Element id in der Entität Dossier muss eindeutig sein.</xs:documentation>
</xs:annotation>
<xs:selector xpath=".//arelda:dossier"/>
<xs:field xpath="@id"/>
</xs:key>
<xs:key name="dokumentIdKey">
<xs:annotation>
<xs:documentation>Das Element id in der Entität Dokument muss eindeutig sein.</xs:documentation>
</xs:annotation>
<xs:selector xpath=".//arelda:dokument"/>
<xs:field xpath="@id"/>
</xs:key>
<xs:key name="archivischeNotizIdKey">
<xs:annotation>
<xs:documentation>Das Element id in der Entität ArchivischeNotiz muss eindeutig sein.</xs:documentation>
</xs:annotation>
<xs:selector xpath=".//arelda:archivischeNotiz"/>
<xs:field xpath="@id"/>
</xs:key>
</xs:element>
<xs:complexType name="paket" abstract="true">
XML
<?xml version="1.0" encoding="UTF-8"?>
<v4:paket schemaVersion="4.0" xsi:type="v4:paketSIP" xmlns:v4="http://bar.admin.ch/arelda/v4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<v4:paketTyp>SIP</v4:paketTyp>
</v4:paket>
由于 曼努埃尔
答案 0 :(得分:2)
类型 paket 是抽象的,但此类型用于元素声明中, v4:paket 元素将在该声明中进行验证。抽象类型不允许这样做。抽象类型只能派生,并且它们的具体派生类型用于验证。
如果您可以控制XSD文档,将抽象设置为 false ,或省略此属性,则应该使错误消失。
<xs:complexType name="paket" abstract="false">
...
</xs:complexType>