Get depth of an element after xpath query
Is there a way to obtain the depth of an element after an xpath query ? I am interested in finding the position of an element in a hierarchy of elements.
Hi,
Is there a way to obtain the depth of an element after an xpath query ? I am interested in finding the position of an element in a hierarchy of elements.
Yes. Please try to bring some sample code like this to show what you want to accomplish.
from lxml import etree
x = etree.XML('<a><b><c/></b></a>')
x.xpath('//c') [<Element c at 0x10983ff00>] c = x.xpath('//c')[0]
c <Element c at 0x10983ff00>
c.xpath('ancestor::*') [<Element a at 0x10983ffa0>, <Element b at 0x10983feb0>] len(c.xpath('ancestor::*')) 2
c.xpath('count(ancestor::*)') 2.0
jens -- qdevelop Softwareentwicklung Jens Quade und Peter Quade GbR On 21.11.2013, at 15:01, Bogdan Cristea <cristeab@gmail.com> wrote:
Is there a way to obtain the depth of an element after an xpath query ? I am interested in finding the position of an element in a hierarchy of elements. _________________________________________________________________ Mailing list for the lxml Python XML toolkit - http://lxml.de/ lxml@lxml.de https://mailman-mail5.webfaction.com/listinfo/lxml
On 11/21/2013 03:46 PM, jens quade wrote:
Hi,
Is there a way to obtain the depth of an element after an xpath query ? I am interested in finding the position of an element in a hierarchy of elements. Yes. Please try to bring some sample code like this to show what you want to accomplish.
from lxml import etree
x = etree.XML('<a><b><c/></b></a>')
x.xpath('//c') [<Element c at 0x10983ff00>] c = x.xpath('//c')[0]
c <Element c at 0x10983ff00> c.xpath('ancestor::*') [<Element a at 0x10983ffa0>, <Element b at 0x10983feb0>] len(c.xpath('ancestor::*')) 2
c.xpath('count(ancestor::*)')
Thanks, this is exactly what I needed.
participants (2)
-
Bogdan Cristea -
jens quade