01-29-2018 05:08 AM - edited 03-01-2019 04:05 AM
Hi,
I've been trying to use both xpath_eval and xpath_eval_expr maapi methods, and I seem to get slightly different results.
In cases where my xpath query should return multiple values, with xpath_eval I can see all matching nodes, but when I use xpath_eval_expr, the return value contains only the first xpath expression that matches the query.
Is that expected behavior?
Am I missing something obvious?
Thanks,
Yftach
(NSO 4.5.2)
For example:
u = "/aaa/authentication/users/user/name"
def foo(x,y):
print('x = {}'.format(x))
print('y = {}'.format(y))
maapi.xpath_eval(sock_maapi, th, u, foo, None, '/')
x = /aaa:aaa/authentication/users/user{admin}/name
y = admin
x = /aaa:aaa/authentication/users/user{oper}/name
y = oper
x = /aaa:aaa/authentication/users/user{private}/name
y = private
x = /aaa:aaa/authentication/users/user{public}/name
y = public
print(maapi.xpath_eval_expr(sock_maapi, th, u, None, '/'))
admin
Solved! Go to Solution.
01-31-2018 12:50 AM
As you have seen, xpath_eval() gives you a callback for each match. This is typically what you want.
xpath_eval_expr() is meant to follow the plain XPATH query rules, and should then return a "concatenation of the string representation of all matching nodes", which would be "adminoperprivatepublic". Completely useless (but defined in the standard). This is only really useful when you are sure there is only even a single matching instance. So by making this function only return the first instance, it gets a bit safer.
Besides the above two, there's a newer query function called query_start()/query_result()/... which gives you a list of matches and fine control over how many matches you are able to consume right now (for paging) etc.
01-31-2018 12:50 AM
As you have seen, xpath_eval() gives you a callback for each match. This is typically what you want.
xpath_eval_expr() is meant to follow the plain XPATH query rules, and should then return a "concatenation of the string representation of all matching nodes", which would be "adminoperprivatepublic". Completely useless (but defined in the standard). This is only really useful when you are sure there is only even a single matching instance. So by making this function only return the first instance, it gets a bit safer.
Besides the above two, there's a newer query function called query_start()/query_result()/... which gives you a list of matches and fine control over how many matches you are able to consume right now (for paging) etc.
01-31-2018 01:53 AM
I see. That indeed explains the behavior I saw.
I'll look into the functions you mentioned.
Thank you!
Yftach
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide