Convert XML and XSL to HTML with Python

xiaoxiao2021-03-06  120

A small program for transforming XML for the previous day, used to convert XML, XSL to HTML.

Use libXML2, so you have to install the libXML2 module first.

# - * - CODING: MBCS - * - #! / usr / bin / python

Import libXML2, LIBXSLT

class compoundXML: def __init __ (self): self._result = None self._xsl = None self._xml = None def do (self, xml_file_name, xsl_file_name): self._xml = libxml2.parseFile (xml_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 (self): 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 be called in the end' '' self._xsl.freeStylesheet () self._xml.freeDoc () Self._Result.Freedoc () self._xsl = none self._xml = none self._result = none

if __name__ == '__main__': test = compoundXML () test.do ( 'test / testxmlutil.xml', 'test / testxmlutil.xsl') print test.get_translated () test.save_translated ( 'test / testxmlutil.htm' Test.Release ()

转载请注明原文地址:https://www.9cbs.com/read-127163.html

New Post(0)