PyXB:无法实例化抽象类型
PyXB: Cannot instantiate abstract type
我尝试使用 PyXB 执行我的第一步,但遇到问题,我无法创建元素数据包。我已经浏览了示例,但找不到有关如何处理此问题的更多信息。如果抽象元素更深一层,似乎有解决方案,但这里是顶层。
有人可以帮我解决这个问题吗?
正在创建类
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
尝试创建数据包元素:
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>
谢谢
曼纽尔
paket 类型是抽象的,但此类型用于验证 v4:paket 元素的元素声明。这对于抽象类型是不允许的。抽象类型只能派生,其具体派生类型用于验证。
如果您可以控制 XSD 文档,将 abstract 设置为 false,或省略此属性,应该使错误消失了。
<xs:complexType name="paket" abstract="false">
...
</xs:complexType>
我尝试使用 PyXB 执行我的第一步,但遇到问题,我无法创建元素数据包。我已经浏览了示例,但找不到有关如何处理此问题的更多信息。如果抽象元素更深一层,似乎有解决方案,但这里是顶层。
有人可以帮我解决这个问题吗?
正在创建类
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
尝试创建数据包元素:
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>
谢谢 曼纽尔
paket 类型是抽象的,但此类型用于验证 v4:paket 元素的元素声明。这对于抽象类型是不允许的。抽象类型只能派生,其具体派生类型用于验证。
如果您可以控制 XSD 文档,将 abstract 设置为 false,或省略此属性,应该使错误消失了。
<xs:complexType name="paket" abstract="false">
...
</xs:complexType>