somejkuser
somejkuser

Reputation: 9040

xpath retrieve data for first level only

Here is my HTML:

    <html>
        <body>
            <div id="articleBody">
                <p>text</p>
                <p>text2</p>
                <div>
                    <p>text3</p>
                    <p>text4</p>
                </div>
            </div>
        </body>
    </html>

Here is my xpathL

    //div[@id="articleBody"]//p

This works fine, however I don't want any of the p tags within the second div.

How do I accomplish this? Also, I don't want it to be limited to just div elements to exclude.

What I'm really looking for is to query and return only the first level p tags

Upvotes: 3

Views: 1583

Answers (1)

user764357
user764357

Reputation:

It really depends on what you are trying to grab. If its just the p tags under the given div you can just use:

//div[@id="articleBody"]/p

Given that you are using an @id attribute, there really should only be one div with that id, meaning that this will just grab any child p elements of that div.

When you use the x//y notation between two nodes, it grabs all the decendant y nodes from the given node x. You only want the direct children, hence using x/y.

Upvotes: 2

Related Questions