Exemplo n.º 1
0

def bar(ctxt, x):
    return "%d" % (x + 2)


doc = libxml2.parseFile("tst.xml")
ctxt = doc.xpathNewContext()
res = ctxt.xpathEval("//*")
if len(res) != 2:
    print("xpath query: wrong node set size")
    sys.exit(1)
if res[0].name != "doc" or res[1].name != "foo":
    print("xpath query: wrong node set value")
    sys.exit(1)
libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)
libxml2.registerXPathFunction(ctxt._o, "bar", None, bar)
i = 10000
while i > 0:
    res = ctxt.xpathEval("foo(1)")
    if res != 2:
        print("xpath extension failure")
        sys.exit(1)
    i = i - 1
i = 10000
while i > 0:
    res = ctxt.xpathEval("bar(1)")
    if res != "3":
        print("xpath extension failure got %s expecting '3'")
        sys.exit(1)
    i = i - 1
Exemplo n.º 2
0
def foo(ctx, str):
    global mydoc

    #
    # test returning a node set works as expected
    #
    parent = mydoc.newDocNode(None, 'p', None)
    mydoc.addChild(parent)
    node = mydoc.newDocText(str)
    parent.addChild(node)
    return [parent]

doc = libxml2.parseFile("tst.xml")
ctxt = doc.xpathNewContext()
libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)
res = ctxt.xpathEval("foo('hello')")
if type(res) != type([]):
    print("Failed to return a nodeset")
    sys.exit(1)
if len(res) != 1:
    print("Unexpected nodeset size")
    sys.exit(1)
node = res[0]
if node.name != 'p':
    print("Unexpected nodeset element result")
    sys.exit(1)
node = node.children
if node.type != 'text':
    print("Unexpected nodeset element children type")
    sys.exit(1)