Help with xpath
I'm trying to create an xpath that I can use to iterate through all units of an interface. I have tried uusing path = "./interfaces/interface[starts-with(name,'ae0')]" but this only seems to catch the first unit and doesn't loop through the others remaining units.
for element in root.xpath("./protocols/isis/interface"):
if_name = element.find('name').text
path = "./interfaces/interface[starts-with(name,'ae0')]"
for element in root.xpath(path):
jcs.emit_warning("Adding debug: " + if_name + " " + element.find('name').text +"."+element.find('unit/name').text)
My ae0 has multiple units
# run show interfaces terse | match ^ae0
ae0 up up multiservice
ae0.50 up up multiservice
ae0.73 up up multiservice
ae0.666 up up multiservice
When I tried to debug by running a loop, I only see ae0.50 (the last column) where I was expecting to see the other units as well
warning: Adding debug: ae0.50 ae0.50
warning: Adding debug: ae0.73 ae0.50
warning: Adding debug: ae0.666 ae0.50
I was expecting
warning: Adding debug: ae0.50 ae0.50
warning: Adding debug: ae0.50 ae0.73
warning: Adding debug: ae0.50 ae0.666
warning: Adding debug: ae0.73 ae0.50
warning: Adding debug: ae0.73 ae0.73
warning: Adding debug: ae0.73 ae0.666
warning: Adding debug: ae0.666 ae0.50
warning: Adding debug: ae0.666 ae0.73
warning: Adding debug: ae0.666 ae0.666
Posted May 25, 2021, 4:56 PM UTC
I can't say for sure without seeing other details, but I think your issue is that you're not parsing what you think you are.
The XML you'd get back from looking at the IS-IS configuration using a path of './protocols/isis/interface' would be all interfaces configured for IS-IS. However, this won't get you a list of elements, but a single item.
Then you say:
.find will always get the first entry of that single item.
What you could try is something like this:
The "//" says find all interfaces. Then iterate over those interfaces to do whatever you wanted.
Thanks for the input. The isis part seems to be working fine as I get all the interfaces from the isis hierarchy, what is not working is when looping those interfaces against a list of interfaces from interface
ae0, this is where only theae0.50is seen and the rest of the units are not seen. What xpath can I use if I want to iterate through all units of an interfaceae0?Similar logic with the "//". Something like:
i.e. "Find all interfaces that start with 'ae0'"
That's exactly what I have and it's failing as explained
path = "./interfaces/interface[starts-with(name,'ae0')]"I saw in your other comment that you got it working.
I just want to point out that what I mentioned isn't the same, notice the initial "//" rather than "./".