How to get path of an element in lxml?

南楼画角 提交于 2019-11-27 11:41:40

Use getpath from ElementTree objects.

from lxml import etree

root = etree.fromstring('<foo><bar>Data</bar><bar><baz>data</baz>'
                        '<baz>data</baz></bar></foo>')

tree = etree.ElementTree(root)
for e in root.iter():
    print tree.getpath(e)

Prints

/foo
/foo/bar[1]
/foo/bar[2]
/foo/bar[2]/baz[1]
/foo/bar[2]/baz[2]

See the Xpath and XSLT with lxml from the lxml documentation This gives the path of the element containg the text

An example would be

import cStringIO
from lxml import etree

f = cStringIO.StringIO('<foo><bar><x1>hello</x1><x1>world</x1></bar></foo>')
tree = lxml.etree.parse(f)
find_text = etree.XPath("//text()")

# and print out the required data
print [tree.getpath( text.getparent()) for text in find_text(tree)]

# answer I get is 
>>> ['/foo/bar/x1[1]', '/foo/bar/x1[2]']

If all you have in your section of code is the element and you want the element's xpath do then element.getroottree().getpath(element) will do the job.

from lxml import etree

xml = '''
<test>
    <a/>
    <b>
       <i/>
       <ii/>
    </b>
</test>
'''
tree = etree.fromstring(xml)

for element in tree.iter():
    print element.getroottree().getpath(element)
root = etree.parse(open('tmp.txt'))

for e in root.iter():
    print root.getpath(e)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!