简体   繁体   English

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

[英]python xml: xpath for elements with childer with specific attribute values

I need an xpath expression for the following:我需要以下 xpath 表达式:

All nodes with the tag "test" that have a child with the tag "status" that have an attribute called "status" with the value "PASS"所有带有标签“test”的节点都有一个带有标签“status”的子节点,这些节点有一个名为“status”的属性,值为“PASS”

so with the xml below, I need to get the node with the id "s1-t1".因此,对于下面的 xml,我需要获取 ID 为“s1-t1”的节点。 I should be able to modify this so that instead of Passed tests, I get tests with the attribute [@status='FAIL'] .我应该能够修改它,而不是通过测试,我得到带有属性[@status='FAIL'] I've tried a couple things to no avail:我尝试了几件事无济于事:

  • ".//test/[@status='PASS']" returns nothing ".//test/[@status='PASS']"返回任何内容
  • ".//test/[status][@status='PASS']" returns nothing ".//test/[status][@status='PASS']"返回任何内容
  • ".//test//[status][@status='PASS']" throws a SyntaxError("invalid descendant") ".//test//[status][@status='PASS']"抛出一个 SyntaxError("无效后代")
  • ".//test//status[@status='PASS']" returns 2 status nodes (one for the test status and one for the keyword status) ".//test//status[@status='PASS']"返回2个状态节点(一个为测试状态,一个为关键字状态)
  • ".//test//status[@status='PASS']/../.." returns the correct test node, but also the suite node ".//test//status[@status='PASS']/../.."返回正确的测试节点,但也返回套件节点

I'm not sure what to try at this point.我不知道此时该尝试什么。 Here's the XML I'm working on:这是我正在处理的 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>

You are almost there:你快到了:

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

This is exactly All nodes with the tag "test" that have a child with the tag "status" that have an attribute called "status" with the value "PASS" .这正是所有带有标签“test”的节点,这些节点有一个带有标签“status”的子节点,它有一个名为“status”的属性,值为“PASS”

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

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