简体   繁体   中英

How to obtain HTML file from XML and XSLT

I have an XML file and a corresponding XSLT. I want to get an HTML file that can be run with a browser with python.

Here is my python code:

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))

The problem is that I get as a return None

Because I'm a beginner I lightened up my file because I thought it was the cause of the problem but it turned out that the problem persists:

XML file:

<?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>

Here is the content of my XSLT sheet:

<?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="verfication">
<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="verification/nberror"/></p>             
        <p><a href="">See more Details ...</a></p>
    </body>
</html>
</xsl:template>
</xsl:stylesheet>

There are multiple sources of error in your code. You need to modify both your XML file and XSLT stylesheet.

In your XML file,

  • the value of the statut attribute of the verification element is not enclosed in either single or double quotes
  • for the value of the value attribute of the f_tst element, there is no opening quote

In your XSLT stylesheet,

  • your template matches "verfication", whereas the name of your input element is "verification"
  • <xsl:value-of select="verification/nberror"/> should read <xsl:value-of select="nberror"/> because of the context (template match for verification )

Further, a DTD named "verif.dtd" might not be available to your XSLT processor or browser.

Input (modified)

<?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>

Stylesheet

<?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>

Nitpicking, but alt="Binevenue" should actually be alt="Bienvenue" in French.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM