libxslt, libxml and xmldoc problem

Brian Quinlan brian at sweetapp.com
Wed Aug 21 14:19:47 EDT 2002


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

If you can switch libraries, I might suggest using Pyana
(http://pyana.sourceforge.net). You could rewrite that entire code block
as:

import Pyana

result = Pyana.transform2String(Pyana.URI("A_First_Scenario.xml"),
                                Pyana.URI("help_page.xsl"))


If you were writing it for Apache and CGI, there would be no reason to
save the result as a string, so you could write it like:

import Pyana
import sys

result = Pyana.transform2Writer(Pyana.URI("A_First_Scenario.xml"),
                                Pyana.URI("help_page.xsl"),
					  sys.stdout)

Note:
1. I'm the author of Pyana so I'm hardly objective
2. If you are using Linux, you will have to build Pyana yourself

Cheers,
Brian





More information about the Python-list mailing list