libxslt, libxml and xmldoc problem

A M aseem at neurobs.com
Wed Aug 21 01:21:40 EDT 2002


Hi All,

I am trying to convert XML docs to HTML docs using an XSL file on the
fly, on a web server (Zope-Zserver). The function (external method :
altho it can apply to apache and cgi too) that I have which does it is
the following:

## code begin

import libxml2
import libxslt

styledoc = libxml2.parseFile("help_page.xsl")
style = libxslt.parseStylesheetDoc(styledoc)
doc = libxml2.parseFile("A_First_Scenario.xml")
result = style.applyStylesheet(doc, None)
style.saveResultToFilename("-", result, 0)
style.freeStylesheet()
doc.freeDoc()
result.freeDoc()

## code end

Now the only problem with this is that result which is an xmldoc
object can only save to a file, I looked at the source of
libxml.py:xmldoc. It can dump and save in a variety of ways but only
to a file or if the arg is "-", as above, to the standard output.

I wanted to save the contents of result to a string which I could then
send back as part of the RESPONSE object.

So I have two ques:

1. If possible, how do I save the contents directly to a string so as
to bypass the whole "print open(html_file).read()" thing??

OR

2. How do I trap the output being sent to stdout and capture it into a
string??

TIA
AM



More information about the Python-list mailing list