Reputation: 9869
This code successfully checks if xpath if a single element exists in xpath:
x = root.xpath("boolean(//*[contains(local-name(), 'bar')])", namespaces=lnamespaces)
print("xx ", x)
But I need to check if a path like foo/bar
is exists. I tried:
x = root.xpath("boolean(//*[contains(local-name(), 'foo/bar')])", namespaces=lnamespaces)
print("xx ", x)
However, the code above evaluates to false even if the foo/bar
path exists.
Code:
from lxml import etree
mystr = """
<some>
<foo>
<bar>
</bar>
</foo>
<baz>
</baz>
<some>
"""
etxml = etree.HTML(mystr)
result = etxml.xpath("boolean(//*[contains(local-name(), 'foo/bar')])")
print("result: ", result)
Upvotes: 2
Views: 407
Reputation: 24930
I'm not sure if this is what you need, but I'm afraid the only way I could get there is like this:
tree_struct = etree.ElementTree(etxml)
for e in etxml.iter('bar'):
print('/foo/bar' in tree_struct.getpath(e))
Output:
True
Upvotes: 1