Martijn Faassen wrote:
I'm trying to understand why in scoder2 there's the use of _fakeRootDoc in a few places. I imagine this is to work around some bugs that show up in the trunk, but I don't have enough context on what and why. Stefan, can you enlighten me? Any tests that expose this behavior in particular?
I initially described the problem here: http://codespeak.net/pipermail/lxml-dev/2005-October/000501.html You may also follow my way towards the implementation in that thread. The original implementation is in SVN revision 18907, I copied the test case here, as added in revision 18906. You may notice that these test cases apply to etree and ElementTree, but only etree fails. I think I added further test cases along the road, so you may find other places in the test files where it says "*_multiple_*". The basic problem is always the same, though. Stefan Index: src/lxml/tests/test_etree.py =================================================================== --- src/lxml/tests/test_etree.py (Revision 18905) +++ src/lxml/tests/test_etree.py (Revision 18906) @@ -1544,7 +1544,25 @@ e = etree.Element('foo') e.set('bar', 'Bar') self.assertEquals(False, bool(e)) - + + def test_multiple_elementrees(self): + etree = self.etree + + a = etree.Element('a') + b = etree.SubElement(a, 'b') + + t = etree.ElementTree(a) + self.assertEquals(self._rootstring(t), '<a><b/></a>') + + t1 = etree.ElementTree(a) + self.assertEquals(self._rootstring(t1), '<a><b/></a>') + self.assertEquals(self._rootstring(t), '<a><b/></a>') + + t2 = etree.ElementTree(b) + self.assertEquals(self._rootstring(t2), '<b/>') + self.assertEquals(self._rootstring(t1), '<a><b/></a>') + self.assertEquals(self._rootstring(t), '<a><b/></a>') + def _writeElement(self, element, encoding='us-ascii'): """Write out element for comparison. """ @@ -2142,6 +2160,42 @@ '<doc><foo>Bar</foo><foo>Baz</foo></doc>', etree.tostring(result.getroot())) + 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), + '<a><b>B</b><c>C</c></a>') + result = tree.xslt(style) + self.assertEquals(self._rootstring(tree), + '<a><b>B</b><c>C</c></a>') + self.assertEquals(self._rootstring(result), + '<A><B>B</B><C>C</C></A>') + + b_tree = etree.ElementTree(tree.getroot()[0]) + self.assertEquals(self._rootstring(b_tree), + '<b>B</b>') + result = b_tree.xslt(style) + self.assertEquals(self._rootstring(tree), + '<a><b>B</b><c>C</c></a>') + self.assertEquals(self._rootstring(result), + '<B>B</B>') + + c_tree = etree.ElementTree(tree.getroot()[1]) + self.assertEquals(self._rootstring(c_tree), + '<c>C</c>') + result = c_tree.xslt(style) + self.assertEquals(self._rootstring(tree), + '<a><b>B</b><c>C</c></a>') + self.assertEquals(self._rootstring(result), + '<C>C</C>') + class ETreeRelaxNGTestCase(HelperTestCase): def test_relaxng(self): tree_valid = self.parse('<a><b></b></a>') @@ -2191,6 +2245,35 @@ self.assert_(tree_valid.relaxng(schema)) self.assert_(not tree_invalid.relaxng(schema)) + 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), '<c>C</c>') + self.assert_(not schema.validate(c_tree)) + + b_tree = etree.ElementTree(tree.getroot()[0]) + self.assertEqual(self._rootstring(b_tree), '<b>B</b>') + self.assert_(schema.validate(b_tree)) + + class ETreeXMLSchemaTestCase(HelperTestCase): def test_xmlschema(self): tree_valid = self.parse('<a><b></b></a>')