Reputation: 133
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>
Upvotes: 2
Views: 2607
Reputation: 22617
There are multiple sources of error in your code. You need to modify both your XML file and XSLT stylesheet.
In your XML file,
statut
attribute of the verification
element is not enclosed in either single or double quotesvalue
attribute of the f_tst
element, there is no opening quoteIn your XSLT stylesheet,
<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.
Upvotes: 2