def test_multiple_elementrees(self): tree = self.parse('<a><b>B</b><c>C</c></a>') schema = etree.RelaxNG( self.parse('''\ <element name="a" xmlns="http://relaxng.org/ns/structure/1.0"> <element name="b"> <text /> </element> <element name="c"> <text /> </element> </element> ''') ) self.assert_(schema.validate(tree)) self.assert_(schema.validate(tree)) schema = etree.RelaxNG( self.parse('''\ <element name="b" xmlns="http://relaxng.org/ns/structure/1.0"> <text /> </element> ''') ) c_tree = etree.ElementTree(tree.getroot()[1]) self.assertEqual(self._rootstring(c_tree), _bytes('<c>C</c>')) self.assert_(not schema.validate(c_tree)) b_tree = etree.ElementTree(tree.getroot()[0]) self.assertEqual(self._rootstring(b_tree), _bytes('<b>B</b>')) self.assert_(schema.validate(b_tree))
def test_multiple_elementrees(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="a"><A><xsl:apply-templates/></A></xsl:template> <xsl:template match="b"><B><xsl:apply-templates/></B></xsl:template> <xsl:template match="c"><C><xsl:apply-templates/></C></xsl:template> </xsl:stylesheet>''') self.assertEquals(self._rootstring(tree), _bytes('<a><b>B</b><c>C</c></a>')) result = tree.xslt(style) self.assertEquals(self._rootstring(tree), _bytes('<a><b>B</b><c>C</c></a>')) self.assertEquals(self._rootstring(result), _bytes('<A><B>B</B><C>C</C></A>')) b_tree = etree.ElementTree(tree.getroot()[0]) self.assertEquals(self._rootstring(b_tree), _bytes('<b>B</b>')) result = b_tree.xslt(style) self.assertEquals(self._rootstring(tree), _bytes('<a><b>B</b><c>C</c></a>')) self.assertEquals(self._rootstring(result), _bytes('<B>B</B>')) c_tree = etree.ElementTree(tree.getroot()[1]) self.assertEquals(self._rootstring(c_tree), _bytes('<c>C</c>')) result = c_tree.xslt(style) self.assertEquals(self._rootstring(tree), _bytes('<a><b>B</b><c>C</c></a>')) self.assertEquals(self._rootstring(result), _bytes('<C>C</C>'))
def test_xpath_evaluator_tree(self): tree = self.parse('<a><b><c></c></b></a>') child_tree = etree.ElementTree(tree.getroot()[0]) e = etree.XPathEvaluator(child_tree) self.assertEquals([], e('a')) root = child_tree.getroot() self.assertEquals([root[0]], e('c'))
def test_xslt_document_elementtree(self): # make sure document('') works from loaded files xslt = etree.XSLT(etree.ElementTree(file=fileInTestDir("test-document.xslt"))) result = xslt(etree.XML('<a/>')) root = result.getroot() self.assertEquals(root.tag, 'test') self.assertEquals(root[0].tag, '{http://www.w3.org/1999/XSL/Transform}stylesheet')
def test_elementtree_getpath_partial(self): a = etree.Element("a") b = etree.SubElement(a, "b") c = etree.SubElement(a, "c") d1 = etree.SubElement(c, "d") d2 = etree.SubElement(c, "d") tree = etree.ElementTree(c) self.assertEqual('/c/d', tree.getpath(d2)[:4]) self.assertEqual([d2], tree.xpath(tree.getpath(d2)))
def test_dtd_internal(self): root = etree.XML(_bytes(''' <!DOCTYPE b SYSTEM "none" [ <!ELEMENT b (a)> <!ELEMENT a EMPTY> ]> <b><a/></b> ''')) dtd = etree.ElementTree(root).docinfo.internalDTD self.assert_(dtd) dtd.assertValid(root)
def test_dtd_internal_invalid(self): root = etree.XML(_bytes(''' <!DOCTYPE b SYSTEM "none" [ <!ELEMENT b (a)> <!ELEMENT a (c)> <!ELEMENT c EMPTY> ]> <b><a/></b> ''')) dtd = etree.ElementTree(root).docinfo.internalDTD self.assertTrue(dtd) self.assertFalse(dtd.validate(root))
def test_dtd_attrs(self): dtd = etree.DTD(fileUrlInTestDir("test.dtd")) # Test DTD.system_url attribute self.assertTrue(dtd.system_url.endswith("test.dtd")) # Test elements and their attributes a = dtd.elements()[0] self.assertEqual(a.name, "a") self.assertEqual(a.type, "element") self.assertEqual(a.content.name, "b") self.assertEqual(a.content.type, "element") self.assertEqual(a.content.occur, "once") aattr = a.attributes()[0] self.assertEqual(aattr.name, "default") self.assertEqual(aattr.type, "enumeration") self.assertEqual(aattr.values(), ["valueA", "valueB"]) self.assertEqual(aattr.default_value, "valueA") b = dtd.elements()[1] self.assertEqual(b.name, "b") self.assertEqual(b.type, "empty") self.assertEqual(b.content, None) # Test entities and their attributes c = dtd.entities()[0] self.assertEqual(c.name, "c") self.assertEqual(c.orig, "*") self.assertEqual(c.content, "*") # Test DTD.name attribute root = etree.XML( _bytes(''' <!DOCTYPE a SYSTEM "none" [ <!ELEMENT a EMPTY> ]> <a/> ''')) dtd = etree.ElementTree(root).docinfo.internalDTD self.assertEqual(dtd.name, "a") # Test DTD.name and DTD.systemID attributes parser = etree.XMLParser(dtd_validation=True) xml = '<!DOCTYPE a SYSTEM "test.dtd"><a><b/></a>' root = etree.fromstring(xml, parser=parser, base_url=fileUrlInTestDir("test.xml")) dtd = root.getroottree().docinfo.internalDTD self.assertEqual(dtd.name, "a") self.assertEqual(dtd.system_url, "test.dtd")
def test_internal_dtds(self): for el_count in range(2, 5): for attr_count in range(4): root = etree.XML( _bytes(''' <!DOCTYPE el0 SYSTEM "none" [ ''' + ''.join([ ''' <!ATTLIST el%d attr%d (x | y | z) "z" > ''' % (e, a) for a in range(attr_count) for e in range(el_count) ]) + ''.join([ ''' <!ELEMENT el%d EMPTY> ''' % e for e in range(1, el_count) ]) + ''' ''' + '<!ELEMENT el0 (%s)>' % '|'.join(['el%d' % e for e in range(1, el_count)]) + ''' ]> <el0><el1 %s /></el0> ''' % ' '.join(['attr%d="x"' % a for a in range(attr_count)]))) dtd = etree.ElementTree(root).docinfo.internalDTD self.assertTrue(dtd) dtd.assertValid(root) e = -1 for e, el in enumerate(dtd.iterelements()): self.assertEqual(attr_count, len(el.attributes())) a = -1 for a, attr in enumerate(el.iterattributes()): self.assertEqual('enumeration', attr.type) self.assertEqual('none', attr.default) self.assertEqual('z', attr.default_value) values = sorted(attr.values()) self.assertEqual(['x', 'y', 'z'], values) self.assertEqual(attr_count - 1, a) self.assertEqual(el_count - 1, e) self.assertEqual(el_count, len(dtd.elements()))
def test_dtd_api_internal(self): root = etree.XML( _bytes(''' <!DOCTYPE b SYSTEM "none" [ <!ATTLIST a attr1 (x | y | z) "z" attr2 CDATA #FIXED "X" > <!ELEMENT b (a)> <!ELEMENT a EMPTY> ]> <b><a/></b> ''')) dtd = etree.ElementTree(root).docinfo.internalDTD self.assertTrue(dtd) dtd.assertValid(root) seen = [] for el in dtd.iterelements(): if el.name == 'a': self.assertEqual(2, len(el.attributes())) for attr in el.iterattributes(): if attr.name == 'attr1': self.assertEqual('enumeration', attr.type) self.assertEqual('none', attr.default) self.assertEqual('z', attr.default_value) values = attr.values() values.sort() self.assertEqual(['x', 'y', 'z'], values) else: self.assertEqual('attr2', attr.name) self.assertEqual('cdata', attr.type) self.assertEqual('fixed', attr.default) self.assertEqual('X', attr.default_value) else: self.assertEqual('b', el.name) self.assertEqual(0, len(el.attributes())) seen.append(el.name) seen.sort() self.assertEqual(['a', 'b'], seen) self.assertEqual(2, len(dtd.elements()))
def test_xslt_elementtree_error(self): self.assertRaises(ValueError, etree.XSLT, etree.ElementTree())
def test_schematron_elementtree_error(self): self.assertRaises(ValueError, etree.Schematron, etree.ElementTree())
def test_xpath_elementtree_error(self): self.assertRaises(ValueError, etree.XPath('*'), etree.ElementTree())
def test_relaxng_elementtree_error(self): self.assertRaises(ValueError, etree.RelaxNG, etree.ElementTree())
def test_xmlschema_elementtree_error(self): self.assertRaises(ValueError, etree.XMLSchema, etree.ElementTree())