Imme22009
Imme22009

Reputation: 4129

Xpath - Select all excluding subnode with attribute

I have a bunch of <div class="post"> nodes, and i need to select the contents of each, whilst excluding a subnode called <div class="quote">.

Example:

<div class="post">
<div class="quote"><a href="{url}">some text</div>
some content<br>more content
</div>

In this example, select "some content<br>more content" and exclude <div class="quote">..</div>

I tried this, doesn't work:

@doc.xpath("//div[@class='post'][not(self::div)]")

Upvotes: 1

Views: 1217

Answers (1)

Jens Erat
Jens Erat

Reputation: 38682

Your XPath expression is selecting all <div/> elements which are no <div/> element, thus it will never return any value. You need an axis step after selecting the "post"-<div/>.

Select all subnodes, but exclude those with local name (omitting namespaces) "div" and class attribute "quoe".

//div[@class='post']/node()[not(local-name() = 'div' and @class='quote')]

You could also use self::div instead of the local name test.

Upvotes: 2

Related Questions