First off, this is a great project. I just have a few quesetions.
For my particular application I need to be able to generate new schema on the fly, then create and load python bindings for the data types described by those schema. This seems like it should be doable with pyxb, but I can't figure out how. My first thought was to create bindings for the W3 schema schema, but I couldn't get it to build with pyxbgen. My next thought was to tear the appropriate guts out of pyxbgen, but I figured I should see if there was a better way before that.
Also, is there a better way to generate bindings while running than either 1) calling pyxbgen with Popen or 2) tearing a slightly different set of guts than the aforementioned out of pyxbgen?
I guess it generally seems like pyxbgen should be accessible programmatically as well as as a tool, and I'd like some more information on if that's the case.
Thanks!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In the tests directory there are a large number of examples of Python scripts that generate bindings from schema either as a string or from a file, import them into the current session, and use them. Do those help answer your question?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
They help to answer part of my question. Specifically, the latter part. I now think I understand how to use pyxb.binding.generate to generate bindings while running (or at least, close enough to be getting on with). However, I still don't really see a way to do the first bit of my question.
To clarify somewhat, what I want to do is build a schema in a similar way to that described on this page: http://pyxb.sourceforge.net/userref_usebind.html#creating-instances-in-python-code
and then generate bindings based on it (presumably using pyxb.binding.generate). For the latter half, at the very least I could write to file and read it back in (which I'd probably want to do anyway), but I don't really see how to do the first bit. As I said, my first attempt involved generating bindings for the schema that defines the schema format (located at http://www.w3.org/2001/XMLSchema.xsd), but I was unsuccessful. The comments in the --help for pyxbgen seem to indicate that this is possible with --allow-builtin-generation, but I couldn't figure it out (got an AttributeError about Element having no attribute getAttribute, when clearly it does). Any help with that, or a pointer to a better approach, would be greatly appreciated.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
OK, now I see. No, pyxb does not support bindings for http://www.w3.org/2001/XMLSchema itself. This is historical: during its evolution the Python classes for schemas had to be far ahead of what PyXB could support, and once the system matured there was no reason to replace them (though I have considered it off and on).
You should be able to do what you want by building the schema representation using a DOM interface, then converting that to the underlying component model that PyXB uses to generate bindings.
The exact technique is beyond what I can provide as free online support, but you should look into how PyXB uses either SAX or DOM to create a DOM representation of the XML schema content, how it passes that to constructors in pyxb.xmlschema.structures (pyxb.sourceforge.net api) to create the component model, then generate the bindings from that with pyxb.binding.generate.GeneratePython (not documented).
It's possible a standard xml.dom.minidom tree would not work and you would have to use pyxb.utils.saxdom (pyxb.sourceforge.net) for the component model; the documentation suggests that DOMImplementation does not support converting back to XML but it's pretty easy to walk a DOM tree and clone it into a different implementation. (Somewhere in pyxb.utils.domutils or a nearby module there's code that does that.)
Sorry I can't help more here: what you want to do is a major undertaking and the time it would take is beyond what I can afford to give.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
No worries, I appreciate the help you have been able to give. If the module doesn't support it out of the box, I'm sure I can put something together using SAX or the DOM as you mention.
Thanks for the assistance!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello,
First off, this is a great project. I just have a few quesetions.
For my particular application I need to be able to generate new schema on the fly, then create and load python bindings for the data types described by those schema. This seems like it should be doable with pyxb, but I can't figure out how. My first thought was to create bindings for the W3 schema schema, but I couldn't get it to build with pyxbgen. My next thought was to tear the appropriate guts out of pyxbgen, but I figured I should see if there was a better way before that.
Also, is there a better way to generate bindings while running than either 1) calling pyxbgen with Popen or 2) tearing a slightly different set of guts than the aforementioned out of pyxbgen?
I guess it generally seems like pyxbgen should be accessible programmatically as well as as a tool, and I'd like some more information on if that's the case.
Thanks!
In the tests directory there are a large number of examples of Python scripts that generate bindings from schema either as a string or from a file, import them into the current session, and use them. Do those help answer your question?
They help to answer part of my question. Specifically, the latter part. I now think I understand how to use pyxb.binding.generate to generate bindings while running (or at least, close enough to be getting on with). However, I still don't really see a way to do the first bit of my question.
To clarify somewhat, what I want to do is build a schema in a similar way to that described on this page:
http://pyxb.sourceforge.net/userref_usebind.html#creating-instances-in-python-code
and then generate bindings based on it (presumably using pyxb.binding.generate). For the latter half, at the very least I could write to file and read it back in (which I'd probably want to do anyway), but I don't really see how to do the first bit. As I said, my first attempt involved generating bindings for the schema that defines the schema format (located at http://www.w3.org/2001/XMLSchema.xsd), but I was unsuccessful. The comments in the --help for pyxbgen seem to indicate that this is possible with --allow-builtin-generation, but I couldn't figure it out (got an AttributeError about Element having no attribute getAttribute, when clearly it does). Any help with that, or a pointer to a better approach, would be greatly appreciated.
OK, now I see. No, pyxb does not support bindings for http://www.w3.org/2001/XMLSchema itself. This is historical: during its evolution the Python classes for schemas had to be far ahead of what PyXB could support, and once the system matured there was no reason to replace them (though I have considered it off and on).
The option you mention was used during development for other, simpler schema like http://www.w3.org/2001/XMLSchema-hasFacetAndProperty. As the documentation mentions you really can't use it for anything.
You should be able to do what you want by building the schema representation using a DOM interface, then converting that to the underlying component model that PyXB uses to generate bindings.
The exact technique is beyond what I can provide as free online support, but you should look into how PyXB uses either SAX or DOM to create a DOM representation of the XML schema content, how it passes that to constructors in pyxb.xmlschema.structures (pyxb.sourceforge.net api) to create the component model, then generate the bindings from that with pyxb.binding.generate.GeneratePython (not documented).
It's possible a standard xml.dom.minidom tree would not work and you would have to use pyxb.utils.saxdom (pyxb.sourceforge.net) for the component model; the documentation suggests that DOMImplementation does not support converting back to XML but it's pretty easy to walk a DOM tree and clone it into a different implementation. (Somewhere in pyxb.utils.domutils or a nearby module there's code that does that.)
Sorry I can't help more here: what you want to do is a major undertaking and the time it would take is beyond what I can afford to give.
No worries, I appreciate the help you have been able to give. If the module doesn't support it out of the box, I'm sure I can put something together using SAX or the DOM as you mention.
Thanks for the assistance!