The example in this article describes how Python transforms XML and XSL into HTML. Share to everyone for your reference. The specific analysis is as follows:
You need to use LIBXML2 here, so you have to install the LIBXML2 module before you can use it. The code is as follows:
#-*-Coding:mbcs-*-#!/usr/bin/pythonimport libxml2, Libxsltclass compoundxml:def __init__ (self): Self._result = No Ne self._xsl = None Self._xml = None def do (self, xml_file_name, xsl_file_name): Self._xml = Libxml2.parsefile (x Ml_file_name) If Self._xml = = None:return 0 Styledoc = libxml2.parsefile (xsl_file_name) If Styledoc = None : return 0 self._xsl = Libxslt.parsestylesheetdoc (styledoc) if self._xsl = = None:return 0 Self._result = Self._xsl.applystylesheet (Self._xml, None) def get_xml_doc (self): return self._result def get_translated (sel f): Return Self._result.serialize (' UTF-8 ') def save_translated (self, file_name): Self._xsl.saveresulttofilename (file_name, Self._result, 0) def release (self): "This function must is called in the end. "' Self._xsl.freestylesheet () Self._xml.freedoc () Self._result.freedoc () self._xsl = None Self._xml = None Self._result = Noneif __name__ = = ' __maIn__ ': test = Compoundxml () test.do (' Test/testxmlutil.xml ', ' test/testxmlutil.xsl ') print test.get_translated () Test.s ave_translated (' test/testxmlutil.htm ') test.release ()
Hopefully this article will help you with Python programming.