S
shaun roe
I am coming to libxml2 python binding for the first time and find I have
to make an xpath extension function. The standard examples at
http://xmlsoft.org/python.html
are a little too simple. I might have expected, given those examples, to
be able to do this:
import sys
import libxml2
def foo(ctx, x):
return x + 1
doc = libxml2.parseFile("channels.xml")
ctxt = doc.xpathNewContext()
libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)
res = ctxt.xpathEval("/channels/channel/value[1]/foo(1)")[0]
print res
(yes, its a silly example) which would return the value 2 . However,
this crashes and burns at the evaluation. I am guessing that
a) There is some magic in the return value whereby it needs to be added
to the result tree manually
and
b) That when I try the next stage with foo(.) it will again crash
because I need to get the value from the context node. (here I am
confused by the difference between the 'xpath context' and 'xpath parser
context')
can anyone enlighten me?
cheers
shaun
ps my source document format is like this:
<channels>
<channel id="0">
<value name="myval">67</value>
</channel>
.
.
.
</channels>
to make an xpath extension function. The standard examples at
http://xmlsoft.org/python.html
are a little too simple. I might have expected, given those examples, to
be able to do this:
import sys
import libxml2
def foo(ctx, x):
return x + 1
doc = libxml2.parseFile("channels.xml")
ctxt = doc.xpathNewContext()
libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)
res = ctxt.xpathEval("/channels/channel/value[1]/foo(1)")[0]
print res
(yes, its a silly example) which would return the value 2 . However,
this crashes and burns at the evaluation. I am guessing that
a) There is some magic in the return value whereby it needs to be added
to the result tree manually
and
b) That when I try the next stage with foo(.) it will again crash
because I need to get the value from the context node. (here I am
confused by the difference between the 'xpath context' and 'xpath parser
context')
can anyone enlighten me?
cheers
shaun
ps my source document format is like this:
<channels>
<channel id="0">
<value name="myval">67</value>
</channel>
.
.
.
</channels>