3

I used a schema file (.xsd) to generate python classes for generating xml-code. I can use all generated calsses but get an error if i try to

print d.toxml("utf-8")
  File "/usr/local/lib/python2.7/dist-packages/pyxb/binding/basis.py", line 541, in toxml
    dom = self.toDOM(bds)
  File "/usr/local/lib/python2.7/dist-packages/pyxb/binding/basis.py", line 513, in toDOM
    raise pyxb.UnboundElementError(self)
pyxb.exceptions_.UnboundElementError: Instance of type visionDataPackage has no bound element for start tag

It turns out that the element_name attribute of the element is missing. So if I set in /usr/local/lib/python2.7/dist-packages/pyxb/binding/basis.py element_name:

    element_name="visionDataPackage"
    if (element_name is None) and (self._element() is not None):
        element_binding = self._element()
        element_name = element_binding.name()
        need_xsi_type = need_xsi_type or element_binding.typeDefinition()._RequireXSIType(type(self))
    if element_name is None:
        raise pyxb.UnboundElementError(self)

Everything works. So what I'm doing wrong?

codegeek
  • 32,236
  • 12
  • 63
  • 63
k1ngarthur
  • 124
  • 1
  • 12

1 Answers1

3

Probably what you're doing is creating d using its type, rather than an element. For example, if your schema has:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="atype">
    <xs:sequence>
      <xs:element name="entry" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="anelt" type="atype"/>
</xs:schema>

and you were to do d = atype() then d would not be bound to any element. If you instead used d = anelt(), then d would still be an instance of atype, but it would be bound to anelt. It is that binding that tells PyXB what element tag to use when generating a DOM or text XML representation from the object.

That PyXB formerly assigned a default element tag inferred from the underlying type when the object was not bound to an element was a long-standing bug discovered and fixed in PyXB 1.2.3.

See additional discussion on the PyXB SF discussion forum.

pabigot
  • 989
  • 7
  • 8