python xml:具有特定属性值的子元素的 xpath

python xml: xpath for elements with childer with specific attribute values

我需要以下内容的 xpath 表达式:

所有带有标签 "test" 的节点都有一个带有标签 "status" 的子节点,这些节点的属性名为 "status",值为 "PASS"

所以对于下面的 xml,我需要获取 ID 为 "s1-t1" 的节点。我应该能够修改它,而不是通过测试,我得到具有属性 [@status='FAIL'] 的测试。我尝试了几件事都无济于事:

我现在不确定要尝试什么。这是我正在处理的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<robot generated="20151111 16:29:28.630" generator="Robot 2.9.1 (Python 2.7.10 on cygwin)">
<suite source="/cygdrive/c/test/robot/thing.robot" id="s1" name="Thing">
<test id="s1-t1" name="Case1">
<kw name="Should Contain" library="BuiltIn">
<doc>Fails if ``item1`` does not contain ``item2`` one or more times.</doc>
<arguments>
<arg>hello</arg>
<arg>hell</arg>
</arguments>
<status status="PASS" endtime="20151111 16:29:28.689" starttime="20151111 16:29:28.689"></status>
</kw>
<status status="PASS" endtime="20151111 16:29:28.689" critical="yes" starttime="20151111 16:29:28.688"></status>
</test>
<test id="s1-t2" name="Case2">
<kw name="Should Contain" library="BuiltIn">
<doc>Fails if ``item1`` does not contain ``item2`` one or more times.</doc>
<arguments>
<arg>hello</arg>
<arg>elo</arg>
</arguments>
<msg timestamp="20151111 16:29:28.690" level="FAIL">'hello' does not contain 'elo'</msg>
<status status="FAIL" endtime="20151111 16:29:28.690" starttime="20151111 16:29:28.690"></status>
</kw>
<status status="FAIL" endtime="20151111 16:29:28.690" critical="yes" starttime="20151111 16:29:28.690">'hello' does not contain 'elo'</status>
</test>
<test id="s1-t3" name="Case3">
<kw name="Should Contain" library="BuiltIn">
<doc>Fails if ``item1`` does not contain ``item2`` one or more times.</doc>
<arguments>
<arg>hello</arg>
</arguments>
<msg timestamp="20151111 16:29:28.691" level="FAIL">Keyword 'BuiltIn.Should Contain' expected 2 to 4 arguments, got 1.</msg>
<status status="FAIL" endtime="20151111 16:29:28.691" starttime="20151111 16:29:28.691"></status>
</kw>
<status status="FAIL" endtime="20151111 16:29:28.691" critical="yes" starttime="20151111 16:29:28.691">Keyword 'BuiltIn.Should Contain' expected 2 to 4 arguments, got 1.</status>
</test>
<status status="FAIL" endtime="20151111 16:29:28.692" starttime="20151111 16:29:28.633"></status>
</suite>
<statistics>
<total>
<stat fail="2" pass="1">Critical Tests</stat>
<stat fail="2" pass="1">All Tests</stat>
</total>
<tag>
</tag>
<suite>
<stat fail="2" id="s1" name="Thing" pass="1">Thing</stat>
</suite>
</statistics>
<errors>
</errors>
</robot>

你快到了:

.//test[status/@status = "PASS"]

这正是 所有带有标签 "test" 的节点都有一个带有标签 "status" 的子节点,这些节点有一个名为 "status" 的属性,其值为 "PASS".