ElementTree oddities

Stefan Behnel stefan_ml at behnel.de
Tue Sep 16 13:40:37 EDT 2008


Mark Thomas wrote:
> here's how you would do it in lxml (http://codespeak.net/
> lxml/index.html), a library which supports XPath:
> 
> from lxml import etree
> tree = etree.fromstring('<highlight><ref>Bar</ref>:</highlight>')
> print ' '.join(tree.xpath('//text()'))

If you want to use XPath, try this:

	print tree.xpath('string()')

or if you want to use it in real code:

	get_tree_text = etree.XPath('string()')
	print get_tree_text(tree)

or just use

	print etree.tostring(tree, method="text")

Stefan



More information about the Python-list mailing list