2010-07-16

XSLT Transform to TXT, with LXML

Maybe this should be obvious, but it wasn't to me. I've got an XML document and an XSLT stylesheet. But that stylesheet just produces text, not XML; it is essentially a template for an e-mail [with some iterations, so something more complex than OIE's very convenient rowTemplateAction can handle]. So I was extending the transformAction for performing XSLT transforms that produce other than XML... but the documentation is a bit thin and every example is XML results. The trick is pretty simple, just
unicode(result)
and make sure [of course] that you have
<xsl:output method="txt" encoding="utf-8" omit-xml-declaration="yes"/>
declared in the template. So it should be as simple as:
def do_action(self):
        source = etree.parse(self._rfile)
        xslt = etree.fromstring(self._xslt)
        transform = etree.XSLT(xslt)
        result = transform(source)
        self._wfile.write(unicode(result))
in order to transform to just about anything.

0 comments:

Post a Comment