</pre><p></p><p><span style="font-family:Verdana;font-size:12px;"><strong>1. 在ubuntu的环境下安装libxml2 libxstl</strong></span></p><p><span style="font-family:Verdana;font-size:12px;"><span style="color: rgb(85, 85, 85);">(1)</span> <span style="color: rgb(85, 85, 85);">apt-get install libxml2 libxml2-dev</span></span></p><p><span style="color: rgb(85, 85, 85);"><span style="font-family:Verdana;font-size:12px;">(2) <span style="color: rgb(85, 85, 85);">apt-get install libxlst1 libxslt1-dev</span></span></span></p><p><span style="color: rgb(85, 85, 85);"><span style="font-family:Verdana;font-size:12px;">(3) <span style="color: rgb(85, 85, 85);">apt-get install python-libxml2 python-libxslt1 </span></span></span></p><p><span style="font-family:Verdana;font-size:12px;color:#555555;">如果安装失败,可参考<a target=_blank href="http://*.com/questions/5178416/pip-install-lxml-error" style="color: rgb(109, 88, 135);">http://*.com/questions/5178416/pip-install-lxml-error</a></span></p><p><span style="font-family:Verdana;font-size:12px;color:#555555;">运行如下命令:</span></p><p><span style="font-family:Verdana;font-size:12px;color:#555555;"></span><table border="1" width="300" cellspacing="1" cellpadding="1"><tbody><tr><td><div style="color: rgb(85, 85, 85);">apt-get install python2.6-dev</div><div style="color: rgb(85, 85, 85);">apt-get install libxml2-dev</div><div style="color: rgb(85, 85, 85);">apt-get install libxslt1-dev</div><div style="color: rgb(85, 85, 85);">easy_install lxml</div><span style="color: rgb(85, 85, 85);">apt-get install idle</span></td></tr></tbody></table></p><p><span style="font-family:Verdana;font-size:12px;color:#555555;"><strong>2.centos环境下安装libxml2 libxstl</strong></span></p><p><span style="font-family:Verdana;font-size:12px;color:#555555;">(1)wget http://ftp.gnome.org/pub/GNOME/sources/libxml2/2.6/libxml2-2.6.30.tar.gz</span></p><p><span style="font-family:Verdana;font-size:12px;color:#555555;">(2)wget http://ftp.gnome.org/pub/GNOME/sources/libxslt/1.1/libxslt-1.1.22.tar.gz</span></p><p><span style="font-family:Verdana;font-size:12px;color:#555555;">(3) 安装libxml2</span></p><p><span style="font-family:Verdana;font-size:12px;color:#555555;">tar -zxf <span style="color: rgb(85, 85, 85);">libxml2-2.6.30.tar.gz</span></span></p><p><span style="font-family:Verdana;font-size:12px;color:#555555;">cd libxml2-2.6.30</span></p><p><span style="font-family:Verdana;font-size:12px;color:#555555;">./configure --prefix=/usr/local/libxml2</span></p><p><span style="font-family:Verdana;font-size:12px;color:#555555;">make; make install</span></p><p><span style="font-family:Verdana;font-size:12px;color:#555555;">(4) 安装libxslt</span></p><p><span style="font-family:Verdana;font-size:12px;color:#555555;">tar -zxf libxslt-1.1.22.tar.gz</span></p><p><span style="font-family:Verdana;font-size:12px;color:#555555;">cd libxslt-1.1.22</span></p><p><span style="font-family:Verdana;font-size:12px;color:#555555;">./configure --prefix=/usr/local/libxslt --with-libxml-prefix=/usr/local/libxml2</span></p><p><span style="font-family:Verdana;font-size:12px;color:#555555;">make; make install</span></p><p><span style="font-family:simsun;color:#555555;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px;">3. python脚本(转载)</span></span></span></span></span></span></span></span></span></span></span></p><p><span style="font-family:simsun;color:#555555;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px;"></span></span></span></span></span></span></span></span></span></span></span><pre name="code" class="python"># -*- 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()