0

I'm working with an xml that gets pretty deep. I'm able to extract a certain element via:

parent_element = tree.find(f'.//ns:{tag}[@eId="{eId}"]', namespaces = nsmap)

Is there any way I can reconstruct the full Xpath of the returned element?

Thanks

Lauren
  • 61
  • 6
  • If you cannot use lxml, maybe this helps: https://stackoverflow.com/a/68239457/407651 – mzjn Aug 25 '21 at 12:29

1 Answers1

2

if you use lxml, try: getpath.

getpath(self, element)

Returns a structural, absolute XPath expression to find the element.

For namespaced elements, the expression uses prefixes from the document, which therefore need to be provided in order to make any use of the expression in XPath.

Also see the method getelementpath(self, element), which returns a self-contained ElementPath expression.

HALF9000
  • 518
  • 2
  • 14