【发布时间】:2020-09-04 09:29:40
【问题描述】:
想象一下我有一个这样的 XML:
<root>
<elements>
<element> foo </element>
<element is="false"> foo </element>
<element is="false"> bli </element>
<element is="false"> bla </element>
</elements>
</root>
我该怎么做:
import xml.etree.ElementTree as ET
root = ET.fromstring(XmlFromAbove)
res_a = root.findall("element[@is='false']")) ##<- This gives me all elements with the specific attribute
res_b = root.findall("element[not@is='false']")) ##<- This would be nice to give me all elements without that specific attribute (`<element> foo </element>` in this case)
现在,我知道 res_b 不起作用,但我想这是一个常见问题,所以有人知道解决方法是什么吗?
再多指出一点(抄自cmets)
我可以肯定地找到包含“foo”的元素,但我想知道是否有办法找到任何不包含属性 is="false" 的元素。
【问题讨论】:
-
你想找什么?
-
foo 通过过滤掉属性 is="false" -
你可以直接找到 foo - 不是吗?为什么要将 foo 与 is=false 结合使用?试着解释一下这里的逻辑。
-
我可以肯定找到“foo”,但我想知道是否有办法找到任何不包含属性 is="false" 的元素。
-
知道了。看我的回答。
标签: python xml xpath elementtree