def test_class_lookup_type_mismatch(self): class MyLookup(etree.CustomElementClassLookup): def lookup(self, t, d, ns, name): if t == 'element': if name == 'root': return etree.ElementBase return etree.CommentBase elif t == 'comment': return etree.PIBase elif t == 'PI': return etree.EntityBase elif t == 'entity': return etree.ElementBase else: raise ValueError('got type %s' % t) parser = etree.XMLParser(resolve_entities=False) parser.set_element_class_lookup(MyLookup()) root = etree.XML(_bytes('<root></root>'), parser) self.assertEqual('root', root.tag) self.assertEqual(etree.ElementBase, type(root)) root = etree.XML(_bytes("<root><test/></root>"), parser) self.assertRaises(TypeError, root.__getitem__, 0) root = etree.XML(_bytes("<root><!-- test --></root>"), parser) self.assertRaises(TypeError, root.__getitem__, 0) root = etree.XML(_bytes("<root><?test?></root>"), parser) self.assertRaises(TypeError, root.__getitem__, 0) root = etree.XML( _bytes('<!DOCTYPE root [<!ENTITY myent "ent">]>' '<root>&myent;</root>'), parser) self.assertRaises(TypeError, root.__getitem__, 0) root = etree.XML(_bytes('<root><root/></root>'), parser) self.assertEqual('root', root[0].tag)
def test_dtd_assertValid(self): root = etree.XML("<b><a/></b>") dtd = etree.DTD(BytesIO("<!ELEMENT b (a)><!ELEMENT a EMPTY>")) dtd.assertValid(root)
def test_dtd_invalid(self): root = etree.XML("<b><a/></b>") dtd = etree.DTD(BytesIO("<!ELEMENT b EMPTY>")) self.assertRaises(etree.DocumentInvalid, dtd.assertValid, root)
def test_declaration_apos(self): root = etree.XML('''<!DOCTYPE a SYSTEM "'"><a/>''') doc = root.getroottree() self.assertEqual(doc.docinfo.doctype, '''<!DOCTYPE a SYSTEM "'">''') self.assertEqual(etree.tostring(doc), _bytes('''<!DOCTYPE a SYSTEM "'">\n<a/>'''))
def test_declaration_quote_withoutpid(self): root = etree.XML('''<!DOCTYPE a SYSTEM '"'><a/>''') doc = root.getroottree() self.assertEqual(doc.docinfo.doctype, '''<!DOCTYPE a SYSTEM '"'>''') self.assertEqual(etree.tostring(doc), _bytes('''<!DOCTYPE a SYSTEM '"'>\n<a/>'''))
def test_dtd_stringio(self): root = etree.XML(_bytes("<b/>")) dtd = etree.DTD(BytesIO("<!ELEMENT b EMPTY>")) self.assertTrue(dtd.validate(root))
def test_xslt_document_XML_resolver(self): # make sure document('') works when custom resolvers are in use assertEquals = self.assertEquals called = {'count' : 0} class TestResolver(etree.Resolver): def resolve(self, url, id, context): assertEquals(url, 'file://ANYTHING') called['count'] += 1 return self.resolve_string('<CALLED/>', context) parser = etree.XMLParser() parser.resolvers.add(TestResolver()) xslt = etree.XSLT(etree.XML(_bytes("""\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:l="local"> <xsl:template match="/"> <test> <xsl:for-each select="document('')//l:data/l:entry"> <xsl:copy-of select="document('file://ANYTHING')"/> <xsl:copy> <xsl:attribute name="value"> <xsl:value-of select="."/> </xsl:attribute> </xsl:copy> </xsl:for-each> </test> </xsl:template> <l:data> <l:entry>A</l:entry> <l:entry>B</l:entry> </l:data> </xsl:stylesheet> """), parser)) self.assertEquals(called['count'], 0) result = xslt(etree.XML('<a/>')) self.assertEquals(called['count'], 1) root = result.getroot() self.assertEquals(root.tag, 'test') self.assertEquals(len(root), 4) self.assertEquals(root[0].tag, 'CALLED') self.assertEquals(root[1].tag, '{local}entry') self.assertEquals(root[1].text, None) self.assertEquals(root[1].get("value"), 'A') self.assertEquals(root[2].tag, 'CALLED') self.assertEquals(root[3].tag, '{local}entry') self.assertEquals(root[3].text, None) self.assertEquals(root[3].get("value"), 'B')
def test_xslt_document_parse_deny_all(self): access_control = etree.XSLTAccessControl.DENY_ALL xslt = etree.XSLT(etree.parse(fileInTestDir("test-document.xslt")), access_control = access_control) self.assertRaises(etree.XSLTApplyError, xslt, etree.XML('<a/>'))
def test_rel_xpath_boolean(self): root = etree.XML('<a><b><c/></b></a>') el = root[0] self.assert_(el.xpath('boolean(c)')) self.assert_(not el.xpath('boolean(d)'))
def lookup(dummy, id): return etree.XML(xml_data).xpath('id(%r)' % id)
def test_unicode_xml(self): tree = etree.XML(_str('<p>%s</p>') % uni) self.assertEquals(uni, tree.text)
def test_proxy_reuse_after_gc(self): root = etree.XML('<a><b><c/></b></a>') b = root.find('b') self.assertTrue(self.etree.iselement(b)) gc.collect() self.assertTrue(b is root[0])
def test_proxy_reuse(self): root = etree.XML('<a><b><c/></b></a>') b = root.find('b') self.assertTrue(b is root[0])