[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'
      xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
      xmlns:foo='http://example.com/foo'>

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

style = libxslt.parseStylesheetDoc(styledoc)
doc = libxml2.parseDoc("<doc></doc>")
result = style.applyStylesheet(doc, {'data':"'test'"})
html = style.saveResultToString(result)
style.freeStylesheet()
doc.freeDoc()
result.freeDoc()
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?

Regards,

Martijn



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