Revision 1814
Added by Aaron Marcuse-Kubitza almost 13 years ago
lib/xml_dom.py | ||
---|---|---|
17 | 17 |
|
18 | 18 |
def unescape(str_): return HTMLParser().unescape(str_) |
19 | 19 |
|
20 |
##### Names |
|
21 |
|
|
22 |
def strip_namespace(name): |
|
23 |
namespace, sep, base = name.partition(':') |
|
24 |
if sep != '': return base |
|
25 |
else: return name |
|
26 |
|
|
20 | 27 |
##### IDs |
21 | 28 |
|
22 | 29 |
def get_id(node): return node.getAttribute('id') |
... | ... | |
190 | 197 |
|
191 | 198 |
##### Searching child nodes |
192 | 199 |
|
193 |
def by_tag_name(node, name, last_only=False): |
|
194 |
'''last_only optimization returns last matching node''' |
|
200 |
def by_tag_name(node, name, last_only=False, ignore_namespace=False): |
|
201 |
'''last_only optimization returns only the last matching node''' |
|
202 |
if ignore_namespace: filter_name = strip_namespace |
|
203 |
else: filter_name = lambda name: name |
|
204 |
name = filter_name(name) |
|
205 |
|
|
195 | 206 |
children = [] |
196 | 207 |
if last_only: iter_ = NodeElemReverseIter(node) |
197 | 208 |
else: iter_ = NodeElemIter(node) |
198 | 209 |
for child in iter_: |
199 |
if child.tagName == name:
|
|
210 |
if filter_name(child.tagName) == name:
|
|
200 | 211 |
children.append(child) |
201 | 212 |
if last_only: break |
202 | 213 |
return children |
Also available in: Unified diff
xml_dom.py: Comments: Added clean_comment() and mk_comment(). Searching child nodes: by_tag_name(): Added ignore_namespace option to ignore namespace of node name.