这是我的xpath:
<Unit>
<_unit_type>FSP</_unit_type>
<_logicalId>FSM1/FSP1</_logicalId>
</Unit>
<Unit>
<_unit_type>FSP</_unit_type>
<_logicalId>FSM1/FSP2</_logicalId>
</Unit>
所以我需要在我的xpath_pattern = .//Unit[_unitType='FSP'][starts-with(_logicalId,'FSM1']
时找到len(root.findall(xpath_pattern)
如果我的模式是.//Unit[_unitType='FSP']
,那么我的代码可以工作,我可以得到长度为2。但我需要添加另一个限定符,因为我的xml文件要复杂得多
Im获取SyntaxError:使用此模式时谓词无效
.//Unit[_unitType='FSP'][starts-with(_logicalId,'FSM1']
问题是
lxml
模块出于兼容性原因保留了两个不同的XPath引擎。从lxml FAQ:使用
xpath()
方法,如控制台输出:
签入here
只需使用和运算符添加所需的条件
例如,如果您正在寻找特定的“单位”:
您还可以使用“|”操作多个XPath。Xpath1 | Xpath2 | Xpath3 |。。。 如果您正在寻找与Unit不同的产品:
你已经写信了
这会导致无效谓词语法错误。这是因为缺少右括号。试试这个:
相关问题 更多 >
编程相关推荐