How do I use xml namespaces with find/findall in lxml?

前端 未结 4 987
星月不相逢
星月不相逢 2020-12-05 02:50

I\'m trying to parse content in an OpenOffice ODS spreadsheet. The ods format is essentially just a zipfile with a number of documents. The content of the spreadsheet is sto

4条回答
  •  萌比男神i
    2020-12-05 03:21

    Here's a way to get all the namespaces in the XML document (and supposing there's no prefix conflict).

    I use this when parsing XML documents where I do know in advance what the namespace URLs are, and only the prefix.

            doc = etree.XML(XML_string)
    
            # Getting all the name spaces.
            nsmap = {}
            for ns in doc.xpath('//namespace::*'):
                if ns[0]: # Removes the None namespace, neither needed nor supported.
                    nsmap[ns[0]] = ns[1]
            doc.xpath('//prefix:element', namespaces=nsmap)
    

提交回复
热议问题