Python中的Xpath。获取SyntaxError(“无效谓词”)

时间:2017-04-28 05:19:41

标签: python python-3.x xpath elementtree

import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')

xpathobjects = tree.findall(".//BuildingNodeBase[name = 'Building name']")

我想要使用值BuildingNodeBase的子标记名称来提取Building name

但是获得:

  

SyntaxError(“invalid predicate”)

2 个答案:

答案 0 :(得分:2)

The XPath support in ElementTree is limited,但支持您的表达类型。只需要删除=

周围的额外空格即可
.//BuildingNodeBase[name='Building name']

答案 1 :(得分:0)

我使用lxml但我想你可以采用它来供你使用:

from lxml import etree

tree = etree.parse('test.xml')

xpathobjects = tree.xpath(".//BuildingNodeBase[@name = 'Building name']")