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

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


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.


Although it’s only useful for writing XML, XMLwitch is freakin’ amazing. For doing non-XML to XML transformations, I highly recommend it!


Hi Dimitre I use lxml to manipulate xml in python.

I have posted some techniques for managing large amounts of xml schemas, namespaces etc.. Automatic XSD validation

One suggestion is to try to use the full xpath when possible.

For example if i had a complex type:

<Person> <name/> <age/></person>

i could run into the problem if i dont use /Person/name and later that complex type changes to:

<person> <name/> <age/> <child>   <son>     <name/>     <age/>   </son></child></person>

The reason being now 'name' exists in multiple places.

Also be aware of schemas that allow for many "persons" in this example. you may need to provide a "key" with your xpath to determine which person you are referencing. you could have 5 or 6 Persons in your xml the xpaths would be identical but the names uniqe, the name would then be your key to use to reference each particular person.

I would also suggest writing your own wrapper functions around lxml that suit your needs. What i did was I created an xmlUtil.py file that contained xml generic functions that i needed. I then created a myXML.py file that had assumptions about my specific xml and behavior. the xmlUtil.py functions only accept the xml content ( this is in case i decide to use something instead of lxml it will be easy to change).

Hope some of this helps. wish i could be more helpful but question is very open ended.