Menu

(no subject)

Help
Max S
2016-05-09
2016-05-11
  • Max S

    Max S - 2016-05-09

    My team is currently trying to work with a third-party XML-based API. Their team has provided us with bunch of XSD files that I can parse with pyxbgen, but when I try to import multiple generated modules, I get a NamespaceUniquenessError, since these XSDs often have conflicting elements. The generated files can parse the xml documents into a useable form, but it's not workable as a solution if I can only do it for one type of XML file. Is it possible to generate .py files that don't conflict with others, basically treating them as unrelated schemas?

    Note that being able to generate XML with this tool would be nice, but not essential. We mainly need to read it in.

     
  • Max S

    Max S - 2016-05-09

    Strange, I definitely included a subject on this post...

     
  • Peter A. Bigot

    Peter A. Bigot - 2016-05-09

    If the schema have no namespace you could create wrapper schema with unique namespaces and use xs:include to bring each one in. There's an example of this technique here. As long as you know for any given document which variant is necessary you can then process it with the corresponding bindings.

     
  • Max S

    Max S - 2016-05-10

    Hmm, I get a pyxb.exceptions.NamespaceError: http://microsoft.com/wsdl/types/ has no category typeDefinition whenever I try this.

    The upstream schema has a <xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:wt="http://microsoft.com/wsdl/types/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> line, from which I removed a targetNamespace="http://microsoft.com/wsdl/types/"

    I've been invoking it with pyxbgen -u nspurchase_order_export.xsd --module=purchase_order_export.

    (Is it a known problem that this forum eats posts?)

     
  • Peter A. Bigot

    Peter A. Bigot - 2016-05-10

    Instead of removing targetNamespace change the namespace value of both it and the namespace declaration that matches targetNamespace to something specific to the schema instance.

    E.g. xmlns:wt="urn:custom1" targetNamespace="urn:custom1",

    This might now work if something else will still require the namespace to be the original value.

    No idea about the forum behavior; SF is pretty creaky.

     
  • Max S

    Max S - 2016-05-10

    Setting both xmlns:wt and targetNamespace in the included file to "URN:purchase_order_export" causes results in this error: pyxb.exceptions._BindingGenerationError: Target file ./purchase_order_export.py for module MR[urn:uuid:e4ef8694-16ce-11e6-9f91-34363bccdca0]@URN:purchase_order_export bindings exists with other content.

     
  • Peter A. Bigot

    Peter A. Bigot - 2016-05-10

    Delete the old bindings file and regenerate it.

     
  • Max S

    Max S - 2016-05-10

    OK, I got it to generate the Python files, and I can import both into a Python shell without issue. But when I try call purchase_order_export.CreateFromDocument(the_xml_string) I get NamespaceError: http://microsoft.com/wsdl/types/ has no category elementBinding.

     
  • Peter A. Bigot

    Peter A. Bigot - 2016-05-10

    If you're still getting references to http://microsoft.com/wsdl/types/ then either the schemas are not independent or you haven't gotten the namespace replacement right.

    Looking back at your original description, if

    since these XSDs often have conflicting elements.

    is true the schema probably don't meet PyXB's validation expectations.

    If the elements aren't conflicting but instead are repeated, that's a limitation in PyXB (it doesn't recognize and eliminate the redundant types). You may be able edit the schema to remove the duplication then generate the bindings all at once.

     
  • Max S

    Max S - 2016-05-11

    Does PyXB support "unloading" a binding file somehow? If nothing else, I could at least do something like:

    import purchase_order_export
    # (Do the necesary parsing stuff)
    # Unload the binding logic for purchase_order_export, thus allowing me to import something else.
    import sales_order_export
    

    That's not great, but it would be workable. This library does exactly what I need for an individual xsd file, so if I could just encapsulate everything I could use it for my project.

     

    Last edit: Max S 2016-05-11
  • Peter A. Bigot

    Peter A. Bigot - 2016-05-11

    No, unloading is not supported. You could try delegating to a subprocess.

     

Log in to post a comment.