XSL transform with Python

Brad Clements bkc at Murkworks.com
Thu Oct 7 00:15:16 CEST 2004


"Vegard Bakke" <vegard at mail.com> wrote in message
news:33a013af.0410060646.3a821dc7 at posting.google.com...
> Hi!
>
> All I want to do is transforming an XML into another XML using an XSLT
> stylesheet.
>
> >From what I have found on the web/usenet there are several xml/xsl
> libraries/wrapper, all in various stages of alpha/beta/stalled
> releases. (Sablotron, Sab-pyth, PySablot, libxml2, libxslt are some of
> those I found.)
>
> The script will be running in a production environment and must
> therefore be stable above all. Alphas and Betas are not that welcome.
>
> Can anyone give me a pointer to what module to use, what is stable,
> and what is and be stay standard for Python?
>


I use libxml2 and libxslt in production.

Simple code like this:

    stylesheetArgs = {}    # optional transform args
    styleDoc = libxml2.parseDoc(docText)  # <xml ...xsl:stylesheet >
    style = libxslt.parseStylesheetDoc(styleDoc)

    doc = libxml2.parseDoc(srcXML)    # <xml input file>
    result = style.applyStylesheet(doc,stylesheetArgs)
    res = style.saveResultToString(result)
    style.freeStylesheet()
    doc.freeDoc()
    result.freeDoc()
    return res

It's plenty fast, and you can re-use compiled stylesheets (that's not
demonstrated above)

-- 
Novell DeveloperNet Sysop #5







More information about the Python-list mailing list