From 99894bd6f2f22cd36f8edd4140c762f5e1142025 Mon Sep 17 00:00:00 2001 From: Goffi Date: Fri, 22 Jan 2016 20:24:17 +0100 Subject: [PATCH] core (xml_tools): added a method to find all elements corresponding to criteria --- src/tools/xml_tools.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/tools/xml_tools.py b/src/tools/xml_tools.py index 371d4d6e..e5361f83 100644 --- a/src/tools/xml_tools.py +++ b/src/tools/xml_tools.py @@ -1395,3 +1395,20 @@ def getText(node): if child.nodeType == child.TEXT_NODE: data.append(child.wholeText) return u"".join(data) + +def findAll(elt, names=None, namespaces=None): + """Find child element at any depth matching criteria + + @param elt(domish.Element): top parent of the elements to find + @param names(iterable, None): names to match + None to accept every names + @param namespace(unicode): URIs to match + None to accept every namespaces + """ + + for child in elt.elements(): + if ((not names or child.name in names) and + (not namespaces or child.uri in namespaces)): + yield child + for found in findAll(child, names): + yield found