以下代码行给我带来了问题:
{% if xml.findall('count("routes/route//process")') > 0 %}
我收到此错误消息:
...
{% if xml.findall('count("routes/route//process")') > 0 %}
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 390, in findall
return ElementPath.findall(self, path, namespaces)
File "/usr/lib/python2.7/xml/etree/ElementPath.py", line 293, in findall
return list(iterfind(elem, path, namespaces))
File "/usr/lib/python2.7/xml/etree/ElementPath.py", line 263, in iterfind
selector.append(ops[token[0]](next, token))
KeyError: '('
有什么建议?
ElementTree仅对XPath表达式提供有限的支持。count()
是不支持的。
您必须切换到lxml库;支持所有XPath 1.0的ElementTree兼容实现。
或者,测试直接返回多少个元素:
{% if xml.findall('routes/route//process') %}
因为非空结果列表在布尔上下文中是true-thy。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句