Skip to content

Instantly share code, notes, and snippets.

@anupamshakya7
Created April 25, 2014 11:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save anupamshakya7/11285898 to your computer and use it in GitHub Desktop.
Save anupamshakya7/11285898 to your computer and use it in GitHub Desktop.
How to transform an XML file using XSLT in Python
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()
@prarabdhtely
Copy link

line 10 : shouldn't it be "f.write(str(result))" ?

@stovberpv
Copy link

import lxml.etree as ET

dom = ET.parse("XML.xml")
xslt = ET.parse("XSLT.xsl")
transform = ET.XSLT(xslt)
newdom = transform(dom)
print(ET.tostring(newdom, pretty_print=True))

@smithasencios
Copy link

looks like it does not work with xls version 2..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment