Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从XML和XSLT中获取HTML文件_Python_Html_Xslt_Xslt 1.0 - Fatal编程技术网

Python 如何从XML和XSLT中获取HTML文件

Python 如何从XML和XSLT中获取HTML文件,python,html,xslt,xslt-1.0,Python,Html,Xslt,Xslt 1.0,我有一个XML文件和相应的XSLT。我想得到一个HTML文件,可以用python浏览器运行 以下是我的python代码: from lxml import etree dom = etree.parse(path_xml) xslt =etree.parse(path_xslt) transform = etree.XSLT(xslt) newdom = transform(dom) print(etree.tostring(newdom, pretty_print=True)) 问题是我没有

我有一个XML文件和相应的XSLT。我想得到一个HTML文件,可以用python浏览器运行

以下是我的python代码:

from lxml import etree
dom = etree.parse(path_xml)
xslt =etree.parse(path_xslt)
transform = etree.XSLT(xslt)
newdom = transform(dom)
print(etree.tostring(newdom, pretty_print=True))
问题是我没有得到回报

因为我是一名初学者,我简化了我的文件,因为我认为这是问题的原因,但事实证明问题仍然存在:

XML文件:

<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet "comparexsl.xsl"?>
<!DOCTYPE verification SYSTEM "verif.dtd">
<verification statut=false>
  <nberror>2537</nberror>
  <f_ref type="t1" value="val"/>
  <f_tst type="t2" value=val2"/>
  <f_ref type="x" value="20"/>
  <f_tst type="x" value="201"/>
  <cnxn log="l" mdp="mdp1" />
  <option name="MAJ" title="" result="False">
    <time time1="116" time2="-31.25" time3="11">
    <time_a time1="0" time2=""/>
    <time_o time1="15" time2="-40"/></time>
  </option>
</verification>

2537

代码中存在多个错误源。您需要修改XML文件和XSLT样式表

在XML文件中

  • verification
    元素的
    statut
    属性的值不包含在单引号或双引号中
  • 对于
    f_tst
    元素的
    value
    属性的值,没有起始引号
在XSLT样式表中

  • 模板匹配“verfication”,而输入元素的名称为“verification”
  • 由于上下文的原因,
    应为
    验证的模板匹配)
此外,名为“verif.DTD”的DTD可能对XSLT处理器或浏览器不可用

输入(修改)


2537
样式表

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" doctype-system="about:legacy-compat" encoding="UTF-8"
indent="yes" />              
<xsl:template match="verification">
<html>
    <head>
        <meta charset="utf-8"/>
    </head>
    <body>
        <p><img src="image.jpg" alt="Binevenue"/></p>
        <h1 align="center" > Comparaison Status= FALSE </h1>
        <p>Nombre d'erreurs = <xsl:value-of select="nberror"/>
                    </p>             
        <p><a href="">See more Details ...</a></p>
    </body>
</html>
</xsl:template>
</xsl:stylesheet>

比较状态=错误 列名=


挑剔,但是
alt=“Binevenue”
实际上应该是法语的
alt=“Bienvenue”

你也可以添加源XML的内容吗?@MatthewGreen:我看到你添加了一个结束语
;你确定他有吗?“这可能是他问题的根源。”HughBothwell说,只是在最初的帖子中没有用空格隔开。你可以在修订历史中看到它,如果你没有得到任何输出,你的样式表就不会被调用,python代码也会出错。如果只得到
DOCTYPE
声明和文本内容,则模板不匹配
<?xml version="1.0" encoding="UTF-8" ?>
<verification statut="false">
  <nberror>2537</nberror>
  <f_ref type="t1" value="val"/>
  <f_tst type="t2" value="val2"/>
  <f_ref type="x" value="20"/>
  <f_tst type="x" value="201"/>
  <cnxn log="l" mdp="mdp1" />
  <option name="MAJ" title="" result="False">
    <time time1="116" time2="-31.25" time3="11">
    <time_a time1="0" time2=""/>
    <time_o time1="15" time2="-40"/></time>
  </option>
</verification>
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" doctype-system="about:legacy-compat" encoding="UTF-8"
indent="yes" />              
<xsl:template match="verification">
<html>
    <head>
        <meta charset="utf-8"/>
    </head>
    <body>
        <p><img src="image.jpg" alt="Binevenue"/></p>
        <h1 align="center" > Comparaison Status= FALSE </h1>
        <p>Nombre d'erreurs = <xsl:value-of select="nberror"/>
                    </p>             
        <p><a href="">See more Details ...</a></p>
    </body>
</html>
</xsl:template>
</xsl:stylesheet>