简体   繁体   中英

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

Use e.xpath , not e.find :


import lxml.etree as ET

content = '''\
<Envelope xmlns="http://www.example.com/zzz/yyy">
  <Header>
    <Version>1</Version>
  </Header>
  <Buckets>
    some stuff
  </Buckets>
</Envelope>
'''
root = ET.fromstring(content)
print(root.xpath("./*[local-name()='Buckets']"))
# [<Element {http://www.example.com/zzz/yyy}Buckets at 0xb73a62ac>]

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