Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the modern way to to do XML transforms in Python?

I need to do bi-direction transforms between some significantly complex XML and flat-file formats in Python. I'm out of date and don't know how people are solving this problem in the far off future year of 2011.

I've got back up to date with the various Python XML libraries, but it's been 8 years since my last time in XSLT hell and I was amazed after googling around that that's still common.

So how do you go about doing complex XML data transforms?

I'd like to do this in Python because the documents are not direct mappings and there is some processing and calculation required. But I'd still like to pass as much off to a rule engine as possible.

Edit: To be clear I'm interested in techniques more so than specific libraries or tools, but please to post those too. I'm trying hard to avoid the word pattern here, but surely this is a common problem.

Edit 2: I still don't think there were any good answers on general techniques, but the original problem I had was solved using the Bots EDI framework for doing document translations. It is quite heavily focussed on EDI but can be used for generic translation. It was a heavy-weight solution though.

like image 861
Jagu Avatar asked Sep 15 '11 06:09

Jagu


People also ask

How do you process XML in Python?

To read an XML file using ElementTree, firstly, we import the ElementTree class found inside xml library, under the name ET (common convension). Then passed the filename of the xml file to the ElementTree. parse() method, to enable parsing of our xml file. Then got the root (parent tag) of our xml file using getroot().

Is a language used to transform XML data into a different form?

What is XSLT? XSL Transformations ( XSLT 2.0) is a language for transforming XML documents into other XML documents, text documents or HTML documents.

How do I import XML into Python?

Example Read XML File in Python To read an XML file, firstly, we import the ElementTree class found inside the XML library. Then, we will pass the filename of the XML file to the ElementTree. parse() method, to start parsing. Then, we will get the parent tag of the XML file using getroot() .


1 Answers

For python, here is a comprehensive list of available XML libs/modules:

http://wiki.python.org/moin/PythonXml

If you are looking for something simpler than XSLT, XMLStarlet is a set of command line tools which may be of interest for you:

http://xmlstar.sourceforge.net/

As any command line tool, this is not especially made for Python, but can be easily integrated into a python script.

like image 61
Doc Brown Avatar answered Oct 11 '22 14:10

Doc Brown