This item used to discuss how to use XSLT in 4Suite. It is now generalized to cover XSLT processing in general for Python. for the former item, click here
from Ft.Xml.Xslt import Transform, TransformPath result = TransformPath(xmlfile, xsltfile)
- libxml/Python, a wrapper for the libxml
import libxml2
import libxslt
styledoc = libxml2.parseFile("test.xsl")
style = libxslt.parseStylesheetDoc(styledoc)
doc = libxml2.parseFile("test.xml")
result = style.applyStylesheet(doc, None)
style.saveResultToFilename("foo", result, 0)
style.freeStylesheet()
doc.freeDoc()
result.freeDoc()
- libxsltmod, alternate libxslt/Python binding
- maki, a simple, flexible framework that allows you to use XML, XSLT, your webserver, and Python to serve web pages.
- PIRXX provides a Python InteRface to Xerces and Xalan, integrating it into PyXML.
- Pyana, a wrapper for the Xalan XSLT processor in Python:
import Pyana s = Pyana.transformToFile( Pyana.URI(file), Pyana.URI(xsl_file), output)
- Jython and JAXP, thanks to Jarno Virtanen:
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()
- MSXML 4.0, thanks to Ross Ridge:
from os import environ
import win32com.client
def buildPage():
xsluri = 'xsl/plainpage.xsl'
xmluri = 'website.xml'
xsl = win32com.client.Dispatch("Msxml2.FreeThreadedDOMDocument.4.0")
xml = win32com.client.Dispatch("Msxml2.DOMDocument.4.0")
xsl.load(xsluri)
xml.load(xmluri)
xslt = win32com.client.Dispatch("Msxml2.XSLTemplate.4.0")
xslt.stylesheet = xsl
proc = xslt.createProcessor()
proc.input = xml
params = {"url":environ['QUERY_STRING'].split("=")[1]}
for i, v in enumerate(environ['QUERY_STRING'].split("/")[1:]):
params["selected_section%s" % (i + 1)] = "/" + v
for param, value in params.items():
proc.addParameter(param, value)
proc.transform()
return proc.output
print "Content-Type: text/html\n\n"
print buildPage()
- Zope XML Methods provides methods to apply to Zope objects for XML/XSLT processing, supporting 4Suite, libxslt, Sablotron and Pyana
The following threads include some discussion of XSLT processing options in Python:
Comments
link 'how to...' is broken
Shouldn't something as 'designed' as Akara automatically check for valid links???
The broken links are fixed, and, in part inspired by the above comment, Akara now does have a link checker and aggregator that generates an XBEL file. See the "XBEL" icon on the pages, or just grab it at
I couldn't get to this page (or any other from the http://uche.ogbuji.net/tech/akara/nodes/2003-01-01/pyxml-akara page) unless I first selected a Style (default, slate) in the style box. It took me quite a while to discover that selecting a style was the magic key that allowed me access to content.
