孙子元素的XPath测试值?

时间:2016-05-29 00:33:25

标签: xml xpath

如果查询返回此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为真的作业,并排除标记lastBuild不存在的作业?

对于我使用的第一个选项

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

但不确定如何摆脱那些没有标签lastBuild ...

的人

1 个答案:

答案 0 :(得分:1)

对于您显示的XML,此XPath,

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

将选择job元素,lastBuildbuilding的{​​{1}}元素:

true