Revision 3226
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/xml_dom.py | ||
---|---|---|
241 | 241 |
if last_only: break |
242 | 242 |
return children |
243 | 243 |
|
244 |
def merge(from_, into): |
|
245 |
'''The into node is saved; the from_ node is deleted.''' |
|
246 |
for child in NodeIter(from_): into.appendChild(child) |
|
247 |
remove(from_) |
|
248 |
|
|
249 |
def merge_adjacent(node): |
|
250 |
'''Repeatedly merges two nodes as long as they or their newly-adjacent |
|
251 |
children have the same tag name''' |
|
252 |
if node == None: return # base case |
|
253 |
|
|
254 |
for node_, next in [(node.previousSibling, node), (node, node.nextSibling)]: |
|
255 |
if node_ != None and next != None and node_.tagName == next.tagName: |
|
256 |
next_first = next.firstChild # save before merge |
|
257 |
merge(next, node_) |
|
258 |
merge_adjacent(next_first) # previousSibling is now node_.lastChild |
|
259 |
|
|
244 | 260 |
##### XML documents |
245 | 261 |
|
246 | 262 |
def create_doc(root='_'): |
Also available in: Unified diff
xml_dom.py: Added merge() and merge_adjacent()