Previous
Parsing XML in Jython with Java SDK's libraries

How to do an XSL transformation in Jython

Since Python standard library doesn't have (to my knowledge, at least) an XSL transformer, and if you happen to have Jython installed, here's how you can do an XSL transformation with Java SDK's libraries:
 from java.io import FileInputStream
 from java.io import ByteArrayOutputStream
 

from javax.xml.transform import TransformerFactory from javax.xml.transform.stream import StreamSource from javax.xml.transform.stream import StreamResult

input = FileInputStream("test.xsl") xslSource = StreamSource(input) xslTemplate = TransformerFactory.newInstance().newTemplates(xslSource); transformer = xslTemplate.newTransformer()

output = ByteArrayOutputStream() result = StreamResult(output)

source = StreamSource(FileInputStream("source.xml")) transformer.transform(source, result)

print output # .. or output.toString()

Filed under: