File: test_dom.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 (42 lines) | stat: -rw-r--r-- 800 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
40
41
42
from xml.dom.transformer import *
from xml.dom.dc_builder import DcBuilder
from xml.dom.pyhtml import *
from xml.dom.writer import HtmlWriter

test_text = """<?xml>
<doc>
<title>This is a test</title>

<h1>Don't panic</h1>

<p>Maybe it will work.</p>

<h2>We can handle it</>

<h3>Yes we can</h3>
<h3>Or maybe not</h3>

End of test.
</doc>
"""


class TestTransformer(Transformer):
	def do_doc(self, node):
		return [HTML(
			HEAD(
				TITLE(cdata(having(node._children, 'this.GI == "title"')))
			),
			BODY(
				{'bgcolor': '#FFFFFF', 'text': '#000000'},
				having(node._children, 'this.GI in ["h1", "h2", "h3"]')
			)
		)]

transformer = TestTransformer()
parser = DcBuilder()
parser.feed(test_text)
document = transformer.transform(parser.document)

writer = HtmlWriter()
writer.write(document)