X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=python%2Fxmltemplate.py;h=eb50625a600550b2e8bf38d992db0f8cf82366e1;hb=83e98596f1532628bff5d122735577d9615098e5;hp=786ad01337dc9cd407f212c6c2c37bca14fdf051;hpb=9930b9c43329752a066ffc812ec8c968d464d5a2;p=xml-template diff --git a/python/xmltemplate.py b/python/xmltemplate.py index 786ad01..eb50625 100644 --- a/python/xmltemplate.py +++ b/python/xmltemplate.py @@ -32,13 +32,21 @@ def process(node, obj, clean = True): id = None attrs = child.attributes + attrs_to_remove = [] if not attrs is None: for i in range(attrs.length): - attr = attrs.item(0) - if attr.namespaceURI == "http://template.sesse.net/" or attr.name == "id": + attr = attrs.item(i) + if attr.namespaceURI == "http://template.sesse.net/" and attr.localName == "id": id = attr.value if clean: - child.removeAttribute(attr.name) + attrs_to_remove.append(attr.name) + if attr.name.startswith("xmlns:") and attr.value == "http://template.sesse.net/" and clean: + attrs_to_remove.append(attr.name) + + for a in attrs_to_remove: + if child.hasAttribute(a): + child.removeAttribute(a) + # check all substitutions to see if we found anything # appropriate @@ -65,11 +73,12 @@ def process(node, obj, clean = True): frag.appendChild(child) for instance in obj: - newnode = frag.cloneNode(True) - node.appendChild(newnode) - process(newnode, instance, clean) - if clean: - _clean(newnode) + if instance is not None: + newnode = frag.cloneNode(True) + node.appendChild(newnode) + process(newnode, instance, clean) + if clean: + _clean(newnode) # remove all the tags @@ -89,6 +98,12 @@ def process(node, obj, clean = True): _clean(node) def alternate(tag, array, *elems): + i = 0 + for ref in array: + if ref is not None: + ref[tag] = elems[i % len(elems)] + i = i + 1 + return array def _clean(node):