0

我有一个 xsd 文件,需要相应地创建一个 xml。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.1" elementFormDefault="qualified" attributeFormDefault="unqualified">

  <xs:simpleType name="edateTimeType">
    <xs:restriction base="xs:dateTime">

    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="eEurocents">
    <xs:restriction base="xs:int">
      <xs:maxExclusive value="1000000"/>
      <xs:minExclusive value="-1000000"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="unixTimestampType">
    <xs:restriction base="xs:nonNegativeInteger">
      <xs:minInclusive value="1262304000"/>
      <!-- after 1.1.2010-->
    </xs:restriction>
  </xs:simpleType>

  <xs:complexType name="timeRangeType">
    <xs:sequence>
      <xs:element name="start" type="edateTimeType" minOccurs="1" maxOccurs="1"/>
      <xs:element name="stop" type="edateTimeType" minOccurs="1" maxOccurs="1"/>
    </xs:sequence>
  </xs:complexType>

  <xs:element name="apiobject">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="header" minOccurs="1" maxOccurs="1">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="A" type="eEurocents" minOccurs="1" maxOccurs="1"/>
              <xs:element name="B" type="eEurocents" minOccurs="0" maxOccurs="1"/>
              <xs:element name="unixTimestamp" type="unixTimestampType" minOccurs="1" maxOccurs="1"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:choice maxOccurs="1">
          <xs:element name="response" minOccurs="0" maxOccurs="1">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="TableType1" minOccurs="0" maxOccurs="5">
                  <xs:complexType>
                    <xs:sequence>
                      <xs:element name="teid" type="unixTimestampType"/>
                      <xs:element name="entry" type="eEurocents" maxOccurs="192"/>
                    </xs:sequence>
                  </xs:complexType>
                </xs:element>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element name="request" minOccurs="0" maxOccurs="1">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="TableType1" minOccurs="0" maxOccurs="5">
                  <xs:complexType>
                    <xs:sequence>
                      <xs:element name="timestamp" type="unixTimestampType"/>
                    </xs:sequence>
                  </xs:complexType>
                </xs:element>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

在使用 python3 pyxb/pyxbgen 并导入绑定库之前,我已经这样做了好几次。

现在我用新版本的 xsd 文件尝试了同样的方法。新的 xsd 在 apiobject 中有两种类型的对象,请求和响应。对于响应和请求,类型 TableType1 的定义不同。但是在绑定库中,我无法访问请求和响应的内部类型。

我想做的是为 apiobject 生成一个 xml 字符串,其响应包含 TableType1。但由于我无法访问 TableType1 对象,我既无法将其填满,也无法将其放入 apiobject 的响应中。

如何使用 TableType1 创建有效的 apiobject 响应?

PS:一些 xml 编辑器说 xsd 是有效的。但是 pyxb 无法使用 xsd 方案导入示例 xml 文件...

4

1 回答 1

0

我对问题的评论中提供的链接确实提供了问题的解决方案。

如果您的 pyxb 生成的文件简称为绑定,那么您可以使用pyxb.BIND(...)生成任何对象。

import binding
import pyxb

xml_object = binding.apiobject()
xml_object.header = pyxb.BIND()
# Now xml_object.header offers you the attributes you would expect from xsd file.
# You can create your objects from the binding and assign it to your object.
xml_object.header.A = binding.eEurocents(1234)

pyxb 甚至足够聪明,可以从层次结构中选择正确的对象。所以它也适用于 TableType1 并根据要求选择正确的对象。

xml_object.request = pyxb.BIND()
xml_object.request.TableType1 = pyxb.BIND()

由于这是在请求上下文中,因此您只需设置一个时间戳。要附加到时间戳序列,您必须获取超级序列的相应元素并附加到它。听起来很有线,但尝试使用示例 xsd 应该可以澄清......

于 2016-04-28T12:04:02.437 回答