1 # xml.etree test. This file contains enough tests to make sure that
2 # all included components work as they should. For a more extensive
3 # test suite, see the selftest script in the ElementTree distribution.
8 from test
import test_support
21 <body xmlns="http://effbot.org/ns">
34 >>> from xml.etree import ElementTree
35 >>> from xml.etree import ElementInclude
36 >>> from xml.etree import ElementPath
39 def check_method(method
):
40 if not callable(method
):
41 print method
, "not callable"
43 def serialize(ET
, elem
, encoding
=None):
45 file = StringIO
.StringIO()
46 tree
= ET
.ElementTree(elem
)
48 tree
.write(file, encoding
)
51 return file.getvalue()
56 def summarize_list(seq
):
57 return map(summarize
, seq
)
61 Test element tree interface.
63 >>> from xml.etree import ElementTree as ET
65 >>> element = ET.Element("tag", key="value")
66 >>> tree = ET.ElementTree(element)
68 Make sure all standard element methods exist.
70 >>> check_method(element.append)
71 >>> check_method(element.insert)
72 >>> check_method(element.remove)
73 >>> check_method(element.getchildren)
74 >>> check_method(element.find)
75 >>> check_method(element.findall)
76 >>> check_method(element.findtext)
77 >>> check_method(element.clear)
78 >>> check_method(element.get)
79 >>> check_method(element.set)
80 >>> check_method(element.keys)
81 >>> check_method(element.items)
82 >>> check_method(element.getiterator)
84 Basic method sanity checks.
86 >>> serialize(ET, element) # 1
88 >>> subelement = ET.Element("subtag")
89 >>> element.append(subelement)
90 >>> serialize(ET, element) # 2
91 '<tag key="value"><subtag /></tag>'
92 >>> element.insert(0, subelement)
93 >>> serialize(ET, element) # 3
94 '<tag key="value"><subtag /><subtag /></tag>'
95 >>> element.remove(subelement)
96 >>> serialize(ET, element) # 4
97 '<tag key="value"><subtag /></tag>'
98 >>> element.remove(subelement)
99 >>> serialize(ET, element) # 5
100 '<tag key="value" />'
101 >>> element.remove(subelement)
102 Traceback (most recent call last):
103 ValueError: list.remove(x): x not in list
104 >>> serialize(ET, element) # 6
105 '<tag key="value" />'
110 Test find methods (including xpath syntax).
112 >>> from xml.etree import ElementTree as ET
114 >>> elem = ET.XML(SAMPLE_XML)
115 >>> elem.find("tag").tag
117 >>> ET.ElementTree(elem).find("tag").tag
119 >>> elem.find("section/tag").tag
121 >>> ET.ElementTree(elem).find("section/tag").tag
123 >>> elem.findtext("tag")
125 >>> elem.findtext("tog")
126 >>> elem.findtext("tog", "default")
128 >>> ET.ElementTree(elem).findtext("tag")
130 >>> elem.findtext("section/tag")
132 >>> ET.ElementTree(elem).findtext("section/tag")
134 >>> summarize_list(elem.findall("tag"))
136 >>> summarize_list(elem.findall("*"))
137 ['tag', 'tag', 'section']
138 >>> summarize_list(elem.findall(".//tag"))
139 ['tag', 'tag', 'tag']
140 >>> summarize_list(elem.findall("section/tag"))
142 >>> summarize_list(elem.findall("section//tag"))
144 >>> summarize_list(elem.findall("section/*"))
146 >>> summarize_list(elem.findall("section//*"))
148 >>> summarize_list(elem.findall("section/.//*"))
150 >>> summarize_list(elem.findall("*/*"))
152 >>> summarize_list(elem.findall("*//*"))
154 >>> summarize_list(elem.findall("*/tag"))
156 >>> summarize_list(elem.findall("*/./tag"))
158 >>> summarize_list(elem.findall("./tag"))
160 >>> summarize_list(elem.findall(".//tag"))
161 ['tag', 'tag', 'tag']
162 >>> summarize_list(elem.findall("././tag"))
164 >>> summarize_list(ET.ElementTree(elem).findall("/tag"))
166 >>> summarize_list(ET.ElementTree(elem).findall("./tag"))
168 >>> elem = ET.XML(SAMPLE_XML_NS)
169 >>> summarize_list(elem.findall("tag"))
171 >>> summarize_list(elem.findall("{http://effbot.org/ns}tag"))
172 ['{http://effbot.org/ns}tag', '{http://effbot.org/ns}tag']
173 >>> summarize_list(elem.findall(".//{http://effbot.org/ns}tag"))
174 ['{http://effbot.org/ns}tag', '{http://effbot.org/ns}tag', '{http://effbot.org/ns}tag']
180 >>> from xml.etree import ElementTree as ET
182 >>> element = ET.XML("<html><body>text</body></html>")
183 >>> ET.ElementTree(element).write(sys.stdout)
184 <html><body>text</body></html>
185 >>> element = ET.fromstring("<html><body>text</body></html>")
186 >>> ET.ElementTree(element).write(sys.stdout)
187 <html><body>text</body></html>
188 >>> print ET.tostring(element)
189 <html><body>text</body></html>
190 >>> print ET.tostring(element, "ascii")
191 <?xml version='1.0' encoding='ascii'?>
192 <html><body>text</body></html>
193 >>> _, ids = ET.XMLID("<html><body>text</body></html>")
196 >>> _, ids = ET.XMLID("<html><body id='body'>text</body></html>")
204 def check_encoding(ET
, encoding
):
206 >>> from xml.etree import ElementTree as ET
208 >>> check_encoding(ET, "ascii")
209 >>> check_encoding(ET, "us-ascii")
210 >>> check_encoding(ET, "iso-8859-1")
211 >>> check_encoding(ET, "iso-8859-15")
212 >>> check_encoding(ET, "cp437")
213 >>> check_encoding(ET, "mac-roman")
215 ET
.XML("<?xml version='1.0' encoding='%s'?><xml />" % encoding
)
219 # xinclude tests (samples from appendix C of the xinclude specification)
223 XINCLUDE
["C1.xml"] = """\
224 <?xml version='1.0'?>
225 <document xmlns:xi="http://www.w3.org/2001/XInclude">
226 <p>120 Mz is adequate for an average home user.</p>
227 <xi:include href="disclaimer.xml"/>
231 XINCLUDE
["disclaimer.xml"] = """\
232 <?xml version='1.0'?>
234 <p>The opinions represented herein represent those of the individual
235 and should not be interpreted as official policy endorsed by this
240 XINCLUDE
["C2.xml"] = """\
241 <?xml version='1.0'?>
242 <document xmlns:xi="http://www.w3.org/2001/XInclude">
243 <p>This document has been accessed
244 <xi:include href="count.txt" parse="text"/> times.</p>
248 XINCLUDE
["count.txt"] = "324387"
250 XINCLUDE
["C3.xml"] = """\
251 <?xml version='1.0'?>
252 <document xmlns:xi="http://www.w3.org/2001/XInclude">
253 <p>The following is the source of the "data.xml" resource:</p>
254 <example><xi:include href="data.xml" parse="text"/></example>
258 XINCLUDE
["data.xml"] = """\
259 <?xml version='1.0'?>
261 <item><![CDATA[Brooks & Shields]]></item>
265 XINCLUDE
["C5.xml"] = """\
266 <?xml version='1.0'?>
267 <div xmlns:xi="http://www.w3.org/2001/XInclude">
268 <xi:include href="example.txt" parse="text">
270 <xi:include href="fallback-example.txt" parse="text">
271 <xi:fallback><a href="mailto:bob@example.org">Report error</a></xi:fallback>
278 XINCLUDE
["default.xml"] = """\
279 <?xml version='1.0'?>
280 <document xmlns:xi="http://www.w3.org/2001/XInclude">
282 <xi:include href="samples/simple.xml"/>
286 def xinclude_loader(href
, parse
="xml", encoding
=None):
288 data
= XINCLUDE
[href
]
290 raise IOError("resource not found")
292 from xml
.etree
.ElementTree
import XML
298 Basic inclusion example (XInclude C.1)
300 >>> from xml.etree import ElementTree as ET
301 >>> from xml.etree import ElementInclude
303 >>> document = xinclude_loader("C1.xml")
304 >>> ElementInclude.include(document, xinclude_loader)
305 >>> print serialize(ET, document) # C1
307 <p>120 Mz is adequate for an average home user.</p>
309 <p>The opinions represented herein represent those of the individual
310 and should not be interpreted as official policy endorsed by this
315 Textual inclusion example (XInclude C.2)
317 >>> document = xinclude_loader("C2.xml")
318 >>> ElementInclude.include(document, xinclude_loader)
319 >>> print serialize(ET, document) # C2
321 <p>This document has been accessed
325 Textual inclusion of XML example (XInclude C.3)
327 >>> document = xinclude_loader("C3.xml")
328 >>> ElementInclude.include(document, xinclude_loader)
329 >>> print serialize(ET, document) # C3
331 <p>The following is the source of the "data.xml" resource:</p>
332 <example><?xml version='1.0'?>
334 <item><![CDATA[Brooks & Shields]]></item>
339 Fallback example (XInclude C.5)
340 Note! Fallback support is not yet implemented
342 >>> document = xinclude_loader("C5.xml")
343 >>> ElementInclude.include(document, xinclude_loader)
344 Traceback (most recent call last):
345 IOError: resource not found
346 >>> # print serialize(ET, document) # C5
351 from test
import test_xml_etree
352 test_support
.run_doctest(test_xml_etree
, verbosity
=True)
354 if __name__
== '__main__':