简体   繁体   中英

lxml._ElementTree.getpath(element) returns “*” instead of tag names for elements in non default name space

Please help to make getpath() to return full tag names in getpath() xpath or to find workaround

I'm trying to generate xpath to element in a lxml.etree._ElementTree . ElementTree is generated by parsing 600Kb response from some production WebService.

print elem.getroottree().getpath(elem)

Here is result I get:

'/S:Envelope/S:Body/ns5:getPhysicalResponse/*[18]/*[12]/*[6]/*[2]'

Unfortunately I cannot post original xml - it contains proprietary customer information. Also I tried to reproduce this result with automatically generated simple element tree that has 100 nested levels, each level having 100 children but without luck - getpath() returned xpath with full tag names.

Update Looking into lxml source code - it points to tree.h xmlGetNodePath method from the libxml2 library. So this is actually libxml2 behavior.

Update Doing more tests I figured out that this happens every time when tag has non default namespace.

根据需要使用getelementpath()和postprocess命名空间。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM