[lxml-dev] lxml extensions (fwd)
Dennis Allison
allison at shasta.stanford.edu
Mon Feb 19 17:33:31 CET 2007
Configuration details: Ubuntu 6.06, Python 2.4, lxml 1.1.2.
I am using the lxml XSLT feature to transform an XML specification into a
collection of files for use in a web application. I need some beyond the
usual XSLT and XPATH capabilities and so have been trying to use the
python extension facilities. I am following the "APIs specific to lxml"
and the "Extension functions for XPath and XSLT" portions of the docs.
The XSLT processing follows the published pattern
from lxml import etree
from StringIO import StringIO
# python extension function
def func(dummy):
return 'some result'
# namespace setup
ns = etree.FunctionNamespace('http://mydomain.com/functions')
ns['func'] = func
ns.prefix = 'nf'
# create XSLT transform and apply to doc
xslt_doc = etree.parse StringIO('xslt_file','r').read())
transform = etree.XLST(xslt_doc)
doc = etree.parse(StringIO('xml_file','r').read())
result = transform(doc)
print str(result)
My test XSLT file contains an XPath expression of the form
<xsl:value-of select="nf:p()" />
When applied, the function does not seem to be invoked. Some
experimentation using parameters cause me to suspect that nf:p resolves to
None or the empty string.
I presume the problem is a namespace problem, but it's unclear how to
resolve it from the documentaton. I am guessing that I need to pass the
namespace object to etree.XSLT when I create the transform() but I was
unable to find a hint in the docs.
And, while Google is my friend, it did not locate help in this case.
It did turn up an exchange on the mailing list with a similar problem,
which remains unresolved.
Any assist would be appreciated. Does anyone have a working example of
XSLT processing which includes execution of python extensions.
More information about the lxml-dev
mailing list