File: transform.py

package info (click to toggle)
python-xml 0.4.19981014-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 2,124 kB
  • ctags: 3,099
  • sloc: ansic: 9,075; python: 8,150; xml: 7,940; makefile: 84; sh: 41
file content (39 lines) | stat: -rw-r--r-- 691 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from xml.dom.core import *

def explode(nodes, condition):
	for node in nodes:
		explode_node(node, condition)

	return nodes

def explode_node(node, condition):
	if node.NodeType != ELEMENT:
		return node

	explode(node.getChildren(), condition)

	if eval(condition, {}, {'this': node}):
		parent = node.getParentNode()
		for child in node.getChildren():
			parent.insertBefore(child, node)
		parent.removeChild(node)

	return node
		
		
if __name__ == '__main__':
	from xml.dom.pyhtml import *
	from xml.dom.writer import HtmlWriter

	tree = HTML(BODY(H1(A({'href':'blah'}, 'blah blah'))))
	w = HtmlWriter()
	w.write(tree)

	explode_node(tree, 'this.GI == "A"')
	w.write(tree)