我正在尝试实现与某些 API 的连接。在通信过程中,我得到了有效载荷 xml 代码。他们提供了一个带有 xml 描述的 .xsd 文件。现在我想解析 xml 字符串并获得相应的类作为回报。这个功能似乎最好由 PyXB 实现。
为了创建 python 绑定,我使用了以下代码:
pyxben -u my_schema.csd -m my_schema
如果我在 python3 中导入 my_schema 并使用 my_schema.CreateFromDocument('some correctlooking xml'),对于某些类,它返回正确的类,对于某些类,它返回一些匿名类类型。变量都正确实现,意味着它们成为返回类的属性,但类的类型不正确。
这从源 my_schema.py 中很明显,因为类的类型设置为 CTD_ANON_XX。一些朋友在 C# 中实现了相同的功能,一切正常,所以 xsd 方案似乎没问题(并且消息来源建议它应该没问题)。
python 绑定适用于以下类:
<xs:complexType name="OrdrBookEntry">
<xs:attribute name="ordrId" type="longType" use="required"/>
<xs:attribute name="qty" type="quantityType" use="required"/>
<xs:attribute name="px" type="priceType" use="required"/>
<xs:attribute name="ordrEntryTime" type="dateTimeType" use="required"/>
<xs:attribute name="ordrExeRestriction" type="ordrRestrictionType" use="optional"/>
<xs:attribute name="ordrType" type="ordrType" use="optional" />
</xs:complexType>
但以下类型失败:
<xs:element name="PblcOrdrBooksResp" final="#all">
<xs:complexType>
<xs:annotation>
<xs:documentation>
</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="StandardHeader" type="standardHeaderType" minOccurs="1" maxOccurs="1"/>
<xs:element name="OrdrbookList" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="1">
<xs:element name="OrdrBook" type="OrdrBook" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
如前所述,该定义在 C# 中运行良好。这是 pyxb 中的错误还是 xml 定义有问题?我是否错过了有关 pyxb 使用的一些信息?谁能帮我?
我想避免玩弄方案定义。如果有新版本,我需要开始一遍又一遍地修复所有内容......
编辑:澄清一下:我们得到一个如下的 xml 标头:
<?xml version="1.0" ?><PblcOrdrBooksResp xmlns="www.apisite.com">
<StandardHeader marketId="EPEX"/>
<OrdrbookList>
...
</OrdrbookList></PblcOrdrBooksResp>
我们现在想使用 pyxb 启动正确的类,然后从对象中读取发送了什么样的内容!?
所以从上面的例子中我们会期望(或希望)
my_object = mypyxb.module.CreatefromDocument(xml)
type(my_object)
返回其类型
PblcOrdrBooksResp
或者至少有一个类型正确的属性......