如何在xpath中选择除包含li节点的表节点下的节点以外的所有节点?

时间:2012-05-28 05:30:37

标签: xpath xpath-2.0

我有很多表,我想从这些表中选择所有节点,除了那些至少包含li节点的表。

示例:

<table>
<tr><td>all these nodes should match</td></tr>
</table>

<table>
<tr><td><ul><li>all these nodes including table should NOT match</li></ul></td></tr>
</table>

<table>
<tr><td><div>all these nodes should match</div></td></tr>
</table>

2 个答案:

答案 0 :(得分:3)

XPATH:

/root/table[not(descendant::li)]

XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <table>
        <tr>
            <td>all these nodes should match</td>
        </tr>
    </table>
    <table>
        <tr>
            <td>
                <ul>
                    <li>all these nodes including table should NOT match</li>
                </ul>
            </td>
        </tr>
    </table>
    <table>
        <tr>
            <td>
                <div>all these nodes should match</div>
            </td>
        </tr>
    </table>
</root>

答案 1 :(得分:0)

试试这个

//table[count(.//li) eq 0]