Search code examples
pythonxhtmllibxml2xhtml-1.0-strictlibxslt

libxslt's applyStylesheet() produces broken XHTML


I've got an XML data file that I'm applying an XSL stylesheet to, using libxslt in Python. Things are almost perfect, but every instance of <hr/> in the XSL file is being turned into <hr> in the output from unicode(applyStylesheet()). This is pretty much all I'm doing:

style = libxslt.parseStylesheetDoc(libxml2.parseFile('template.xsl'))
xmlDoc = libxml2.parseFile('data.xml')
data = unicode(self.style.applyStylesheet(xmlDoc, None))
xmlDoc.freeDoc()

Am I doing something wrong? Is there an option that I'm missing to make the output valid XHTML?

(My current plan is to append .replace('<hr>','<hr/>') to the data = line to just make it work, but it seems like there should be a better way.)


Solution

  • Can we see the template.xsl file? Does it contain the <xsl:output/> tag? Is the method attribute of this tag set to html?

    If it is, then libxsl automatically strips all / from self closing tags.

    Beware that, xml output method does not always output correct html, refer to xml, html or xhtml in <xsl:output>: Which is the better choice? for more information.

    As explained in the answer of the linked question, the best choice would be xhtml, but it is only supported by xsl starting at version 2.