PyXB:无法实例化抽象类型

2024-04-19 20:06:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试用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>

谢谢 曼纽尔


Tags: keynameinidhttpdocumentationtypeannotation
1条回答
网友
1楼 · 发布于 2024-04-19 20:06:46

类型paket是抽象的,但此类型用于验证v4:paket元素的元素声明中。抽象类型不允许这样做。抽象类型只能派生,其具体派生类型用于验证。你知道吗

如果您可以控制XSD文档,那么将abstract设置为false,或者忽略此属性,应该可以消除错误。你知道吗

<xs:complexType name="paket" abstract="false">
  ...
</xs:complexType>

相关问题 更多 >