3

I'm working with multiple XSD class bindings using PyXB 1.2.6. I want to get the order of sub-element names of an arbitrary element type definition. So far I came up with this code:

from pyxb.binding.basis import element

def get_sub_elements(type_definition):
    """Returns an ordered list of the respective
    sub-elements of the given PyXB element.
    """

    if isinstance(type_definition, element):
        type_definition = type_definition._element__typeDefinition()

    element_map = type_definition._ElementMap
    return tuple(name.localName() for name in element_map.keys())

However, since the _ElementMap is a dict, it yields the keys in random order. I however need the order of the elements, as it is defined in the corresponding XSD. How can I achieve that?

Richard Neumann
  • 2,986
  • 2
  • 25
  • 50
  • I don't think this is possible, but if you have the XML Schema object (class `pyxb.xmlschema.structures.Schema`) you can query that. – Markus Oct 18 '19 at 09:06
  • @Markus I only have the class binding generated by pyxbgen. Is there a way to get the corresponding schema object from the type definition? Since it is possible to call `toxml()` on a arbitrary instance of the type definition to get a valid XML document, the correct order of the sub elements of each type definition must be stored somewhere. – Richard Neumann Oct 21 '19 at 10:04
  • According to http://pyxb.sourceforge.net/arch_content.html the order is encoded in the automaton that is created by `pyxbgen`. So, in theory, it should be possible to get the order from there. But AFAICT there is no API for doing this. By the way, the location of the XSD used to build the Python class can be found in generated code as well. – Markus Oct 21 '19 at 10:31
  • Forgot to say... the only help I could offer is to show you how to use the `Schema` class to get a schema object from the XSD. – Markus Oct 21 '19 at 10:34

0 Answers0