Marx
Marx

Reputation: 71

XPath exclude given class

I'm trying to extract text from a div but excluding a given class:

This is what i'm trying:

$pattern = "//div/@title[not(contains (@class, 'second_card local_impact_icon impact-2'))]";

but its not excluding the given class, i need to extract just the text of title='' but just from the first div title.

This is the html:

<div class="match_info"><div title='Yellow Card' class='local_impact_icon impact-1'></div><div title='Red Card' class='second_card local_impact_icon impact-2'></div></div>

Upvotes: 0

Views: 2128

Answers (1)

matthias_h
matthias_h

Reputation: 11416

Following XPath

//div/div[not(contains (@class, 'second_card local_impact_icon impact-2'))]/@title

returns

title="Yellow Card"

Simplified explanation - just select the div that doesn't contain the class you want to exclude and retrieve the title attribute for this div only. When you set this exclude at the position ../@title you already are at the title-attributes of both divs.

And as the question is how to retrieve the text - in given example

string(//div/div[not(contains (@class, 'second_card local_impact_icon impact-2'))]/@title)  

returns Yellow Card

Upvotes: 2

Related Questions