user10424684
user10424684

Reputation:

How to Ignore element in XPATH

I want to select all a tags thats not inside <div id="not-take-inside">....</div> element.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div>
        <div><a href="#">TEXT</a></div>
        <div><a href="#">TEXT</a></div>
        <div><a href="#">TEXT</a></div>
    </div>
    <div id="not-take-inside">
        <ul>
            <li><a href="#">TEXT</a></li>
            <li><a href="#">TEXT</a></li>
            <li><a href="#">TEXT</a></li>
            <li><a href="#">TEXT</a></li>
            <li><a href="#">TEXT</a></li>
        </ul>
    </div>
    <div>
        <div><a href="#">TEXT</a></div>
        <div><a href="#">TEXT</a></div>
        <div><a href="#">TEXT</a></div>
        <div>
            <div><a href="#">TEXT</a></div>
            <div><a href="#">TEXT</a></div>
            <div><a href="#">TEXT</a></div>
        </div>
    </div>
</body>
</html>

I want to select all a tags thats not inside <div id="not-take-inside">....</div> element.

I try XPATH like //a[parent::*[not(@id='not-take-inside')]] not work.

Upvotes: 0

Views: 1359

Answers (2)

zx485
zx485

Reputation: 29052

You only need to select all as which do not have an ancestor:: attribute node of @id with the content not-take-inside:

//a[not(ancestor::*/@id='not-take-inside')]

There is no need for two predicates.

Upvotes: 1

ABHIJIT
ABHIJIT

Reputation: 689

Simple use //a[not(ancestor::*[@id='not-take-inside'])] :)

Upvotes: 2

Related Questions