This article describes how to convert xml and xsl files to html file storage in python. For more information, see the following prerequisites: install libxml2 libxstl.
Http://xmlsoft.org/XSLT/index.html
Installation package download: http://xmlsoft.org/sources/
Download the exe installation file on windows:
Http://xmlsoft.org/sources/win32/python/
Here is the reprinted test code:
The code is as follows:
#-*-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, pai_file_name ):
Self. _ xml = libxml2.parseFile (xml_file_name)
If self. _ xml = None:
Return 0
Styledoc = libxml2.parseFile (pai_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 ()