简体   繁体   English

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

[英]Xpath in Python . Getting SyntaxError(“invalid predicate”)

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

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

I am wanting to pull a BuildingNodeBase with a child tag name that has value Building name . 我想拉BuildingNodeBase与具有价值的子标签名Building name

But Getting: 但是得到:

SyntaxError("invalid predicate") SyntaxError(“无效谓词”)

The XPath support in ElementTree is limited , but your type of expression is supported. ElementTree中的XPath支持是受限制的 ,但是支持您的表达式类型。 It's just that you need to remove the extra spaces around the = : 只是您需要删除=周围的多余空格:

.//BuildingNodeBase[name='Building name']

I use lxml but I guess you can adopt this for your use: 我使用lxml但我想您可以采用此lxml

from lxml import etree

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM