[Kimchi-devel] [WIP PATCH 3/5] Allow to update XML attribute in "xml_item_update"

Crístian Viana vianac at linux.vnet.ibm.com
Fri Oct 17 08:26:50 UTC 2014


The function "xml_item_update" only updates the content of any given
tag, filtered by an XPath expression. However, one might want to update
one of the tag's attributes instead of its content.

Add an optional parameter to "xml_item_update" which sets the attribute
name to be updated; if not set, the function will behave as before, i.e.
update the tag's content.

Signed-off-by: Crístian Viana <vianac at linux.vnet.ibm.com>
---
 src/kimchi/xmlutils.py | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/kimchi/xmlutils.py b/src/kimchi/xmlutils.py
index 8b19a20..ee56203 100644
--- a/src/kimchi/xmlutils.py
+++ b/src/kimchi/xmlutils.py
@@ -34,13 +34,18 @@ def xpath_get_text(xml, expr):
     return ret
 
 
-def xml_item_update(xml, xpath, value):
+def xml_item_update(xml, xpath, value, attr=None):
     ElementTree.register_namespace(kimchi.model.utils.KIMCHI_NAMESPACE,
                                    kimchi.model.utils.KIMCHI_META_URL)
 
     root = ElementTree.fromstring(xml)
     item = root.find(xpath)
-    item.text = value
+
+    if attr is None:
+        item.text = value
+    else:
+        item.set(attr, value)
+
     return ElementTree.tostring(root, encoding="utf-8")
 
 
-- 
1.9.3




More information about the Kimchi-devel mailing list