Basically, I need to iterate over all environments (i.e. AUX, INT, UAT, etc.) and replace the version number with a new one that I specify as input.
I'd like to do it without writing a file. Instead, my xml is captured as a variable named page_content
here's my xml:
<body>
<p ><br /></p>
<table >
<colgroup>
<col />
<col />
</colgroup>
<tbody>
<tr>
<th>Environment</th>
<th>Version</th>
</tr>
<tr>
<td>INT</td>
<td>1.2.3-5</td>
</tr>
<tr>
<td>XQA</td>
<td><br /></td>
</tr>
<tr>
<td colspan="1">UAT</td>
<td colspan="1"><br /></td>
</tr>
<tr>
<td colspan="1">AUX</td>
<td colspan="1"><br /></td>
</tr>
<tr>
<td colspan="1">SIT</td>
<td colspan="1"><br /></td>
</tr>
<tr>
<td colspan="1">PQA</td>
<td colspan="1"><br /></td>
</tr>
<tr>
<td colspan="1">Production</td>
<td colspan="1"><br /></td>
</tr>
</tbody>
</table>
<p ><br /></p>
</body>
CodePudding user response:
You can use xpath to find the tag and then update the value
Below function updates the version in given xml string and returns updated xml string
def fun(xml_data, env, version):
root = ET.fromstring(xml_data)
node = root.find(f".//tr[td='{env}']/td[2]")
if node:
node.text = version
return ET.tostring(root).decode()