xpath返回根本不包含某个节点的节点

时间:2016-02-25 08:34:28

标签: html xml xpath

我使用xpath来定位html标签。我有一个包含5行的表,我想根据表行中某些表数据标记中的类选择行。

<table class="layoutDataDefinitionTable grid-row-table">
    <thead/>
    <tbody>
        <tr id="row_0"/>
            <td data-dojo-attach-point="_iconNode"/>
            <td class="dgrid-cell-padding dgrid-no-left"/>
            <td class="dgrid-cell-padding"/>
            <td class="dgrid-cell-padding"/>
            <td class="dgrid-cell-padding"/>
            <td class="dgrid-cell-padding"/>
            <td class="dgrid-cell-padding"/>
        <tr id="row_1">
            <td data-dojo-attach-point="_iconNode" class="arrowDown"/>
            <td class="dgrid-cell-padding dgrid-no-left"/>
            <td class="dgrid-cell-padding"/>
            <td class="dgrid-cell-padding"/>
            <td class="dgrid-cell-padding"/>
            <td class="dgrid-cell-padding"/>
            <td class="dgrid-cell-padding"/>
        <tr id="row_2">
            <td data-dojo-attach-point="_iconNode"/>
            <td class="dgrid-cell-padding dgrid-no-left dgrid-cell-indent"/>
            <td class="dgrid-cell-padding"/>
            <td class="dgrid-cell-padding"/>
            <td class="dgrid-cell-padding"/>
            <td class="dgrid-cell-padding"/>
            <td class="dgrid-cell-padding"/>
        <tr id="row_3">
            <td data-dojo-attach-point="_iconNode" class="arrowDown"/>
            <td class="dgrid-cell-padding dgrid-no-left"/>
            <td class="dgrid-cell-padding"/>
            <td class="dgrid-cell-padding"/>
            <td class="dgrid-cell-padding"/>
            <td class="dgrid-cell-padding"/>
            <td class="dgrid-cell-padding"/>
        <tr id="row_4">
            <td data-dojo-attach-point="_iconNode" class="arrowDown"/>
            <td class="dgrid-cell-padding dgrid-no-left"/>
            <td class="dgrid-cell-padding"/>
            <td class="dgrid-cell-padding"/>
            <td class="dgrid-cell-padding"/>
            <td class="dgrid-cell-padding"/>
            <td class="dgrid-cell-padding"/>
    </tbody>
</table>

我想选择没有带有箭头类的表数据的表行,而没有类dgrid-cell-padding dgrid-no-left dgrid-cell-indent。我试过这样做:

$x("//table[contains(@class, 'layoutDataDefinitionTable grid-row-table')]//tbody//tr//td[not(contains(@class, 'dgrid-cell dgrid-cell-padding dgrid-no-left dgrid-cell-indent')) and not(contains(@class, 'arrowDown'))]/..");

但是这只返回所有行,因为每一行都有一个td,它不包含这两个类。我希望它只返回第0行。

1 个答案:

答案 0 :(得分:2)

您可以将谓词移至tr,并检查所有td个孩子,看看他们是否都符合不包含所需类别的标准: //table[contains(@class, 'layoutDataDefinitionTable grid-row-table')]/tbody/tr[not(contains(td/@class, 'dgrid-cell-padding dgrid-no-left dgrid-cell-indent')) and not(contains(td/@class, 'arrowDown'))]

这将仅返回ID为tr的{​​{1}}。