1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# xml.etree test.  This file contains enough tests to make sure that
2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# all included components work as they should.
3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Large parts are extracted from the upstream test suite.
4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# IMPORTANT: the same doctests are run from "test_xml_etree_c" in
6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# order to ensure consistency between the C implementation and the
7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Python implementation.
8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#
9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# For this purpose, the module-level "ET" symbol is temporarily
10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# monkey-patched when running the "test_xml_etree_c" test suite.
11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Don't re-import "xml.etree.ElementTree" module in the docstring,
12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# except if the test is specific to the Python implementation.
13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport sys
15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport cgi
16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom test import test_support
18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom test.test_support import findfile
19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom xml.etree import ElementTree as ET
21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSIMPLE_XMLFILE = findfile("simple.xml", subdir="xmltestdata")
23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSIMPLE_NS_XMLFILE = findfile("simple-ns.xml", subdir="xmltestdata")
24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSAMPLE_XML = """\
26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<body>
27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <tag class='a'>text</tag>
28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <tag class='b' />
29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <section>
30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    <tag class='b' id='inner'>subtext</tag>
31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  </section>
32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep</body>
33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""
34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSAMPLE_SECTION = """\
36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<section>
37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <tag class='b' id='inner'>subtext</tag>
38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <nexttag />
39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <nextsection>
40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    <tag />
41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  </nextsection>
42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep</section>
43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""
44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSAMPLE_XML_NS = """
46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<body xmlns="http://effbot.org/ns">
47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <tag>text</tag>
48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <tag />
49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <section>
50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    <tag>subtext</tag>
51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  </section>
52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep</body>
53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""
54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef sanity():
57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Import sanity.
59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> from xml.etree import ElementTree
61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> from xml.etree import ElementInclude
62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> from xml.etree import ElementPath
63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef check_method(method):
66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if not hasattr(method, '__call__'):
67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        print method, "not callable"
68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef serialize(elem, to_string=True, **options):
70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    import StringIO
71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    file = StringIO.StringIO()
72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    tree = ET.ElementTree(elem)
73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    tree.write(file, **options)
74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if to_string:
75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return file.getvalue()
76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    else:
77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        file.seek(0)
78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return file
79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef summarize(elem):
81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if elem.tag == ET.Comment:
82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return "<Comment>"
83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return elem.tag
84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef summarize_list(seq):
86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return [summarize(elem) for elem in seq]
87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef normalize_crlf(tree):
89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    for elem in tree.iter():
90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if elem.text:
91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            elem.text = elem.text.replace("\r\n", "\n")
92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if elem.tail:
93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            elem.tail = elem.tail.replace("\r\n", "\n")
94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef check_string(string):
96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    len(string)
97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    for char in string:
98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if len(char) != 1:
99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            print "expected one-character string, got %r" % char
100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    new_string = string + ""
101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    new_string = string + " "
102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    string[:0]
103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef check_mapping(mapping):
105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    len(mapping)
106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    keys = mapping.keys()
107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    items = mapping.items()
108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    for key in keys:
109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        item = mapping[key]
110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    mapping["key"] = "value"
111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if mapping["key"] != "value":
112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        print "expected value string, got %r" % mapping["key"]
113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef check_element(element):
115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if not ET.iselement(element):
116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        print "not an element"
117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if not hasattr(element, "tag"):
118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        print "no tag member"
119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if not hasattr(element, "attrib"):
120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        print "no attrib member"
121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if not hasattr(element, "text"):
122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        print "no text member"
123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if not hasattr(element, "tail"):
124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        print "no tail member"
125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    check_string(element.tag)
127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    check_mapping(element.attrib)
128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if element.text is not None:
129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check_string(element.text)
130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if element.tail is not None:
131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check_string(element.tail)
132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    for elem in element:
133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check_element(elem)
134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# --------------------------------------------------------------------
136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# element tree tests
137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef interface():
139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    r"""
140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test element tree interface.
141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> element = ET.Element("tag")
143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_element(element)
144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree = ET.ElementTree(element)
145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_element(tree.getroot())
146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> element = ET.Element("t\xe4g", key="value")
148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree = ET.ElementTree(element)
149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> repr(element)   # doctest: +ELLIPSIS
150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    "<Element 't\\xe4g' at 0x...>"
151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> element = ET.Element("tag", key="value")
152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Make sure all standard element methods exist.
154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(element.append)
156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(element.extend)
157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(element.insert)
158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(element.remove)
159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(element.getchildren)
160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(element.find)
161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(element.iterfind)
162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(element.findall)
163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(element.findtext)
164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(element.clear)
165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(element.get)
166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(element.set)
167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(element.keys)
168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(element.items)
169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(element.iter)
170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(element.itertext)
171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(element.getiterator)
172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    These methods return an iterable. See bug 6472.
174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(element.iter("tag").next)
176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(element.iterfind("tag").next)
177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(element.iterfind("*").next)
178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(tree.iter("tag").next)
179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(tree.iterfind("tag").next)
180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_method(tree.iterfind("*").next)
181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    These aliases are provided:
183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> assert ET.XML == ET.fromstring
185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> assert ET.PI == ET.ProcessingInstruction
186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> assert ET.XMLParser == ET.XMLTreeBuilder
187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef simpleops():
190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Basic method sanity checks.
192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.XML("<body><tag/></body>")
194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem)
195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<body><tag /></body>'
196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e = ET.Element("tag2")
197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.append(e)
198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem)
199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<body><tag /><tag2 /></body>'
200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.remove(e)
201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem)
202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<body><tag /></body>'
203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.insert(0, e)
204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem)
205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<body><tag2 /><tag /></body>'
206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.remove(e)
207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.extend([e])
208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem)
209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<body><tag /><tag2 /></body>'
210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.remove(e)
211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> element = ET.Element("tag", key="value")
213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(element) # 1
214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag key="value" />'
215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> subelement = ET.Element("subtag")
216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> element.append(subelement)
217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(element) # 2
218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag key="value"><subtag /></tag>'
219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> element.insert(0, subelement)
220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(element) # 3
221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag key="value"><subtag /><subtag /></tag>'
222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> element.remove(subelement)
223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(element) # 4
224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag key="value"><subtag /></tag>'
225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> element.remove(subelement)
226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(element) # 5
227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag key="value" />'
228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> element.remove(subelement)
229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Traceback (most recent call last):
230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ValueError: list.remove(x): x not in list
231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(element) # 6
232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag key="value" />'
233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> element[0:0] = [subelement, subelement, subelement]
234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(element[1])
235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<subtag />'
236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> element[1:9] == [element[1], element[2]]
237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    True
238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> element[:9:2] == [element[0], element[2]]
239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    True
240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> del element[1:2]
241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(element)
242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag key="value"><subtag /><subtag /></tag>'
243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef cdata():
246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test CDATA handling (etc).
248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(ET.XML("<tag>hello</tag>"))
250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag>hello</tag>'
251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(ET.XML("<tag>&#104;&#101;&#108;&#108;&#111;</tag>"))
252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag>hello</tag>'
253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(ET.XML("<tag><![CDATA[hello]]></tag>"))
254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag>hello</tag>'
255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Only with Python implementation
258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef simplefind():
259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test find methods using the elementpath fallback.
261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> from xml.etree import ElementTree
263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> CurrentElementPath = ElementTree.ElementPath
265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ElementTree.ElementPath = ElementTree._SimpleElementPath()
266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ElementTree.XML(SAMPLE_XML)
267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.find("tag").tag
268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'tag'
269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ElementTree.ElementTree(elem).find("tag").tag
270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'tag'
271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.findtext("tag")
272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'text'
273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.findtext("tog")
274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.findtext("tog", "default")
275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'default'
276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ElementTree.ElementTree(elem).findtext("tag")
277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'text'
278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("tag"))
279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag', 'tag']
280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall(".//tag"))
281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag', 'tag', 'tag']
282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Path syntax doesn't work in this case.
284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.find("section/tag")
286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.findtext("section/tag")
287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("section/tag"))
288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    []
289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ElementTree.ElementPath = CurrentElementPath
291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef find():
294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test find methods (including xpath syntax).
296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.XML(SAMPLE_XML)
298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.find("tag").tag
299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'tag'
300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.ElementTree(elem).find("tag").tag
301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'tag'
302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.find("section/tag").tag
303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'tag'
304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.find("./tag").tag
305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'tag'
306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.ElementTree(elem).find("./tag").tag
307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'tag'
308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.ElementTree(elem).find("/tag").tag
309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'tag'
310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem[2] = ET.XML(SAMPLE_SECTION)
311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.find("section/nexttag").tag
312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'nexttag'
313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.ElementTree(elem).find("section/tag").tag
314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'tag'
315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.ElementTree(elem).find("tog")
316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.ElementTree(elem).find("tog/foo")
317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.findtext("tag")
318edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'text'
319edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.findtext("section/nexttag")
320edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ''
321edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.findtext("section/nexttag", "default")
322edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ''
323edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.findtext("tog")
324edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.findtext("tog", "default")
325edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'default'
326edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.ElementTree(elem).findtext("tag")
327edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'text'
328edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.ElementTree(elem).findtext("tog/foo")
329edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.ElementTree(elem).findtext("tog/foo", "default")
330edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'default'
331edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.ElementTree(elem).findtext("./tag")
332edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'text'
333edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.ElementTree(elem).findtext("/tag")
334edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'text'
335edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.findtext("section/tag")
336edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'subtext'
337edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.ElementTree(elem).findtext("section/tag")
338edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'subtext'
339edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("."))
340edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['body']
341edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("tag"))
342edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag', 'tag']
343edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("tog"))
344edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    []
345edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("tog/foo"))
346edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    []
347edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("*"))
348edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag', 'tag', 'section']
349edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall(".//tag"))
350edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag', 'tag', 'tag', 'tag']
351edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("section/tag"))
352edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag']
353edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("section//tag"))
354edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag', 'tag']
355edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("section/*"))
356edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag', 'nexttag', 'nextsection']
357edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("section//*"))
358edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag', 'nexttag', 'nextsection', 'tag']
359edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("section/.//*"))
360edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag', 'nexttag', 'nextsection', 'tag']
361edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("*/*"))
362edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag', 'nexttag', 'nextsection']
363edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("*//*"))
364edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag', 'nexttag', 'nextsection', 'tag']
365edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("*/tag"))
366edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag']
367edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("*/./tag"))
368edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag']
369edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("./tag"))
370edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag', 'tag']
371edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall(".//tag"))
372edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag', 'tag', 'tag', 'tag']
373edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("././tag"))
374edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag', 'tag']
375edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall(".//tag[@class]"))
376edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag', 'tag', 'tag']
377edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall(".//tag[@class='a']"))
378edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag']
379edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall(".//tag[@class='b']"))
380edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag', 'tag']
381edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall(".//tag[@id]"))
382edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag']
383edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall(".//section[tag]"))
384edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['section']
385edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall(".//section[element]"))
386edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    []
387edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("../tag"))
388edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    []
389edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("section/../tag"))
390edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag', 'tag']
391edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(ET.ElementTree(elem).findall("./tag"))
392edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag', 'tag']
393edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
394edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Following example is invalid in 1.2.
395edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    A leading '*' is assumed in 1.3.
396edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
397edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.findall("section//") == elem.findall("section//*")
398edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    True
399edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
400edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ET's Path module handles this case incorrectly; this gives
401edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    a warning in 1.3, and the behaviour will be modified in 1.4.
402edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
403edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(ET.ElementTree(elem).findall("/tag"))
404edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag', 'tag']
405edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
406edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.XML(SAMPLE_XML_NS)
407edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("tag"))
408edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    []
409edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall("{http://effbot.org/ns}tag"))
410edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['{http://effbot.org/ns}tag', '{http://effbot.org/ns}tag']
411edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem.findall(".//{http://effbot.org/ns}tag"))
412edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['{http://effbot.org/ns}tag', '{http://effbot.org/ns}tag', '{http://effbot.org/ns}tag']
413edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
414edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
415edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef file_init():
416edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
417edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> import StringIO
418edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
419edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> stringfile = StringIO.StringIO(SAMPLE_XML)
420edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree = ET.ElementTree(file=stringfile)
421edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree.find("tag").tag
422edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'tag'
423edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree.find("section/tag").tag
424edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'tag'
425edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
426edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree = ET.ElementTree(file=SIMPLE_XMLFILE)
427edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree.find("element").tag
428edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'element'
429edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree.find("element/../empty-element").tag
430edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'empty-element'
431edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
432edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
433edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef bad_find():
434edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
435edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Check bad or unsupported path expressions.
436edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
437edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.XML(SAMPLE_XML)
438edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.findall("/tag")
439edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Traceback (most recent call last):
440edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    SyntaxError: cannot use absolute path on element
441edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
442edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
443edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef path_cache():
444edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
445edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Check that the path cache behaves sanely.
446edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
447edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.XML(SAMPLE_XML)
448edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> for i in range(10): ET.ElementTree(elem).find('./'+str(i))
449edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> cache_len_10 = len(ET.ElementPath._cache)
450edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> for i in range(10): ET.ElementTree(elem).find('./'+str(i))
451edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> len(ET.ElementPath._cache) == cache_len_10
452edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    True
453edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> for i in range(20): ET.ElementTree(elem).find('./'+str(i))
454edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> len(ET.ElementPath._cache) > cache_len_10
455edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    True
456edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> for i in range(600): ET.ElementTree(elem).find('./'+str(i))
457edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> len(ET.ElementPath._cache) < 500
458edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    True
459edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
460edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
461edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef copy():
462edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
463edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test copy handling (etc).
464edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
465edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> import copy
466edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e1 = ET.XML("<tag>hello<foo/></tag>")
467edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e2 = copy.copy(e1)
468edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e3 = copy.deepcopy(e1)
469edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e1.find("foo").tag = "bar"
470edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(e1)
471edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag>hello<bar /></tag>'
472edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(e2)
473edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag>hello<bar /></tag>'
474edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(e3)
475edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag>hello<foo /></tag>'
476edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
477edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
478edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
479edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef attrib():
480edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
481edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test attribute handling.
482edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
483edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.Element("tag")
484edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.get("key") # 1.1
485edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.get("key", "default") # 1.2
486edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'default'
487edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.set("key", "value")
488edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.get("key") # 1.3
489edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'value'
490edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
491edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.Element("tag", key="value")
492edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.get("key") # 2.1
493edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'value'
494edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.attrib # 2.2
495edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    {'key': 'value'}
496edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
497edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> attrib = {"key": "value"}
498edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.Element("tag", attrib)
499edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> attrib.clear() # check for aliasing issues
500edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.get("key") # 3.1
501edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'value'
502edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.attrib # 3.2
503edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    {'key': 'value'}
504edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
505edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> attrib = {"key": "value"}
506edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.Element("tag", **attrib)
507edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> attrib.clear() # check for aliasing issues
508edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.get("key") # 4.1
509edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'value'
510edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.attrib # 4.2
511edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    {'key': 'value'}
512edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
513edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.Element("tag", {"key": "other"}, key="value")
514edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.get("key") # 5.1
515edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'value'
516edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.attrib # 5.2
517edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    {'key': 'value'}
518edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
519edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.Element('test')
520edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.text = "aa"
521edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.set('testa', 'testval')
522edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.set('testb', 'test2')
523edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(elem)
524edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<test testa="testval" testb="test2">aa</test>'
525edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> sorted(elem.keys())
526edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['testa', 'testb']
527edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> sorted(elem.items())
528edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    [('testa', 'testval'), ('testb', 'test2')]
529edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.attrib['testb']
530edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'test2'
531edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.attrib['testb'] = 'test1'
532edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.attrib['testc'] = 'test2'
533edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(elem)
534edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<test testa="testval" testb="test1" testc="test2">aa</test>'
535edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
536edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
537edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef makeelement():
538edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
539edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test makeelement handling.
540edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
541edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.Element("tag")
542edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> attrib = {"key": "value"}
543edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> subelem = elem.makeelement("subtag", attrib)
544edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> if subelem.attrib is attrib:
545edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     print "attrib aliasing"
546edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.append(subelem)
547edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem)
548edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag><subtag key="value" /></tag>'
549edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
550edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.clear()
551edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem)
552edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag />'
553edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.append(subelem)
554edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem)
555edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag><subtag key="value" /></tag>'
556edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.extend([subelem, subelem])
557edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem)
558edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag><subtag key="value" /><subtag key="value" /><subtag key="value" /></tag>'
559edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem[:] = [subelem]
560edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem)
561edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag><subtag key="value" /></tag>'
562edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem[:] = tuple([subelem])
563edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem)
564edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag><subtag key="value" /></tag>'
565edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
566edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
567edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
568edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef parsefile():
569edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
570edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test parsing from file.
571edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
572edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree = ET.parse(SIMPLE_XMLFILE)
573edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> normalize_crlf(tree)
574edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree.write(sys.stdout)
575edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    <root>
576edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep       <element key="value">text</element>
577edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep       <element>text</element>tail
578edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep       <empty-element />
579edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    </root>
580edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree = ET.parse(SIMPLE_NS_XMLFILE)
581edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> normalize_crlf(tree)
582edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree.write(sys.stdout)
583edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    <ns0:root xmlns:ns0="namespace">
584edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep       <ns0:element key="value">text</ns0:element>
585edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep       <ns0:element>text</ns0:element>tail
586edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep       <ns0:empty-element />
587edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    </ns0:root>
588edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
589edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> with open(SIMPLE_XMLFILE) as f:
590edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     data = f.read()
591edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
592edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> parser = ET.XMLParser()
593edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> parser.version  # doctest: +ELLIPSIS
594edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'Expat ...'
595edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> parser.feed(data)
596edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> print serialize(parser.close())
597edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    <root>
598edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep       <element key="value">text</element>
599edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep       <element>text</element>tail
600edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep       <empty-element />
601edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    </root>
602edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
603edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> parser = ET.XMLTreeBuilder() # 1.2 compatibility
604edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> parser.feed(data)
605edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> print serialize(parser.close())
606edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    <root>
607edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep       <element key="value">text</element>
608edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep       <element>text</element>tail
609edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep       <empty-element />
610edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    </root>
611edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
612edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> target = ET.TreeBuilder()
613edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> parser = ET.XMLParser(target=target)
614edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> parser.feed(data)
615edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> print serialize(parser.close())
616edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    <root>
617edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep       <element key="value">text</element>
618edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep       <element>text</element>tail
619edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep       <empty-element />
620edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    </root>
621edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
622edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
623edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef parseliteral():
624edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
625edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> element = ET.XML("<html><body>text</body></html>")
626edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.ElementTree(element).write(sys.stdout)
627edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    <html><body>text</body></html>
628edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> element = ET.fromstring("<html><body>text</body></html>")
629edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.ElementTree(element).write(sys.stdout)
630edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    <html><body>text</body></html>
631edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> sequence = ["<html><body>", "text</bo", "dy></html>"]
632edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> element = ET.fromstringlist(sequence)
633edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> print ET.tostring(element)
634edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    <html><body>text</body></html>
635edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> print "".join(ET.tostringlist(element))
636edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    <html><body>text</body></html>
637edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(element, "ascii")
638edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    "<?xml version='1.0' encoding='ascii'?>\\n<html><body>text</body></html>"
639edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> _, ids = ET.XMLID("<html><body>text</body></html>")
640edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> len(ids)
641edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    0
642edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> _, ids = ET.XMLID("<html><body id='body'>text</body></html>")
643edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> len(ids)
644edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    1
645edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ids["body"].tag
646edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'body'
647edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
648edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
649edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef iterparse():
650edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
651edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test iterparse interface.
652edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
653edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> iterparse = ET.iterparse
654edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
655edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> context = iterparse(SIMPLE_XMLFILE)
656edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> action, elem = next(context)
657edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> print action, elem.tag
658edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end element
659edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> for action, elem in context:
660edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...   print action, elem.tag
661edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end element
662edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end empty-element
663edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end root
664edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> context.root.tag
665edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'root'
666edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
667edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> context = iterparse(SIMPLE_NS_XMLFILE)
668edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> for action, elem in context:
669edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...   print action, elem.tag
670edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end {namespace}element
671edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end {namespace}element
672edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end {namespace}empty-element
673edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end {namespace}root
674edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
675edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> events = ()
676edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> context = iterparse(SIMPLE_XMLFILE, events)
677edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> for action, elem in context:
678edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...   print action, elem.tag
679edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
680edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> events = ()
681edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> context = iterparse(SIMPLE_XMLFILE, events=events)
682edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> for action, elem in context:
683edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...   print action, elem.tag
684edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
685edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> events = ("start", "end")
686edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> context = iterparse(SIMPLE_XMLFILE, events)
687edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> for action, elem in context:
688edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...   print action, elem.tag
689edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    start root
690edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    start element
691edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end element
692edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    start element
693edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end element
694edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    start empty-element
695edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end empty-element
696edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end root
697edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
698edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> events = ("start", "end", "start-ns", "end-ns")
699edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> context = iterparse(SIMPLE_NS_XMLFILE, events)
700edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> for action, elem in context:
701edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...   if action in ("start", "end"):
702edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     print action, elem.tag
703edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...   else:
704edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     print action, elem
705edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    start-ns ('', 'namespace')
706edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    start {namespace}root
707edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    start {namespace}element
708edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end {namespace}element
709edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    start {namespace}element
710edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end {namespace}element
711edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    start {namespace}empty-element
712edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end {namespace}empty-element
713edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end {namespace}root
714edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end-ns None
715edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
716edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> events = ("start", "end", "bogus")
717edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> with open(SIMPLE_XMLFILE, "rb") as f:
718edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     iterparse(f, events)
719edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Traceback (most recent call last):
720edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ValueError: unknown event 'bogus'
721edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
722edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> import StringIO
723edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
724edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> source = StringIO.StringIO(
725edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     "<?xml version='1.0' encoding='iso-8859-1'?>\\n"
726edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     "<body xmlns='http://&#233;ffbot.org/ns'\\n"
727edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     "      xmlns:cl\\xe9='http://effbot.org/ns'>text</body>\\n")
728edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> events = ("start-ns",)
729edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> context = iterparse(source, events)
730edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> for action, elem in context:
731edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     print action, elem
732edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    start-ns ('', u'http://\\xe9ffbot.org/ns')
733edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    start-ns (u'cl\\xe9', 'http://effbot.org/ns')
734edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
735edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> source = StringIO.StringIO("<document />junk")
736edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> try:
737edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...   for action, elem in iterparse(source):
738edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     print action, elem.tag
739edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ... except ET.ParseError, v:
740edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...   print v
741edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end document
742edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    junk after document element: line 1, column 12
743edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
744edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
745edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef writefile():
746edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
747edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.Element("tag")
748edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.text = "text"
749edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem)
750edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag>text</tag>'
751edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.SubElement(elem, "subtag").text = "subtext"
752edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem)
753edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag>text<subtag>subtext</subtag></tag>'
754edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
755edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test tag suppression
756edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.tag = None
757edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem)
758edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'text<subtag>subtext</subtag>'
759edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.insert(0, ET.Comment("comment"))
760edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem)     # assumes 1.3
761edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'text<!--comment--><subtag>subtext</subtag>'
762edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem[0] = ET.PI("key", "value")
763edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem)
764edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'text<?key value?><subtag>subtext</subtag>'
765edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
766edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
767edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef custom_builder():
768edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
769edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test parser w. custom builder.
770edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
771edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> with open(SIMPLE_XMLFILE) as f:
772edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     data = f.read()
773edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> class Builder:
774edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     def start(self, tag, attrib):
775edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...         print "start", tag
776edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     def end(self, tag):
777edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...         print "end", tag
778edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     def data(self, text):
779edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...         pass
780edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> builder = Builder()
781edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> parser = ET.XMLParser(target=builder)
782edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> parser.feed(data)
783edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    start root
784edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    start element
785edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end element
786edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    start element
787edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end element
788edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    start empty-element
789edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end empty-element
790edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end root
791edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
792edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> with open(SIMPLE_NS_XMLFILE) as f:
793edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     data = f.read()
794edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> class Builder:
795edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     def start(self, tag, attrib):
796edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...         print "start", tag
797edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     def end(self, tag):
798edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...         print "end", tag
799edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     def data(self, text):
800edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...         pass
801edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     def pi(self, target, data):
802edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...         print "pi", target, repr(data)
803edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     def comment(self, data):
804edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...         print "comment", repr(data)
805edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> builder = Builder()
806edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> parser = ET.XMLParser(target=builder)
807edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> parser.feed(data)
808edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    pi pi 'data'
809edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    comment ' comment '
810edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    start {namespace}root
811edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    start {namespace}element
812edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end {namespace}element
813edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    start {namespace}element
814edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end {namespace}element
815edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    start {namespace}empty-element
816edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end {namespace}empty-element
817edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    end {namespace}root
818edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
819edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
820edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
821edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef getchildren():
822edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
823edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test Element.getchildren()
824edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
825edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> with open(SIMPLE_XMLFILE, "r") as f:
826edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     tree = ET.parse(f)
827edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> for elem in tree.getroot().iter():
828edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     summarize_list(elem.getchildren())
829edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['element', 'element', 'empty-element']
830edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    []
831edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    []
832edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    []
833edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> for elem in tree.getiterator():
834edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     summarize_list(elem.getchildren())
835edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['element', 'element', 'empty-element']
836edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    []
837edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    []
838edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    []
839edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
840edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.XML(SAMPLE_XML)
841edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> len(elem.getchildren())
842edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    3
843edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> len(elem[2].getchildren())
844edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    1
845edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem[:] == elem.getchildren()
846edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    True
847edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> child1 = elem[0]
848edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> child2 = elem[2]
849edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> del elem[1:2]
850edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> len(elem.getchildren())
851edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    2
852edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> child1 == elem[0]
853edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    True
854edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> child2 == elem[1]
855edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    True
856edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem[0:2] = [child2, child1]
857edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> child2 == elem[0]
858edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    True
859edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> child1 == elem[1]
860edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    True
861edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> child1 == elem[0]
862edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    False
863edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.clear()
864edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.getchildren()
865edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    []
866edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
867edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
868edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef writestring():
869edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
870edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.XML("<html><body>text</body></html>")
871edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(elem)
872edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<html><body>text</body></html>'
873edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.fromstring("<html><body>text</body></html>")
874edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(elem)
875edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<html><body>text</body></html>'
876edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
877edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
878edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef check_encoding(encoding):
879edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
880edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_encoding("ascii")
881edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_encoding("us-ascii")
882edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_encoding("iso-8859-1")
883edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_encoding("iso-8859-15")
884edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_encoding("cp437")
885edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> check_encoding("mac-roman")
886edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
887edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ET.XML("<?xml version='1.0' encoding='%s'?><xml />" % encoding)
888edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
889edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef encoding():
890edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    r"""
891edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test encoding issues.
892edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
893edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.Element("tag")
894edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.text = u"abc"
895edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem)
896edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag>abc</tag>'
897edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem, encoding="utf-8")
898edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag>abc</tag>'
899edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem, encoding="us-ascii")
900edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag>abc</tag>'
901edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem, encoding="iso-8859-1")
902edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    "<?xml version='1.0' encoding='iso-8859-1'?>\n<tag>abc</tag>"
903edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
904edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.text = "<&\"\'>"
905edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem)
906edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag>&lt;&amp;"\'&gt;</tag>'
907edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem, encoding="utf-8")
908edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag>&lt;&amp;"\'&gt;</tag>'
909edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem, encoding="us-ascii") # cdata characters
910edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag>&lt;&amp;"\'&gt;</tag>'
911edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem, encoding="iso-8859-1")
912edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<?xml version=\'1.0\' encoding=\'iso-8859-1\'?>\n<tag>&lt;&amp;"\'&gt;</tag>'
913edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
914edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.attrib["key"] = "<&\"\'>"
915edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.text = None
916edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem)
917edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag key="&lt;&amp;&quot;\'&gt;" />'
918edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem, encoding="utf-8")
919edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag key="&lt;&amp;&quot;\'&gt;" />'
920edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem, encoding="us-ascii")
921edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag key="&lt;&amp;&quot;\'&gt;" />'
922edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem, encoding="iso-8859-1")
923edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<?xml version=\'1.0\' encoding=\'iso-8859-1\'?>\n<tag key="&lt;&amp;&quot;\'&gt;" />'
924edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
925edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.text = u'\xe5\xf6\xf6<>'
926edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.attrib.clear()
927edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem)
928edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag>&#229;&#246;&#246;&lt;&gt;</tag>'
929edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem, encoding="utf-8")
930edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag>\xc3\xa5\xc3\xb6\xc3\xb6&lt;&gt;</tag>'
931edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem, encoding="us-ascii")
932edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag>&#229;&#246;&#246;&lt;&gt;</tag>'
933edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem, encoding="iso-8859-1")
934edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    "<?xml version='1.0' encoding='iso-8859-1'?>\n<tag>\xe5\xf6\xf6&lt;&gt;</tag>"
935edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
936edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.attrib["key"] = u'\xe5\xf6\xf6<>'
937edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.text = None
938edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem)
939edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag key="&#229;&#246;&#246;&lt;&gt;" />'
940edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem, encoding="utf-8")
941edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag key="\xc3\xa5\xc3\xb6\xc3\xb6&lt;&gt;" />'
942edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem, encoding="us-ascii")
943edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag key="&#229;&#246;&#246;&lt;&gt;" />'
944edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem, encoding="iso-8859-1")
945edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<?xml version=\'1.0\' encoding=\'iso-8859-1\'?>\n<tag key="\xe5\xf6\xf6&lt;&gt;" />'
946edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
947edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
948edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef methods():
949edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    r"""
950edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test serialization methods.
951edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
952edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e = ET.XML("<html><link/><script>1 &lt; 2</script></html>")
953edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e.tail = "\n"
954edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(e)
955edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<html><link /><script>1 &lt; 2</script></html>\n'
956edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(e, method=None)
957edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<html><link /><script>1 &lt; 2</script></html>\n'
958edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(e, method="xml")
959edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<html><link /><script>1 &lt; 2</script></html>\n'
960edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(e, method="html")
961edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<html><link><script>1 < 2</script></html>\n'
962edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(e, method="text")
963edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '1 < 2\n'
964edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
965edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
966edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef iterators():
967edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
968edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test iterators.
969edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
970edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e = ET.XML("<html><body>this is a <i>paragraph</i>.</body>..</html>")
971edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(e.iter())
972edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['html', 'body', 'i']
973edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(e.find("body").iter())
974edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['body', 'i']
975edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize(next(e.iter()))
976edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'html'
977edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> "".join(e.itertext())
978edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'this is a paragraph...'
979edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> "".join(e.find("body").itertext())
980edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'this is a paragraph.'
981edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> next(e.itertext())
982edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'this is a '
983edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
984edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Method iterparse should return an iterator. See bug 6472.
985edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
986edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> sourcefile = serialize(e, to_string=False)
987edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> next(ET.iterparse(sourcefile))  # doctest: +ELLIPSIS
988edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ('end', <Element 'i' at 0x...>)
989edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
990edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree = ET.ElementTree(None)
991edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree.iter()
992edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Traceback (most recent call last):
993edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    AttributeError: 'NoneType' object has no attribute 'iter'
994edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
995edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
996edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepENTITY_XML = """\
997edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<!DOCTYPE points [
998edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<!ENTITY % user-entities SYSTEM 'user-entities.xml'>
999edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep%user-entities;
1000edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep]>
1001edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<document>&entity;</document>
1002edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""
1003edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1004edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef entity():
1005edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1006edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test entity handling.
1007edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1008edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    1) good entities
1009edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1010edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e = ET.XML("<document title='&#x8230;'>test</document>")
1011edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(e)
1012edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<document title="&#33328;">test</document>'
1013edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1014edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    2) bad entities
1015edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1016edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.XML("<document>&entity;</document>")
1017edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Traceback (most recent call last):
1018edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ParseError: undefined entity: line 1, column 10
1019edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1020edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.XML(ENTITY_XML)
1021edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Traceback (most recent call last):
1022edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ParseError: undefined entity &entity;: line 5, column 10
1023edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1024edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    3) custom entity
1025edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1026edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> parser = ET.XMLParser()
1027edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> parser.entity["entity"] = "text"
1028edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> parser.feed(ENTITY_XML)
1029edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> root = parser.close()
1030edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(root)
1031edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<document>text</document>'
1032edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1033edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1034edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef error(xml):
1035edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1036edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1037edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test error handling.
1038edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1039edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> issubclass(ET.ParseError, SyntaxError)
1040edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    True
1041edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> error("foo").position
1042edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    (1, 0)
1043edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> error("<tag>&foo;</tag>").position
1044edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    (1, 5)
1045edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> error("foobar<").position
1046edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    (1, 6)
1047edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1048edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1049edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    try:
1050edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ET.XML(xml)
1051edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    except ET.ParseError:
1052edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return sys.exc_value
1053edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1054edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef namespace():
1055edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1056edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test namespace issues.
1057edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1058edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    1) xml namespace
1059edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1060edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.XML("<tag xml:lang='en' />")
1061edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem) # 1.1
1062edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag xml:lang="en" />'
1063edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1064edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    2) other "well-known" namespaces
1065edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1066edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.XML("<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#' />")
1067edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem) # 2.1
1068edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" />'
1069edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1070edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.XML("<html:html xmlns:html='http://www.w3.org/1999/xhtml' />")
1071edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem) # 2.2
1072edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<html:html xmlns:html="http://www.w3.org/1999/xhtml" />'
1073edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1074edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.XML("<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope' />")
1075edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem) # 2.3
1076edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<ns0:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope" />'
1077edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1078edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    3) unknown namespaces
1079edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.XML(SAMPLE_XML_NS)
1080edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> print serialize(elem)
1081edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    <ns0:body xmlns:ns0="http://effbot.org/ns">
1082edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep      <ns0:tag>text</ns0:tag>
1083edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep      <ns0:tag />
1084edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep      <ns0:section>
1085edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        <ns0:tag>subtext</ns0:tag>
1086edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep      </ns0:section>
1087edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    </ns0:body>
1088edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1089edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1090edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef qname():
1091edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1092edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test QName handling.
1093edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1094edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    1) decorated tags
1095edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1096edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.Element("{uri}tag")
1097edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem) # 1.1
1098edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<ns0:tag xmlns:ns0="uri" />'
1099edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.Element(ET.QName("{uri}tag"))
1100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem) # 1.2
1101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<ns0:tag xmlns:ns0="uri" />'
1102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.Element(ET.QName("uri", "tag"))
1103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem) # 1.3
1104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<ns0:tag xmlns:ns0="uri" />'
1105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.Element(ET.QName("uri", "tag"))
1106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> subelem = ET.SubElement(elem, ET.QName("uri", "tag1"))
1107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> subelem = ET.SubElement(elem, ET.QName("uri", "tag2"))
1108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem) # 1.4
1109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<ns0:tag xmlns:ns0="uri"><ns0:tag1 /><ns0:tag2 /></ns0:tag>'
1110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    2) decorated attributes
1112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.clear()
1114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.attrib["{uri}key"] = "value"
1115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem) # 2.1
1116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<ns0:tag xmlns:ns0="uri" ns0:key="value" />'
1117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.clear()
1119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.attrib[ET.QName("{uri}key")] = "value"
1120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem) # 2.2
1121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<ns0:tag xmlns:ns0="uri" ns0:key="value" />'
1122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    3) decorated values are not converted by default, but the
1124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep       QName wrapper can be used for values
1125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.clear()
1127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.attrib["{uri}key"] = "{uri}value"
1128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem) # 3.1
1129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<ns0:tag xmlns:ns0="uri" ns0:key="{uri}value" />'
1130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.clear()
1132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.attrib["{uri}key"] = ET.QName("{uri}value")
1133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem) # 3.2
1134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<ns0:tag xmlns:ns0="uri" ns0:key="ns0:value" />'
1135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.clear()
1137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> subelem = ET.Element("tag")
1138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> subelem.attrib["{uri1}key"] = ET.QName("{uri2}value")
1139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.append(subelem)
1140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.append(subelem)
1141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem) # 3.3
1142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<ns0:tag xmlns:ns0="uri" xmlns:ns1="uri1" xmlns:ns2="uri2"><tag ns1:key="ns2:value" /><tag ns1:key="ns2:value" /></ns0:tag>'
1143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    4) Direct QName tests
1145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> str(ET.QName('ns', 'tag'))
1147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '{ns}tag'
1148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> str(ET.QName('{ns}tag'))
1149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '{ns}tag'
1150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> q1 = ET.QName('ns', 'tag')
1151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> q2 = ET.QName('ns', 'tag')
1152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> q1 == q2
1153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    True
1154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> q2 = ET.QName('ns', 'other-tag')
1155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> q1 == q2
1156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    False
1157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> q1 == 'ns:tag'
1158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    False
1159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> q1 == '{ns}tag'
1160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    True
1161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef doctype_public():
1164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test PUBLIC doctype.
1166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.XML('<!DOCTYPE html PUBLIC'
1168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...   ' "-//W3C//DTD XHTML 1.0 Transitional//EN"'
1169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...   ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
1170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...   '<html>text</html>')
1171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef xpath_tokenizer(p):
1175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test the XPath tokenizer.
1177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> # tests from the xml specification
1179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xpath_tokenizer("*")
1180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['*']
1181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xpath_tokenizer("text()")
1182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['text', '()']
1183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xpath_tokenizer("@name")
1184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['@', 'name']
1185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xpath_tokenizer("@*")
1186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['@', '*']
1187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xpath_tokenizer("para[1]")
1188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['para', '[', '1', ']']
1189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xpath_tokenizer("para[last()]")
1190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['para', '[', 'last', '()', ']']
1191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xpath_tokenizer("*/para")
1192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['*', '/', 'para']
1193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xpath_tokenizer("/doc/chapter[5]/section[2]")
1194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['/', 'doc', '/', 'chapter', '[', '5', ']', '/', 'section', '[', '2', ']']
1195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xpath_tokenizer("chapter//para")
1196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['chapter', '//', 'para']
1197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xpath_tokenizer("//para")
1198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['//', 'para']
1199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xpath_tokenizer("//olist/item")
1200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['//', 'olist', '/', 'item']
1201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xpath_tokenizer(".")
1202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['.']
1203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xpath_tokenizer(".//para")
1204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['.', '//', 'para']
1205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xpath_tokenizer("..")
1206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['..']
1207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xpath_tokenizer("../@lang")
1208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['..', '/', '@', 'lang']
1209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xpath_tokenizer("chapter[title]")
1210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['chapter', '[', 'title', ']']
1211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xpath_tokenizer("employee[@secretary and @assistant]")
1212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['employee', '[', '@', 'secretary', '', 'and', '', '@', 'assistant', ']']
1213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> # additional tests
1215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xpath_tokenizer("{http://spam}egg")
1216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['{http://spam}egg']
1217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xpath_tokenizer("./spam.egg")
1218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['.', '/', 'spam.egg']
1219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xpath_tokenizer(".//{http://spam}egg")
1220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['.', '//', '{http://spam}egg']
1221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    from xml.etree import ElementPath
1223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    out = []
1224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    for op, tag in ElementPath.xpath_tokenizer(p):
1225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        out.append(op or tag)
1226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return out
1227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef processinginstruction():
1229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test ProcessingInstruction directly
1231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(ET.ProcessingInstruction('test', 'instruction'))
1233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<?test instruction?>'
1234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(ET.PI('test', 'instruction'))
1235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<?test instruction?>'
1236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Issue #2746
1238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(ET.PI('test', '<testing&>'))
1240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<?test <testing&>?>'
1241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(ET.PI('test', u'<testing&>\xe3'), 'latin1')
1242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    "<?xml version='1.0' encoding='latin1'?>\\n<?test <testing&>\\xe3?>"
1243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#
1246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# xinclude tests (samples from appendix C of the xinclude specification)
1247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepXINCLUDE = {}
1249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepXINCLUDE["C1.xml"] = """\
1251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<?xml version='1.0'?>
1252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<document xmlns:xi="http://www.w3.org/2001/XInclude">
1253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <p>120 Mz is adequate for an average home user.</p>
1254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <xi:include href="disclaimer.xml"/>
1255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep</document>
1256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""
1257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepXINCLUDE["disclaimer.xml"] = """\
1259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<?xml version='1.0'?>
1260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<disclaimer>
1261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <p>The opinions represented herein represent those of the individual
1262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  and should not be interpreted as official policy endorsed by this
1263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  organization.</p>
1264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep</disclaimer>
1265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""
1266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepXINCLUDE["C2.xml"] = """\
1268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<?xml version='1.0'?>
1269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<document xmlns:xi="http://www.w3.org/2001/XInclude">
1270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <p>This document has been accessed
1271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <xi:include href="count.txt" parse="text"/> times.</p>
1272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep</document>
1273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""
1274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepXINCLUDE["count.txt"] = "324387"
1276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepXINCLUDE["C2b.xml"] = """\
1278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<?xml version='1.0'?>
1279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<document xmlns:xi="http://www.w3.org/2001/XInclude">
1280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <p>This document has been <em>accessed</em>
1281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <xi:include href="count.txt" parse="text"/> times.</p>
1282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep</document>
1283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""
1284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepXINCLUDE["C3.xml"] = """\
1286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<?xml version='1.0'?>
1287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<document xmlns:xi="http://www.w3.org/2001/XInclude">
1288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <p>The following is the source of the "data.xml" resource:</p>
1289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <example><xi:include href="data.xml" parse="text"/></example>
1290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep</document>
1291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""
1292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepXINCLUDE["data.xml"] = """\
1294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<?xml version='1.0'?>
1295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<data>
1296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <item><![CDATA[Brooks & Shields]]></item>
1297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep</data>
1298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""
1299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepXINCLUDE["C5.xml"] = """\
1301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<?xml version='1.0'?>
1302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<div xmlns:xi="http://www.w3.org/2001/XInclude">
1303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <xi:include href="example.txt" parse="text">
1304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    <xi:fallback>
1305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep      <xi:include href="fallback-example.txt" parse="text">
1306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        <xi:fallback><a href="mailto:bob@example.org">Report error</a></xi:fallback>
1307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep      </xi:include>
1308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    </xi:fallback>
1309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  </xi:include>
1310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep</div>
1311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""
1312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepXINCLUDE["default.xml"] = """\
1314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<?xml version='1.0'?>
1315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<document xmlns:xi="http://www.w3.org/2001/XInclude">
1316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <p>Example.</p>
1317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <xi:include href="{}"/>
1318edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep</document>
1319edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep""".format(cgi.escape(SIMPLE_XMLFILE, True))
1320edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1321edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef xinclude_loader(href, parse="xml", encoding=None):
1322edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    try:
1323edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        data = XINCLUDE[href]
1324edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    except KeyError:
1325edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        raise IOError("resource not found")
1326edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if parse == "xml":
1327edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        from xml.etree.ElementTree import XML
1328edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return XML(data)
1329edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return data
1330edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1331edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef xinclude():
1332edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    r"""
1333edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Basic inclusion example (XInclude C.1)
1334edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1335edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> from xml.etree import ElementTree as ET
1336edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> from xml.etree import ElementInclude
1337edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1338edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> document = xinclude_loader("C1.xml")
1339edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ElementInclude.include(document, xinclude_loader)
1340edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> print serialize(document) # C1
1341edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    <document>
1342edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep      <p>120 Mz is adequate for an average home user.</p>
1343edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep      <disclaimer>
1344edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep      <p>The opinions represented herein represent those of the individual
1345edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep      and should not be interpreted as official policy endorsed by this
1346edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep      organization.</p>
1347edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    </disclaimer>
1348edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    </document>
1349edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1350edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Textual inclusion example (XInclude C.2)
1351edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1352edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> document = xinclude_loader("C2.xml")
1353edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ElementInclude.include(document, xinclude_loader)
1354edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> print serialize(document) # C2
1355edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    <document>
1356edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep      <p>This document has been accessed
1357edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep      324387 times.</p>
1358edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    </document>
1359edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1360edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Textual inclusion after sibling element (based on modified XInclude C.2)
1361edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1362edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> document = xinclude_loader("C2b.xml")
1363edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ElementInclude.include(document, xinclude_loader)
1364edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> print(serialize(document)) # C2b
1365edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    <document>
1366edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep      <p>This document has been <em>accessed</em>
1367edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep      324387 times.</p>
1368edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    </document>
1369edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1370edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Textual inclusion of XML example (XInclude C.3)
1371edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1372edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> document = xinclude_loader("C3.xml")
1373edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ElementInclude.include(document, xinclude_loader)
1374edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> print serialize(document) # C3
1375edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    <document>
1376edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep      <p>The following is the source of the "data.xml" resource:</p>
1377edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep      <example>&lt;?xml version='1.0'?&gt;
1378edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    &lt;data&gt;
1379edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep      &lt;item&gt;&lt;![CDATA[Brooks &amp; Shields]]&gt;&lt;/item&gt;
1380edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    &lt;/data&gt;
1381edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    </example>
1382edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    </document>
1383edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1384edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Fallback example (XInclude C.5)
1385edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Note! Fallback support is not yet implemented
1386edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1387edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> document = xinclude_loader("C5.xml")
1388edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ElementInclude.include(document, xinclude_loader)
1389edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Traceback (most recent call last):
1390edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    IOError: resource not found
1391edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> # print serialize(document) # C5
1392edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1393edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1394edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef xinclude_default():
1395edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1396edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> from xml.etree import ElementInclude
1397edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1398edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> document = xinclude_loader("default.xml")
1399edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ElementInclude.include(document)
1400edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> print serialize(document) # default
1401edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    <document>
1402edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep      <p>Example.</p>
1403edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep      <root>
1404edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep       <element key="value">text</element>
1405edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep       <element>text</element>tail
1406edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep       <empty-element />
1407edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    </root>
1408edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    </document>
1409edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1410edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1411edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#
1412edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# badly formatted xi:include tags
1413edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1414edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepXINCLUDE_BAD = {}
1415edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1416edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepXINCLUDE_BAD["B1.xml"] = """\
1417edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<?xml version='1.0'?>
1418edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<document xmlns:xi="http://www.w3.org/2001/XInclude">
1419edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <p>120 Mz is adequate for an average home user.</p>
1420edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  <xi:include href="disclaimer.xml" parse="BAD_TYPE"/>
1421edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep</document>
1422edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""
1423edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1424edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepXINCLUDE_BAD["B2.xml"] = """\
1425edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<?xml version='1.0'?>
1426edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<div xmlns:xi="http://www.w3.org/2001/XInclude">
1427edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    <xi:fallback></xi:fallback>
1428edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep</div>
1429edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""
1430edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1431edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef xinclude_failures():
1432edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    r"""
1433edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test failure to locate included XML file.
1434edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1435edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> from xml.etree import ElementInclude
1436edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1437edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> def none_loader(href, parser, encoding=None):
1438edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     return None
1439edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1440edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> document = ET.XML(XINCLUDE["C1.xml"])
1441edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ElementInclude.include(document, loader=none_loader)
1442edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Traceback (most recent call last):
1443edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    FatalIncludeError: cannot load 'disclaimer.xml' as 'xml'
1444edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1445edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test failure to locate included text file.
1446edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1447edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> document = ET.XML(XINCLUDE["C2.xml"])
1448edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ElementInclude.include(document, loader=none_loader)
1449edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Traceback (most recent call last):
1450edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    FatalIncludeError: cannot load 'count.txt' as 'text'
1451edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1452edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test bad parse type.
1453edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1454edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> document = ET.XML(XINCLUDE_BAD["B1.xml"])
1455edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ElementInclude.include(document, loader=none_loader)
1456edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Traceback (most recent call last):
1457edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    FatalIncludeError: unknown parse type in xi:include tag ('BAD_TYPE')
1458edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1459edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test xi:fallback outside xi:include.
1460edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1461edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> document = ET.XML(XINCLUDE_BAD["B2.xml"])
1462edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ElementInclude.include(document, loader=none_loader)
1463edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Traceback (most recent call last):
1464edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    FatalIncludeError: xi:fallback tag must be child of xi:include ('{http://www.w3.org/2001/XInclude}fallback')
1465edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1466edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1467edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# --------------------------------------------------------------------
1468edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# reported bugs
1469edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1470edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef bug_xmltoolkit21():
1471edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1472edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1473edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    marshaller gives obscure errors for non-string values
1474edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1475edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.Element(123)
1476edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem) # tag
1477edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Traceback (most recent call last):
1478edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    TypeError: cannot serialize 123 (type int)
1479edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.Element("elem")
1480edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.text = 123
1481edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem) # text
1482edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Traceback (most recent call last):
1483edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    TypeError: cannot serialize 123 (type int)
1484edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.Element("elem")
1485edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.tail = 123
1486edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem) # tail
1487edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Traceback (most recent call last):
1488edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    TypeError: cannot serialize 123 (type int)
1489edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.Element("elem")
1490edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.set(123, "123")
1491edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem) # attribute key
1492edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Traceback (most recent call last):
1493edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    TypeError: cannot serialize 123 (type int)
1494edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.Element("elem")
1495edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem.set("123", 123)
1496edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(elem) # attribute value
1497edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Traceback (most recent call last):
1498edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    TypeError: cannot serialize 123 (type int)
1499edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1500edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1501edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1502edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef bug_xmltoolkit25():
1503edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1504edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1505edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    typo in ElementTree.findtext
1506edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1507edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.XML(SAMPLE_XML)
1508edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree = ET.ElementTree(elem)
1509edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree.findtext("tag")
1510edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'text'
1511edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree.findtext("section/tag")
1512edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'subtext'
1513edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1514edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1515edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1516edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef bug_xmltoolkit28():
1517edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1518edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1519edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    .//tag causes exceptions
1520edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1521edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree = ET.XML("<doc><table><tbody/></table></doc>")
1522edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(tree.findall(".//thead"))
1523edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    []
1524edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(tree.findall(".//tbody"))
1525edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tbody']
1526edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1527edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1528edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1529edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef bug_xmltoolkitX1():
1530edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1531edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1532edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    dump() doesn't flush the output buffer
1533edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1534edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree = ET.XML("<doc><table><tbody/></table></doc>")
1535edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.dump(tree); sys.stdout.write("tail")
1536edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    <doc><table><tbody /></table></doc>
1537edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    tail
1538edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1539edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1540edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1541edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef bug_xmltoolkit39():
1542edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1543edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1544edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    non-ascii element and attribute names doesn't work
1545edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1546edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree = ET.XML("<?xml version='1.0' encoding='iso-8859-1'?><t\xe4g />")
1547edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(tree, "utf-8")
1548edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<t\\xc3\\xa4g />'
1549edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1550edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree = ET.XML("<?xml version='1.0' encoding='iso-8859-1'?><tag \xe4ttr='v&#228;lue' />")
1551edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree.attrib
1552edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    {u'\\xe4ttr': u'v\\xe4lue'}
1553edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(tree, "utf-8")
1554edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag \\xc3\\xa4ttr="v\\xc3\\xa4lue" />'
1555edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1556edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree = ET.XML("<?xml version='1.0' encoding='iso-8859-1'?><t\xe4g>text</t\xe4g>")
1557edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(tree, "utf-8")
1558edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<t\\xc3\\xa4g>text</t\\xc3\\xa4g>'
1559edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1560edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree = ET.Element(u"t\u00e4g")
1561edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(tree, "utf-8")
1562edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<t\\xc3\\xa4g />'
1563edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1564edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree = ET.Element("tag")
1565edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree.set(u"\u00e4ttr", u"v\u00e4lue")
1566edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(tree, "utf-8")
1567edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag \\xc3\\xa4ttr="v\\xc3\\xa4lue" />'
1568edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1569edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1570edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1571edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef bug_xmltoolkit54():
1572edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1573edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1574edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    problems handling internally defined entities
1575edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1576edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e = ET.XML("<!DOCTYPE doc [<!ENTITY ldots '&#x8230;'>]><doc>&ldots;</doc>")
1577edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(e)
1578edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<doc>&#33328;</doc>'
1579edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1580edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1581edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1582edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef bug_xmltoolkit55():
1583edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1584edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1585edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    make sure we're reporting the first error, not the last
1586edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1587edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e = ET.XML("<!DOCTYPE doc SYSTEM 'doc.dtd'><doc>&ldots;&ndots;&rdots;</doc>")
1588edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Traceback (most recent call last):
1589edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ParseError: undefined entity &ldots;: line 1, column 36
1590edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1591edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1592edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1593edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass ExceptionFile:
1594edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def read(self, x):
1595edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        raise IOError
1596edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1597edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef xmltoolkit60():
1598edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1599edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1600edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Handle crash in stream source.
1601edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> tree = ET.parse(ExceptionFile())
1602edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Traceback (most recent call last):
1603edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    IOError
1604edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1605edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1606edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1607edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepXMLTOOLKIT62_DOC = """<?xml version="1.0" encoding="UTF-8"?>
1608edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<!DOCTYPE patent-application-publication SYSTEM "pap-v15-2001-01-31.dtd" []>
1609edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<patent-application-publication>
1610edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<subdoc-abstract>
1611edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep<paragraph id="A-0001" lvl="0">A new cultivar of Begonia plant named &lsquo;BCT9801BEG&rsquo;.</paragraph>
1612edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep</subdoc-abstract>
1613edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep</patent-application-publication>"""
1614edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1615edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1616edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef xmltoolkit62():
1617edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1618edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1619edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Don't crash when using custom entities.
1620edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1621edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xmltoolkit62()
1622edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    u'A new cultivar of Begonia plant named \u2018BCT9801BEG\u2019.'
1623edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1624edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1625edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ENTITIES = {u'rsquo': u'\u2019', u'lsquo': u'\u2018'}
1626edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    parser = ET.XMLTreeBuilder()
1627edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    parser.entity.update(ENTITIES)
1628edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    parser.feed(XMLTOOLKIT62_DOC)
1629edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    t = parser.close()
1630edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return t.find('.//paragraph').text
1631edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1632edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef xmltoolkit63():
1633edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1634edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1635edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Check reference leak.
1636edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> xmltoolkit63()
1637edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> count = sys.getrefcount(None)
1638edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> for i in range(1000):
1639edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     xmltoolkit63()
1640edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> sys.getrefcount(None) - count
1641edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    0
1642edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1643edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1644edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    tree = ET.TreeBuilder()
1645edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    tree.start("tag", {})
1646edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    tree.data("text")
1647edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    tree.end("tag")
1648edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1649edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# --------------------------------------------------------------------
1650edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1651edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1652edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef bug_200708_newline():
1653edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    r"""
1654edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1655edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Preserve newlines in attributes.
1656edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1657edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e = ET.Element('SomeTag', text="def _f():\n  return 3\n")
1658edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(e)
1659edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<SomeTag text="def _f():&#10;  return 3&#10;" />'
1660edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.XML(ET.tostring(e)).get("text")
1661edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'def _f():\n  return 3\n'
1662edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(ET.XML(ET.tostring(e)))
1663edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<SomeTag text="def _f():&#10;  return 3&#10;" />'
1664edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1665edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1666edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1667edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef bug_200708_close():
1668edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1669edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1670edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test default builder.
1671edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> parser = ET.XMLParser() # default
1672edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> parser.feed("<element>some text</element>")
1673edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize(parser.close())
1674edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'element'
1675edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1676edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Test custom builder.
1677edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> class EchoTarget:
1678edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...     def close(self):
1679edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...         return ET.Element("element") # simulate root
1680edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> parser = ET.XMLParser(EchoTarget())
1681edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> parser.feed("<element>some text</element>")
1682edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize(parser.close())
1683edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    'element'
1684edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1685edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1686edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1687edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef bug_200709_default_namespace():
1688edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1689edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1690edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e = ET.Element("{default}elem")
1691edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> s = ET.SubElement(e, "{default}elem")
1692edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(e, default_namespace="default") # 1
1693edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<elem xmlns="default"><elem /></elem>'
1694edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1695edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e = ET.Element("{default}elem")
1696edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> s = ET.SubElement(e, "{default}elem")
1697edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> s = ET.SubElement(e, "{not-default}elem")
1698edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(e, default_namespace="default") # 2
1699edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<elem xmlns="default" xmlns:ns1="not-default"><elem /><ns1:elem /></elem>'
1700edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1701edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e = ET.Element("{default}elem")
1702edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> s = ET.SubElement(e, "{default}elem")
1703edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> s = ET.SubElement(e, "elem") # unprefixed name
1704edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(e, default_namespace="default") # 3
1705edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Traceback (most recent call last):
1706edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ValueError: cannot use non-qualified names with default_namespace option
1707edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1708edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1709edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1710edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef bug_200709_register_namespace():
1711edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1712edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1713edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(ET.Element("{http://namespace.invalid/does/not/exist/}title"))
1714edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<ns0:title xmlns:ns0="http://namespace.invalid/does/not/exist/" />'
1715edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.register_namespace("foo", "http://namespace.invalid/does/not/exist/")
1716edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(ET.Element("{http://namespace.invalid/does/not/exist/}title"))
1717edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<foo:title xmlns:foo="http://namespace.invalid/does/not/exist/" />'
1718edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1719edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    And the Dublin Core namespace is in the default list:
1720edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1721edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(ET.Element("{http://purl.org/dc/elements/1.1/}title"))
1722edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<dc:title xmlns:dc="http://purl.org/dc/elements/1.1/" />'
1723edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1724edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1725edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1726edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef bug_200709_element_comment():
1727edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1728edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1729edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Not sure if this can be fixed, really (since the serializer needs
1730edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ET.Comment, not cET.comment).
1731edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1732edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> a = ET.Element('a')
1733edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> a.append(ET.Comment('foo'))
1734edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> a[0].tag == ET.Comment
1735edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    True
1736edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1737edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> a = ET.Element('a')
1738edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> a.append(ET.PI('foo'))
1739edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> a[0].tag == ET.PI
1740edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    True
1741edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1742edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1743edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1744edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef bug_200709_element_insert():
1745edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1746edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1747edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> a = ET.Element('a')
1748edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> b = ET.SubElement(a, 'b')
1749edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> c = ET.SubElement(a, 'c')
1750edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> d = ET.Element('d')
1751edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> a.insert(0, d)
1752edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(a)
1753edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['d', 'b', 'c']
1754edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> a.insert(-1, d)
1755edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(a)
1756edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['d', 'b', 'd', 'c']
1757edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1758edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1759edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1760edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef bug_200709_iter_comment():
1761edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1762edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1763edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> a = ET.Element('a')
1764edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> b = ET.SubElement(a, 'b')
1765edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> comment_b = ET.Comment("TEST-b")
1766edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> b.append(comment_b)
1767edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(a.iter(ET.Comment))
1768edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['<Comment>']
1769edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1770edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1771edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1772edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# --------------------------------------------------------------------
1773edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# reported on bugs.python.org
1774edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1775edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef bug_1534630():
1776edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1777edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1778edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> bob = ET.TreeBuilder()
1779edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e = bob.data("data")
1780edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e = bob.start("tag", {})
1781edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e = bob.end("tag")
1782edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e = bob.close()
1783edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(e)
1784edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<tag />'
1785edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1786edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1787edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1788edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef check_issue6233():
1789edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1790edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1791edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e = ET.XML("<?xml version='1.0' encoding='utf-8'?><body>t\\xc3\\xa3g</body>")
1792edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(e, 'ascii')
1793edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    "<?xml version='1.0' encoding='ascii'?>\\n<body>t&#227;g</body>"
1794edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e = ET.XML("<?xml version='1.0' encoding='iso-8859-1'?><body>t\\xe3g</body>")
1795edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(e, 'ascii')
1796edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    "<?xml version='1.0' encoding='ascii'?>\\n<body>t&#227;g</body>"
1797edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1798edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1799edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1800edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef check_issue3151():
1801edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1802edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1803edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e = ET.XML('<prefix:localname xmlns:prefix="${stuff}"/>')
1804edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> e.tag
1805edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '{${stuff}}localname'
1806edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> t = ET.ElementTree(e)
1807edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> ET.tostring(e)
1808edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<ns0:localname xmlns:ns0="${stuff}" />'
1809edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1810edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1811edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1812edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef check_issue6565():
1813edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1814edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1815edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem = ET.XML("<body><tag/></body>")
1816edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem)
1817edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag']
1818edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> newelem = ET.XML(SAMPLE_XML)
1819edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elem[:] = newelem[:]
1820edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> summarize_list(elem)
1821edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ['tag', 'tag', 'section']
1822edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1823edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1824edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1825edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef check_html_empty_elems_serialization(self):
1826edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # issue 15970
1827edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # from http://www.w3.org/TR/html401/index/elements.html
1828edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1829edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1830edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> empty_elems = ['AREA', 'BASE', 'BASEFONT', 'BR', 'COL', 'FRAME', 'HR',
1831edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ...                'IMG', 'INPUT', 'ISINDEX', 'LINK', 'META', 'PARAM']
1832edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elems = ''.join('<%s />' % elem for elem in empty_elems)
1833edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(ET.XML('<html>%s</html>' % elems), method='html')
1834edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<html><AREA><BASE><BASEFONT><BR><COL><FRAME><HR><IMG><INPUT><ISINDEX><LINK><META><PARAM></html>'
1835edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(ET.XML('<html>%s</html>' % elems.lower()), method='html')
1836edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<html><area><base><basefont><br><col><frame><hr><img><input><isindex><link><meta><param></html>'
1837edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> elems = ''.join('<%s></%s>' % (elem, elem) for elem in empty_elems)
1838edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(ET.XML('<html>%s</html>' % elems), method='html')
1839edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<html><AREA><BASE><BASEFONT><BR><COL><FRAME><HR><IMG><INPUT><ISINDEX><LINK><META><PARAM></html>'
1840edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    >>> serialize(ET.XML('<html>%s</html>' % elems.lower()), method='html')
1841edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    '<html><area><base><basefont><br><col><frame><hr><img><input><isindex><link><meta><param></html>'
1842edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1843edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
1844edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1845edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# --------------------------------------------------------------------
1846edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1847edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1848edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass CleanContext(object):
1849edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """Provide default namespace mapping and path cache."""
1850edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    checkwarnings = None
1851edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1852edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __init__(self, quiet=False):
1853edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if sys.flags.optimize >= 2:
1854edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # under -OO, doctests cannot be run and therefore not all warnings
1855edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # will be emitted
1856edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            quiet = True
1857edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        deprecations = (
1858edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Search behaviour is broken if search path starts with "/".
1859edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ("This search is broken in 1.3 and earlier, and will be fixed "
1860edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep             "in a future version.  If you rely on the current behaviour, "
1861edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep             "change it to '.+'", FutureWarning),
1862edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Element.getchildren() and Element.getiterator() are deprecated.
1863edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ("This method will be removed in future versions.  "
1864edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep             "Use .+ instead.", DeprecationWarning),
1865edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ("This method will be removed in future versions.  "
1866edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep             "Use .+ instead.", PendingDeprecationWarning),
1867edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # XMLParser.doctype() is deprecated.
1868edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ("This method of XMLParser is deprecated.  Define doctype.. "
1869edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep             "method on the TreeBuilder target.", DeprecationWarning))
1870edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.checkwarnings = test_support.check_warnings(*deprecations,
1871edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                                         quiet=quiet)
1872edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1873edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __enter__(self):
1874edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        from xml.etree import ElementTree
1875edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._nsmap = ElementTree._namespace_map
1876edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._path_cache = ElementTree.ElementPath._cache
1877edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Copy the default namespace mapping
1878edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ElementTree._namespace_map = self._nsmap.copy()
1879edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Copy the path cache (should be empty)
1880edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ElementTree.ElementPath._cache = self._path_cache.copy()
1881edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.checkwarnings.__enter__()
1882edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1883edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __exit__(self, *args):
1884edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        from xml.etree import ElementTree
1885edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Restore mapping and path cache
1886edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ElementTree._namespace_map = self._nsmap
1887edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ElementTree.ElementPath._cache = self._path_cache
1888edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.checkwarnings.__exit__(*args)
1889edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1890edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1891edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef test_main(module_name='xml.etree.ElementTree'):
1892edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    from test import test_xml_etree
1893edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1894edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    use_py_module = (module_name == 'xml.etree.ElementTree')
1895edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1896edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # The same doctests are used for both the Python and the C implementations
1897edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    assert test_xml_etree.ET.__name__ == module_name
1898edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1899edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # XXX the C module should give the same warnings as the Python module
1900edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    with CleanContext(quiet=not use_py_module):
1901edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test_support.run_doctest(test_xml_etree, verbosity=True)
1902edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1903edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # The module should not be changed by the tests
1904edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    assert test_xml_etree.ET.__name__ == module_name
1905edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1906edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif __name__ == '__main__':
1907edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    test_main()
1908