How to use python to generate an html file storage example from an xmlxsl file

Source: Internet
Author: User
Tags xsl
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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.