0

My program is giving errors while I take input using namespaces and XPATH with f-string in for modification in XML File

This works fine and it show all the attributes of the XML file

for x in root.findall(".//{http://www.github/cliffe/SecGen/scenario}vulnerability"):
    print(x.tag,"--->",x.attrib) 
        

But in this piece of code it gives an error NameError: name 'http' is not defined while I take the input data from the user due to f-string

ch = input('\nEnter Tag you want to change : ')
for x in root.findall(f".//{http://www.github/cliffe/SecGen/scenario}vulnerability[@module_path={ch}]"): #ERROR
    print("Tag you want to change ",x.tag,"--->",x.attrib) 
    changes = str(input('\nEnter Your changed tag : '))
    x.attrib['module_path'] = f'{changes}'
mzjn
  • 48,958
  • 13
  • 128
  • 248
  • `f".//{http://www.github/cliffe/SecGen/scenario}` tries to find a variable named `http...` and replace the variable's value in the `findall()` parameter. It seems that this is the Exception that is thrown. In your original script, you have the angle brackets, but the `findall` parameter *was not* an `f-string`. – Yannis P. May 25 '22 at 17:48

1 Answers1

0

Since the namespace URI is delimited by curly braces, you need to use double curly braces for that part of the f-string. You also need quotes around the attribute value in the predicate.

root.findall(f".//{{http://www.github/cliffe/SecGen/scenario}}vulnerability[@module_path='{ch}']")
mzjn
  • 48,958
  • 13
  • 128
  • 248