Peter Pik
Peter Pik

Reputation: 11193

xpath exclude certain child element with class

I have a html structure which for instance could look like below in a simplified version. I want to exclude the yarpp-related div from the xpath content. Here is what i'm using at the moment:

//div[@class='entry-content']

How can i exlude the yarpp-related div?

html structure

<div class="entry-content">
    <div class="yarpp-related">
    </div>
</div>

Upvotes: 0

Views: 4257

Answers (2)

kjhughes
kjhughes

Reputation: 111491

XPath is for selection, not manipulation. You can select nodes as they exist in an XML document, but you cannot transform those nodes.

In your case, if your XML document includes this node,

<div class="entry-content">
    <div class="yarpp-related">
    </div>
</div>

you can select the entry-content div via //div[@class='entry-content'], but the selected node will appear as it appears in the source XML, that is, with the child yarpp-related div node.

If you want to manipulate or transform a node selected via XPath (to exclude its children elements) you'll have to use the hosting language (XSLT, Python, Java, C#, etc) to manipulate the selection.

Upvotes: 0

Jacob Brown
Jacob Brown

Reputation: 7561

//div[@class='entry-content'][not(contains(div/@class, 'yarpp-related'))]

or

//div[@class='entry-content']/div[not(contains(@class, 'yarpp-related'))]

Upvotes: 3

Related Questions