python3使用lxml库编码实现xml文件转html功能
python3使用lxml库编码实现xml文件转html功能
零、诉苦
由于工作需要,最近研究了使用python脚本实现xml文件转换html文件的功能,费了很大的劲才搞定,费劲的地方不是编码的问题,是python3.7.4的安装问题,不得不吐槽一下python3.7.4真是坑死我了!最开始默认安装的安装在了C盘,结果:创建虚拟环境不成功、使用本地真是环境也不行等等各种问题。重新安装到D盘,安装过程中预编译标准库等了一夜没有成功,中间各种尝试都没有成功啊!希望此bug官方尽快解决。最后下载了python3.6.4安装到了D盘秒成功。
一、环境说明
环境:windows7 64位。
IDE:pycharm2019社区版。
python版本:python3.6.4
lxml版:4.4.1
二、python3.6.4安装及lxml安装
1、python的安装具体步骤不在叙述,建议不要安装在C盘,安装时创建用户权利为所有用户。
2、lxml的安装我采用的是在线安装,直接使用的pycharm安装的,下图中我的已经安装,方法如下:
也可以在cmd窗口下采用命令行的方式pip在线安装或者采用安装包安装都可以
1》pip安装命令:pip install lxml即可
2》安装包命令,下载地址:http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml,参考下图:
注意根据你的python版本进行对应下载,安装命令如下:
pip install lxml-4.3.5-cp36-cp36m-win_amd64.whl
三、lxml和xsl学习使用
1、官方参考文档:https://lxml.de/,可以下载pdf版本看,都是英文的,代码可以看看,如下点击可以预览:
2、xsl模板学习:https://www.w3school.com.cn/xsl/xsl_client.asp
3、参考的博客:
》https://www.cnblogs.com/zhangxinqi/p/9210211.html#_label4
》https://www.cnblogs.com/StitchSun/p/4233904.html
四、测试代码
# -*- coding:utf8 -*-
import sys
from lxml import etree
xml_tree = etree.parse("xmlToHtmlTest.xml")
xsl_tree = etree.parse("xmlToHtmlTest.xsl")
xml_file_content = etree.tostring(xml_tree,encoding='UTF-8',method='xml')
xsl_file_content = etree.tostring(xsl_tree,encoding='UTF-8',method='html')
xsl_dom = etree.XML(xsl_file_content)
xml_dom = etree.XML(xml_file_content)
transform = etree.XSLT(xsl_dom)
html_result = transform(xml_dom)
print(html_result)
str_html = str(html_result)
with open('xmlToHtmlTest.html','w+') as f:
f.write(str_html)
f.close()
运行结果:
五、使用的xml模板和xsl模板
1、xml模板
<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="8" failures="1" disabled="0" errors="0" timestamp="2019-09-02T15:22:29" time="43.06" name="AllTests">
<testsuite name="iTest_001_001_001_004" tests="1" failures="0" disabled="0" errors="0" time="0">
<testcase name="testFunction1_Run" status="run" time="0" classname="iTest_001_001_001_004" />
</testsuite>
<testsuite name="iTest_002_001_001_005" tests="1" failures="0" disabled="0" errors="0" time="0.001">
<testcase name="testFunction2_Run" status="run" time="0" classname="iTest_002_001_001_005" />
</testsuite>
<testsuite name="iTest_003_001_002_001" tests="6" failures="1" disabled="0" errors="0" time="0.368">
<testcase name="testFunction3_Run" status="run" time="0.069" classname="iTest_003_001_002_001" />
<testcase name="testFunction4_Run" status="run" time="0.089" classname="iTest_003_001_002_001" />
<testcase name="testFunction5_Run" status="run" time="0.067" classname="iTest_003_001_002_001" />
<testcase name="testFunction6_Run" status="run" time="0.065" classname="iTest_003_001_002_001" />
<testcase name="testFunction7_Run" status="run" time="0.072" classname="iTest_003_001_002_001" />
</testsuite>
<testsuite name="iTest_004_001_002_001" tests="1" failures="1" disabled="0" errors="0" time="1.925">
<testcase name="testFunction8_Run" status="run" time="1.924" classname="iClover_055_001_001_002">
<failure message="unknown file
SEH exception with code 0xc0000005 thrown in the test body." type=""><![CDATA[unknown file
SEH exception with code 0xc0000005 thrown in the test body.]]></failure>
</testcase>
</testsuite>
</testsuites>
2、xsl模板
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<table cellpadding="2" cellspacing="5" border="1px">
<tr>
<th bgcolor="#20B2AA"><font color="#FFFFFF">Testcase Num(测试用例个数)</font></th>
<th bgcolor="#20B2AA"><font color="#FFFFFF">Failure Num(失败个数)</font></th>
<th bgcolor="#20B2AA"><font color="#FFFFFF">timestamp(生成报告时间)</font></th>
</tr>
<tr>
<td style="font-family: Verdana; font-size: 15px; font-weight: bold;"><xsl:value-of select="testsuites/@tests"/> </td>
<td style="font-family: Verdana; font-size: 15px; font-weight: bold;"><xsl:value-of select="testsuites/@failures"/> </td>
<td style="font-family: Verdana; font-size: 15px; font-weight: bold;"><xsl:value-of select="testsuites/@timestamp"/> </td>
</tr>
</table>
<table cellpadding="2" cellspacing="5">
<tr><td style="font-family: Verdana; font-size: 10px;">
<table align="left" cellpadding="2" cellspacing="0" style="font-family: Verdana; font-size: 10px;">
<tr>
<th bgcolor="#20B2AA"><font color="#FFFFFF"><b>TestSuites(用例编号)</b></font></th>
<th bgcolor="#20B2AA">
<table width="1000px" align="left" cellpadding="1" cellspacing="0" style="font-family: Verdana; font-size: 10px;">
<tr style="font-family: Verdana; font-size: 10px;">
<td width="45%"><font color="#FFFFFF"><b>Testcase(测试名称)</b></font></td>
<td width="15%"><font color="#FFFFFF"><b>Time(花费时间:秒)</b></font></td>
<td width="15%"><font color="#FFFFFF"><b>Result(测试结果)</b></font></td>
<td width="25%"><font color="#FFFFFF"><b>ErrorInfo(错误信息)</b></font></td>
</tr>
</table>
</th>
</tr>
<xsl:for-each select="testsuites/testsuite">
<tr>
<td style="border: 1px solid #20B2AA"><xsl:value-of select="@name"/></td>
<td style="border: 1px solid #20B2AA">
<table width="1000px" align="left" cellpadding="1" cellspacing="0" style="font-family: Verdana; font-size: 10px;">
<xsl:for-each select="testcase">
<tr>
<td style="border: 1px solid #20B2AA" width="45%" rowspan="@tests"><xsl:value-of select="@name"/></td>
<td style="border: 1px solid #20B2AA" width="15%" rowspan="@tests"><xsl:value-of select="@time"/></td>
<xsl:choose>
<xsl:when test="failure">
<td style="border: 1px solid #20B2AA" bgcolor="#ff00ff" width="15%">Failure</td>
<td style="border: 1px solid #20B2AA" bgcolor="#ff00ff" width="25%"><xsl:value-of select="failure/@message"/></td>
</xsl:when>
<xsl:otherwise>
<td style="border: 1px solid #20B2AA" width="15%">Success</td>
<td style="border: 1px solid #20B2AA" width="25%"><xsl:value-of select="failure/@message"/></td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</td>
</tr>
</xsl:for-each>
</table>
</td>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>