90""" 91 Test find methods (including xpath syntax). 92 Originally copied from 'selftest.py'. 93 """ 94elem=etree.XML(""" 95 <body> 96 <tag class='a'>text</tag> 97 <tag class='b' /> 98 <section> 99 <tag class='b' id='inner'>subtext</tag>100 </section>101 </body>102 """)103104self.assertEqual(elem.find("tag").tag,105'tag')106self.assertEqual(etree.ElementTree(elem).find("tag").tag,107'tag')108self.assertEqual(elem.find("section/tag").tag,109'tag')110self.assertEqual(etree.ElementTree(elem).find("section/tag").tag,111'tag')112113self.assertEqual(elem.findtext("tag"),114'text')115self.assertEqual(elem.findtext("tog"),116None)117self.assertEqual(elem.findtext("tog","default"),118'default')119self.assertEqual(etree.ElementTree(elem).findtext("tag"),120'text')121self.assertEqual(elem.findtext("section/tag"),122'subtext')123self.assertEqual(etree.ElementTree(elem).findtext("section/tag"),124'subtext')125126self.assertEqual(summarize_list(elem.findall("tag")),127['tag','tag'])128self.assertEqual(summarize_list(elem.findall("*")),129['tag','tag','section'])130self.assertEqual(summarize_list(elem.findall(".//tag")),131['tag','tag','tag'])132self.assertEqual(summarize_list(elem.findall("section/tag")),133['tag'])134self.assertEqual(summarize_list(elem.findall("section//tag")),135['tag'])136137self.assertEqual(summarize_list(elem.findall("section/*")),138['tag'])139self.assertEqual(summarize_list(elem.findall("section//*")),140['tag'])141self.assertEqual(summarize_list(elem.findall("section/.//*")),142['tag'])143self.assertEqual(summarize_list(elem.findall("*/*")),144['tag'])145self.assertEqual(summarize_list(elem.findall("*//*")),146['tag'])147self.assertEqual(summarize_list(elem.findall("*/tag")),148['tag'])149self.assertEqual(summarize_list(elem.findall("*/./tag")),150['tag'])151self.assertEqual(summarize_list(elem.findall("./tag")),152['tag','tag'])153self.assertEqual(summarize_list(elem.findall(".//tag")),154['tag','tag','tag'])155self.assertEqual(summarize_list(elem.findall("././tag")),156['tag','tag'])157158self.assertEqual(summarize_list(elem.findall(".//tag[@class]")),159['tag','tag','tag'])160self.assertEqual(summarize_list(elem.findall(".//tag[ @class]")),161['tag','tag','tag'])162self.assertEqual(summarize_list(elem.findall(".//tag[@class ]")),163['tag','tag','tag'])164self.assertEqual(summarize_list(elem.findall(".//tag[ @class ]")),165['tag','tag','tag'])166self.assertEqual(summarize_list(elem.findall(".//tag[@class='a']")),167['tag'])168self.assertEqual(summarize_list(elem.findall('.//tag[@class="a"]')),169['tag'])170self.assertEqual(summarize_list(elem.findall(".//tag[@class='b']")),171['tag','tag'])172self.assertEqual(summarize_list(elem.findall('.//tag[@class="b"]')),173['tag','tag'])174self.assertEqual(summarize_list(elem.findall('.//tag[@class = "b"]')),175['tag','tag'])176self.assertEqual(summarize_list(elem.findall(".//tag[@id]")),177['tag'])178self.assertEqual(summarize_list(elem.findall(".//tag[@class][@id]")),179['tag'])180self.assertEqual(summarize_list(elem.findall(".//section[tag]")),181['section'])182self.assertEqual(summarize_list(elem.findall(".//section[element]")),183[])184185self.assertEqual(summarize_list(elem.findall(".//section[tag='subtext']")),186['section'])187self.assertEqual(summarize_list(elem.findall(".//section[tag ='subtext']")),188['section'])189self.assertEqual(summarize_list(elem.findall(".//section[tag= 'subtext']")),190['section'])191self.assertEqual(summarize_list(elem.findall(".//section[tag = 'subtext']")),192['section'])193self.assertEqual(summarize_list(elem.findall(".//section[ tag = 'subtext' ]")),194['section'])195self.assertEqual(summarize_list(elem.findall(".//tag[.='subtext']")),196['tag'])197self.assertEqual(summarize_list(elem.findall(".//tag[. ='subtext']")),198['tag'])199self.assertEqual(summarize_list(elem.findall('.//tag[.= "subtext"]')),200['tag'])201self.assertEqual(summarize_list(elem.findall(".//tag[. = 'subtext']")),202['tag'])203self.assertEqual(summarize_list(elem.findall(".//tag[. = 'subtext ']")),204[])205self.assertEqual(summarize_list(elem.findall(".//tag[.= ' subtext']")),206[])207208self.assertEqual(summarize_list(elem.findall("../tag")),209[])210self.assertEqual(summarize_list(elem.findall("section/../tag")),211['tag','tag'])212self.assertEqual(summarize_list(etree.ElementTree(elem).findall("./tag")),213['tag','tag'])214215# FIXME: ET's Path module handles this case incorrectly; this gives216# a warning in 1.3, and the behaviour will be modified in 1.4.217self.assertEqual(summarize_list(etree.ElementTree(elem).findall("/tag")),218['tag','tag'])219220# duplicate section => 2x tag matches221elem[1]=deepcopy(elem[2])222self.assertEqual(summarize_list(elem.findall(".//section[tag = 'subtext']")),223['section','section'])224self.assertEqual(summarize_list(elem.findall(".//tag[. = 'subtext']")),225['tag','tag'])226self.assertEqual(summarize_list(elem.findall(".//tag[@class][@id]")),227['tag','tag'])
228229230#class ElementTreeElementPathTestCase(EtreeElementPathTestCase):231# import xml.etree.ElementTree as etree232# import xml.etree.ElementPath as _elementpath233234