How to transform an XML file using XSLT in Python?
The best way is to do it using lxml, but it only support XSLT 1
import osimport lxml.etree as ETinputpath = "D:\\temp\\"xsltfile = "D:\\temp\\test.xsl"outpath = "D:\\output"for dirpath, dirnames, filenames in os.walk(inputpath): for filename in filenames: if filename.endswith(('.xml', '.txt')): dom = ET.parse(inputpath + filename) xslt = ET.parse(xsltfile) transform = ET.XSLT(xslt) newdom = transform(dom) infile = unicode((ET.tostring(newdom, pretty_print=True))) outfile = open(outpath + "\\" + filename, 'a') outfile.write(infile)
to use XSLT 2 you can check options from Use saxon with python