how do I use empty namespaces in an lxml xpath query?

前端 未结 2 1654

I have an xml document in the following format:



        
相关标签:
2条回答
  • 2020-11-30 09:12

    Something like this should work:

    import lxml.etree as et
    
    ns = {"atom": "http://www.w3.org/2005/Atom"}
    tree = et.fromstring(xml)
    for node in tree.xpath('//atom:entry', namespaces=ns):
        print node
    

    See also http://lxml.de/xpathxslt.html#namespaces-and-prefixes.

    Alternative:

    for node in tree.xpath("//*[local-name() = 'entry']"):
        print node
    
    0 讨论(0)
  • 2020-11-30 09:16

    Use findall method.

    for item in tree.findall('{http://www.w3.org/2005/Atom}entry'): 
        print item
    
    0 讨论(0)
提交回复
热议问题