如何在lxml中的find/findall中使用xml名称空间? [英] How do I use xml namespaces with find/findall in lxml?
问题描述
我正在尝试解析OpenOffice ODS电子表格中的内容. ods格式本质上只是一个带有许多文档的zip文件.电子表格的内容存储在"content.xml"中.
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 stored in 'content.xml'.
import zipfile
from lxml import etree
zf = zipfile.ZipFile('spreadsheet.ods')
root = etree.parse(zf.open('content.xml'))
电子表格的内容位于一个单元格中:
The content of the spreadsheet is in a cell:
table = root.find('.//{urn:oasis:names:tc:opendocument:xmlns:table:1.0}table')
我们也可以直接进入这些行:
We can also go straight for the rows:
rows = root.findall('.//{urn:oasis:names:tc:opendocument:xmlns:table:1.0}table-row')
各个元素都知道名称空间:
The individual elements know about the namespaces:
>>> table.nsmap['table']
'urn:oasis:names:tc:opendocument:xmlns:table:1.0'
如何直接在find/findall中使用命名空间?
How do I use the namespaces directly in find/findall?
明显的解决方案不起作用.
The obvious solution does not work.
尝试从表中获取行:
>>> root.findall('.//table:table')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "lxml.etree.pyx", line 1792, in lxml.etree._ElementTree.findall (src/lxml/lxml.etree.c:41770)
File "lxml.etree.pyx", line 1297, in lxml.etree._Element.findall (src/lxml/lxml.etree.c:37027)
File "/usr/lib/python2.6/dist-packages/lxml/_elementpath.py", line 225, in findall
return list(iterfind(elem, path))
File "/usr/lib/python2.6/dist-packages/lxml/_elementpath.py", line 200, in iterfind
selector = _build_path_iterator(path)
File "/usr/lib/python2.6/dist-packages/lxml/_elementpath.py", line 184, in _build_path_iterator
selector.append(ops[token[0]](_next, token))
KeyError: ':'
推荐答案
如果root.nsmap
包含table
名称空间前缀,则可以:
If root.nsmap
contains the table
namespace prefix then you could:
root.xpath('.//table:table', namespaces=root.nsmap)
findall(path)
接受{namespace}name
语法而不是namespace:name
.因此,在将path
传递给findall()
之前,应使用名称空间字典将其预处理为{namespace}name
形式.
findall(path)
accepts {namespace}name
syntax instead of namespace:name
. Therefore path
should be preprocessed using namespace dictionary to the {namespace}name
form before passing it to findall()
.
这篇关于如何在lxml中的find/findall中使用xml名称空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!