Set the requested attribute key and value for matched xpath element. CLI Example: .. code-block:: bash salt '*' xml.set_attribute /tmp/test.xml ".//element[@id='3']" editedby "gal"
(file, element, key, value)
| 81 | |
| 82 | |
| 83 | def set_attribute(file, element, key, value): |
| 84 | """ |
| 85 | Set the requested attribute key and value for matched xpath element. |
| 86 | |
| 87 | CLI Example: |
| 88 | |
| 89 | .. code-block:: bash |
| 90 | |
| 91 | salt '*' xml.set_attribute /tmp/test.xml ".//element[@id='3']" editedby "gal" |
| 92 | """ |
| 93 | try: |
| 94 | root = ET.parse(file) |
| 95 | element = root.find(element) |
| 96 | except AttributeError: |
| 97 | log.error("Unable to find element matching %s", element) |
| 98 | return False |
| 99 | element.set(key, str(value)) |
| 100 | root.write(file) |
| 101 | return True |