Thursday, 24 April 2014

Transform an XML file using XSLT in Python

Howdy,

Form last two months was so busy, so not able to write any new post. But in last two months while doing my project encountered with an issue. The issue was to generate CSV files from the given XML & XSLT file using python. But by doing some research able to find solution using the most popular library LXML. Lets try to explore how its is possible.


Go to your python terminal, and import lxml library
>>> from lxml import etree

1) Open xslt file and read its content.
>>> data = open('D:\Conversion\MACSXML_Parts.xslt')
>>> xslt_content = data.read()

2) Now parse xslt content to etree.XML method. "etree.XML" returns root node (or the result returned by a parser target).
>>> xslt_root = etree.XML(xslt_content)

3) Parse required XML file to etree.parse method, will return ElementTree object like this "<lxml.etree._ElementTree object at 0x01AB1F08>"
>>> dom = etree.parse('D:\Conversion\Cat2015UF.xml')

4) Parse xslt_root node to etree.XSLT(created above). "etree.XSLT" turn an XSL document into an XSLT object.
>>> transform = etree.XSLT(xslt_root)

5) Finally parse dom that has been created above to the new created transformed XSLT object.
>>> result = transform(dom)

Your result contained the appropriate data. You can store this into CSV file like:
>>> f = open('D:\Conversion\MACSXML_Parts.csv', 'w')
>>> f.write(str(result))
>>> f.close()

Full example is as:


 from lxml import etree

 data = open('D:\Conversion\MACSXML_Parts.xslt')

 xslt_content = data.read()
 xslt_root = etree.XML(xslt_content)
 dom = etree.parse('D:\Conversion\Cat2015UF.xml')
 transform = etree.XSLT(xslt_root)
 result = transform(dom)
 f = open('D:\Conversion\MACSXML_Parts.csv', 'w')
 f.write(str(result1))
 f.close()
You can view the same code on gist too. Link is as https://gist.github.com/roopsinghadi/11285898
Hope it is helpful. If it's don't forget to share :P

1 comment:

  1. Seems like github handle is not working any more. Please provide appropriate URL.

    ReplyDelete