如果查询返回此XML

<hudson>
    <job>
        <displayName>test1</displayName>
        <url>https://something/test1</url>
        <lastBuild>
            <building>true</building>
            <builtOn>server1</builtOn>
        </lastBuild>
    </job>
    <job>
        <displayName>test2</displayName>
        <url>https://something/test2</url>
        <lastBuild>
            <building>false</building>
            <builtOn>server1</builtOn>
        </lastBuild>
    </job>
    <job>
        <displayName>test3</displayName>
        <url>https://something/test3
        </url>
        <lastBuild>
            <building>true</building>
            <builtOn>server2</builtOn>
        </lastBuild>
    </job>
    <job>
        <displayName>test4</displayName>
        <url>https://something/test4</url>
    </job>
    <job>
        <displayName>test5</displayName>
        <url>https://something/test5</url>
    </job>
</hudson>


如何使用xpath进一步过滤以仅获取标记building为true的作业,而排除标记lastBuild不存在的那些作业?

对于第一个选项,我使用

&xpath=/hudson/job/lastBuild[contains(building,'true')]


但是不知道如何清除那些没有标签lastBuild的标签...

最佳答案

对于您显示的XML,此XPath,

/hudson/job[lastBuild/building = 'true']


将选择具有job元素的lastBuild元素,其中buildingtrue

<job>
    <displayName>test1</displayName>
    <url>https://something/test1</url>
    <lastBuild>
      <building>true</building>
      <builtOn>server1</builtOn>
    </lastBuild>
</job>
<job>
    <displayName>test3</displayName>
    <url>https://something/test3</url>
    <lastBuild>
      <building>true</building>
      <builtOn>server2</builtOn>
    </lastBuild>
</job>

09-18 21:20