cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1504
Views
35
Helpful
4
Replies

Is it possible to generate an xpath to a node using the Python API?

Hubro
Level 1
Level 1

Given a Maagic "Node" object, I need to automatically generate an xpath to it.

 

I know NSO can already do this, as it generates xpaths when running "show full-configuration foo bar | display xpath". Is there any way to do this using NSO's Python API?

 

I know I can get the keypath to the node using the "_path" attribute, but that value can't be saved in a leaf with the "instance-identifier" type, as it has to be an xpath.

1 Accepted Solution

Accepted Solutions

vleijon
Cisco Employee
Cisco Employee

You can use _ncs.xpath_pp_kpath for this.

View solution in original post

4 Replies 4

vleijon
Cisco Employee
Cisco Employee

You can use _ncs.xpath_pp_kpath for this.

That sounds ideal, but there is one issue: It requires a HKeypathRef object, but all I get from node._path is a string. Is there some way to convert a string to a HKeypathRef object, or can I get a HKeypathRef from a Maagic node somehow?

This is one way to get it, although I do think it is not quite elegant:

def maagic_node_to_xpath(maagic_node):
    with ncs.maapi.single_read_trans('admin', 'system') as m:
        m.cd(maagic_node._path)
        return _ncs.xpath_pp_kpath(m.getcwd_kpath())

"getcwd_kpath" returns the current position in maapi, as a HKeypathRef. So, you first 'cd' to the required path, then get it's HKeypathRef using which you get the XPath.

/Ram

Is there any restconf equivalent call to get the service configs in xpath format?

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the NSO Developer community: