[xml] arguments to an XSLT transformation in Python

Hi there,

I have a Python script that uses an extension function, and also uses an argument that is passed to this function. For example:

import libxml2
import libxslt

def f(ctx, data):
    print data
    return 'nothing'

libxslt.registerExtModuleFunction("foo", "http://example.com/foo";, f)

styledoc = libxml2.parseDoc("""
    <xsl:stylesheet version='1.0'

      <xsl:template match='/'>
        <article><xsl:value-of select='foo:foo($data)' /></article>

style = libxslt.parseStylesheetDoc(styledoc)
doc = libxml2.parseDoc("<doc></doc>")
result = style.applyStylesheet(doc, {'data':"'test'"})
html = style.saveResultToString(result)
print html

This works, but I'd like to pass something else to the transformation (and thus f()) than a string or the result of an xpath expression. What I would to pass is an arbitrary Python object. While the XSLT transformation itself cannot use it, I can pass it to my Python extension function which then should be able to call methods on it.

I can't get this to work though. When I pass in a python object what is passed to f() is an empty list ([]). The only thing that seems to be legal to pass is an xpath expression...

Is something like what I want supported at all? If not, is there a workaround? I need to be able to get to a context object that has nothing to do with XSLT from my Python script. Is it possible to stuff such a thing (before the transformation starts) on what gets passed as ctx, perhaps?



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]