0

I'm having a xsd file and need to create an xml accordingly.

<?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>

I've done that several time before using python3 pyxb/pyxbgen and importing the bindings lib.

Now I have tried the same with a new version of the xsd file. The new xsd has in the apiobject two type of objects, requests and responses. The type TableType1 is defined differently for response and requests. But in the binding lib, the inner types of the requests and responses are not accessible to me.

What I would like to do is to generate an xml string for an apiobject with a response containing a TableType1. But since I cannot access the TableType1 object, I neither can fill it up nor put it to the response of the apiobject.

How can I create a valid apiobject response with TableType1?

PS: Some xml Editor says the xsd is valid. But pyxb cannot import a sample xml file using the xsd scheme...

Jochen
  • 155
  • 1
  • 3
  • 15
  • [this](http://stackoverflow.com/questions/17584287/unable-to-bind-to-pyxb-classes-with-nested-anonymous-types?rq=1) offers an answer, but I don't see how this will fill up my inner complex type with the correct objects!? – Jochen Apr 25 '16 at 15:57

1 Answers1

0

The link provided in my comment to the questions does indeed offer the solution to the question.

If your pyxb generated file is simply called binding, then you can generate any object with 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 is even smart enough to select the correct object from the hierarchy. So it will also work for TableType1 and select the right object according to the requirements.

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

As this is within the request context, you will only have a timestamp to set. To append to the timestamp sequence, you have to get the according element of your super sequence and append to it. Sounds wired, but trying around with the example xsd should clarify...

Community
  • 1
  • 1
Jochen
  • 155
  • 1
  • 3
  • 15