xpath上元素树的限制 [英] The limit of Element Tree on xpath
本文介绍了xpath上元素树的限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经使用元素树一段时间了,由于它的简单性,我喜欢它。
I've used Element Tree for a while and i love it because of its simplicity
但是我怀疑它在x路径上的实现
But I'm doubting of its implementation of x path
这是XML文件
<a>
<b name="b1"></b>
<b name="b2"><c/></b>
<b name="b2"></b>
<b name="b3"></b>
</a>
python代码
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
root.findall("b[@name='b2' and c]")
程序显示错误:
invalid predicate
但是,如果我使用
root.findall("b[@name='b2']") or
root.findall("b[c]")
有效,
推荐答案
ElementTree对XPath表达式提供了有限的支持。目标是支持缩写语法的一小部分。完整的XPath引擎不在核心库的范围内。
ElementTree provides limited support for XPath expressions. The goal is to support a small subset of the abbreviated syntax; a full XPath engine is outside the scope of the core library.
(F。Lundh, ElementTree中的XPath支持。)
(F. Lundh, XPath Support in ElementTree.)
用于支持XPath的ElementTree实现(1.0),请检出 LXML :
For an ElementTree implementation that supports XPath (1.0), check out LXML:
>>> s = """<a>
<b name="b1"></b>
<b name="b2"><c /></b>
<b name="b2"></b>
<b name="b3"></b>
</a>"""
>>> from lxml import etree
>>> t = etree.fromstring(s)
>>> t.xpath("b[@name='b2' and c]")
[<Element b at 1340788>]
这篇关于xpath上元素树的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文