I am writting a python script which goal is to change the value of a xml tag when matching a given criteria : here I'd like to change the active tag from true to false if the id is 'Pikachu'
When I run my python script, I get SyntaxError: invalid predicate error.
Here is my python sample code
import xml.etree.ElementTree as ET
def engineXmlCleaning(bs):
enginePath = "Engine.xml"
tree = ET.parse(enginePath)
root = tree.getroot()
id_to_check='pikachu'
# Search for the element with specified ID
matching_elements = root.findall(".//tototiti.engine.BestMatchingConfig[bestMatchingDataID/id='" + id_to_check + "']")
# Check if element exists
if matching_elements:
print("The element with id " + id_to_check + " exists.")
for element in matching_elements:
active_element = element.find('.//active')
if active_element is not None:
print('... Amending the Active value ...')
active_element.text = 'false'
tree.write(enginePath)
Here is a sample of xml file I'd like to amend via my script
<Engine id="1">
<bestMatchingConfigs id="50">
<tototiti.engine.BestMatchingConfig id="51">
<ParsingTypeName>alex_ordinate</ParsingTypeName>
<bestMatchingDataID id="52">
<id>pikachu</id>
</bestMatchingDataID>
<scriptID id="53">
<id>pika_js</id>
</scriptID>
<active>true</active>
</tototiti.engine.BestMatchingConfig>
<tototiti.engine.BestMatchingConfig id="54">
<ParsingTypeName>don_pillar</ParsingTypeName>
<bestMatchingDataID id="55">
<id>don</id>
</bestMatchingDataID>
<scriptID id="56">
<id>don_js</id>
</scriptID>
<active>true</active>
</tototiti.engine.BestMatchingConfig>
</bestMatchingConfigs>
</Engine>
When I run my python script, I get SyntaxError: invalid predicate error.
if id_to_check in matching_elements:? Id_to_check is always true, because you declare it few lines above.