def change(filename, xlsfile): #Used to change and sort XML files dom = et.parse(filename) #Same parse as before xslt_root = etree.XML(xlsfile) #Reads the XLS code transform = et.XSLT(xslt_root) #Sets XLS code to be called newdom = transform(dom) #Transforming our .xml file with our XLS code log = open(filename, 'w') #Opens the temp*.xml file as a writeable print(str(newdom), file = log) #Writes out to the tempfile
def downloadHTML(): # checks if there was a xml and a xsl inserted; if not, show a pop up error message if (inputXml == "" or inputXsl == ""): errorWindow = Tk() errorWindow.title('Download ERROR') errorLabel = Label(errorWindow, text="Please input both the XML and the XSL!", fg='red', font=("Helvetica", 16)) errorLabel.place(x=60, y=50) errorWindow.geometry("550x100+350+200") errorWindow.mainloop() return # convert the XML using the XSL to HTML try: dom = ET.parse(inputXml) xslFile = ET.parse(inputXsl) transform = ET.XSLT(xslFile) print(transform) newdom = transform(dom) print(newdom) resultHtml = (ET.tostring(newdom, pretty_print=True)).decode("UTF-8") except Exception as e: resultXml = "" errorWindow = Tk() errorWindow.title('Convert ERROR') errorLabel = Label(errorWindow, text="XML or XSL is bad!", fg='red', font=("Helvetica", 16)) errorLabel.place(x=60, y=50) errorWindow.geometry("300x100+350+200") errorWindow.mainloop() return # download the HTML inXml = inputXml inXml = inXml.replace(".xml", "") with open(inXml + 'ToHtml.html', "w") as textFile: textFile.write(resultHtml) textFile.close() return
import os.path import xml.etree.ElementTree as ET import lxml.etree as ET import re for filename in os.listdir("."): if filename.endswith(".dfl"): #open xml file #tree = ET.parse(os.path.join('stories_xml', filename)) #root = tree.getroot() #convert xml attributes to elements dom = ET.parse(filename) xslt = ET.parse("attributesToElements.xsl") transform = ET.XSLT(xslt) newdom = transform(dom) # import to new directory save_path = '/Users/timothyhuang/Projects/DFP/danishfolklore/converted xml files/informants' if not os.path.exists(save_path): os.makedirs(save_path) filename = os.path.join(save_path, filename) #write output to filename file = open(filename, 'w') file.write(ET.tostring(newdom, pretty_print=True)) file.close() print(filename + ' completed') continue else: continue