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}'
CodePudding user response:
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}']")