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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Does PyXB support "unloading" a binding file somehow? If nothing else, I could at least do something like:
importpurchase_order_export# (Do the necesary parsing stuff)# Unload the binding logic for purchase_order_export, thus allowing me to import something else.importsales_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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
Strange, I definitely included a subject on this post...
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.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 atargetNamespace="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?)
Instead of removing
targetNamespace
change the namespace value of both it and the namespace declaration that matchestargetNamespace
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.
Setting both
xmlns:wt
andtargetNamespace
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
.Delete the old bindings file and regenerate it.
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 getNamespaceError: http://microsoft.com/wsdl/types/ has no category elementBinding
.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
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.
Does PyXB support "unloading" a binding file somehow? If nothing else, I could at least do something like:
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
No, unloading is not supported. You could try delegating to a subprocess.