cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1565
Views
0
Helpful
2
Replies

python xpath_eval() vs. xpath_eval_expr()

yfherzog
Cisco Employee
Cisco Employee

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

1 Accepted Solution

Accepted Solutions

Jan Lindblad
Cisco Employee
Cisco Employee

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.

View solution in original post

2 Replies 2

Jan Lindblad
Cisco Employee
Cisco Employee

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.

I see. That indeed explains the behavior I saw.

I'll look into the functions you mentioned.

Thank you!

Yftach