balki balki - 1 year ago 191
Python Question

Wildcard namespaces in lxml

How to query using xpath ignoring the xml namespace? I am using python lxml library. I tried the solution from this question but doesn't seem to work.

In [151]: e.find("./*[local-name()='Buckets']")
File "<string>", line unknown
SyntaxError: invalid predicate

Answer Source

Use e.xpath, not e.find:

import lxml.etree as ET

content = '''\
<Envelope xmlns="">
    some stuff
root = ET.fromstring(content)
# [<Element {}Buckets at 0xb73a62ac>]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download