How to convert XSD to Python Class
Asked Answered
I

5

53

I just want to know if there is a program that can convert an XSD file to a Python class as JAXB does for Java?

Indigent answered 2/7, 2009 at 6:49 Comment(1)
Since Pythons dynamics means that you can set whatever attributes you want on a class, it really doesn't have field definitions in the same way, so I'm not sure it makes as much sense. But of course you could use getters and setters to make type-checks and whatnot. More interestingly though would be something that creates schema definitions for various Python schema frameworks like Zope schemas, Dexterity or SQLAlchemy. That would really be neat if that existed.Harneen
I
45

generateDS : I think this is the good tool I need

Edit : Actually, generateDS does very well the job !! It generates the Python class with all methods (setters and getters, export to XML, import from XML). It works very well !

Indigent answered 2/7, 2009 at 7:35 Comment(5)
After testing generateDS and PyXB I decided to use PyXB, because it has better validation support (patterns) and it's possible to invoke the generator from within a python module (setup.py) without using something like system().Genevivegenevra
@EnnoGröper You should probably generate the classes ahead of time. One of my XSDs results in a 29k line Python file (35k with PyXB). Unless you don't have access to the needed XSD until install/runtime, don't generate the classes at install/runtime.Romeliaromelle
Export XML is really very slow in generateDS.... Is there any other alternative ? Which can be fast and efficient.Hutchison
This doesn't work with python3.6 so the answer is getting outdatedPogue
After trying all the mentioned solutions here I used the xsdata solution, which was the most convenient for me, given it can be easily installed via pip and is managed on GitHub.Cole
S
11

xsdata generates dataclasses from an XSD file. According to the documentation it was inspired by JAXB.

Sequester answered 19/12, 2021 at 18:18 Comment(0)
P
9

For anyone coming across this question now (in 2021) I suggest checking out xmlschema

I tried the above suggestions (despite the EOL warnings), but wasn't enjoying the experience. Then I discovered xmlschema, and parsed my data in just 3 lines, including the import.

>> import xmlschema
>> data_schema = xmlschema.XMLSchema('my_schema.xsd')
>> data=data_schema.to_dict('my_data.xml')

The imported data is a nested dictionary, with keys and values that match the schema. Beautiful!

Primaveria answered 6/12, 2021 at 1:37 Comment(0)
T
8

PyXB: http://pyxb.sourceforge.net/

Templia answered 30/5, 2012 at 11:6 Comment(6)
PyXB errors out on an XSD I created using dtd2xsd.pl (generateDS has no problem with this XSD). PyXB also seems to generate larger files (29k lines vs 35k for one of my XSDs). As for speed, compile time will be the same for both (probably a full second - huge files), and at runtime generateDS is quite fast so I haven't bothered to test PyXB's output - it can't really be significantly faster.Romeliaromelle
I've spent the better part of two days trying to use PyXB. The documentation is terrible, it generates syntax errors when generating from this XSD companieshouse.gov.uk/ef/xbrl/uk/fr/gaap/ae/2009-06-21/…, due to the fact that identifiers can't contain dashes.Bjork
Once I'd fixed those errors, I just want to start accessing the elements when I've read in the XML.... nope, all the examples use trivial XML without namespaces, and there's almost no API documentation for the objects beyond a bunch of incomprehensible graphsBjork
All in all, to anyone else reading this, stear clear of PyXB. I really don't get the hype, it's incredibly frustrating to work with.Bjork
pyxb is at the end of lifeDarky
Yes, PyXB is EOL. I'm using this fork: github.com/jonfoster/pyxb But I can't really recommend that for "green field" development. If you're already using PyXB it may help you put off the big rewrite to use something else.Templia
R
1

Look at http://pypi.python.org/pypi/rsl.xsd/0.2.3

Also, you might want http://pyxsd.org/ it works very nicely.

Rabbinism answered 2/7, 2009 at 10:6 Comment(2)
I see that pyxsd never got beyond a 0.1 release. Any notable limitations?Dendritic
@Marcin: "Any notable limitations" is impossible to answer. XSD allows one to write things that aren't easy to translate to Python. Is that a "limitation"? And if so, isn't it a limitation in Python itself? The only thing you can do is to use PyXSD for your specific XSD's and see if it works for you. If it doesn't work, then it has a notable limitation. A given set of XSD's may be quite complex or quite simple.Rabbinism

© 2022 - 2024 — McMap. All rights reserved.