python - 关于布尔运算符的Python问题elemetree
问题描述
有没有办法在 findall 命令的 xpath 中使用布尔运算符。例如:
root.findall("./example/[value='a' or 'b']/example2"
解决方案
无法让 XPATH 在其中工作,ElementTree
但能够使其在lxml
.
不清楚value
是属性还是子级,example
所以我同时显示。
import lxml.etree as ET
data = '''\
<abc>
<example value="a">
<example2>foo</example2>
</example>
<example value="b">
<example2>bar</example2>
</example>
<example value="x">
<example2>not this</example2>
</example>
</abc>
'''
root = ET.fromstring(data)
for e in root.xpath('./example[@value="a" or @value="b"]/example2'):
print(e.text)
data2 = '''\
<abc>
<example>
<value>a</value>
<example2>abc</example2>
</example>
<example>
<value>b</value>
<example2>def</example2>
</example>
<example>
<value>not this</value>
<example2>xyz</example2>
</example>
</abc>
'''
root = ET.fromstring(data2)
for e in root.xpath('./example[value="a" or value="b"]/example2'):
print(e.text)
推荐阅读
- c++ - 无法在 VS2017 中打开 DirectX 11 应用程序(通用 Windows)模板 - HResult 0x80042003
- python - 在执行操作时获取单独子数组中的 numpy 子数组的结果,而不使用 for 循环
- python - 在熊猫中,如何将一系列浮点数或无浮点数转换为带整数的字符串
- unit-testing - JEST中参数和返回的使用
- marklogic - 如何使用路径范围索引搜索空值?
- node.js - 无法读取节点 js 中未定义的属性“管道”
- javascript - 将超出列表项移动到引导程序中的下一列
- c# - 如何刷新 GridView 内容?
- css - 对某些部分使用另一个 CSS 框架
- javascript - Vue.js 和 Axios - 在 401 上重定向