Ron
Ron

Reputation: 287

XPath Syntax - unknown element value

I'm using XPath to extract a table from HTML. I have a unique situation where I only know half of my elements attribute value and am trying to write an xpath expression to find the half I know and ignore the rest. For example

HtmlNodeCollection cols = doc.DocumentNode.SelectNodes("//td[@class='Iknowthis_DontKnowThis']");

After looking w3 schools I see that there are wild card operators for selecting unknown nodes but I cant find anything that says how to use them in a situation like this.

Ive tried something like this but cant get anything to work:

doc.DocumentNode.SelectNodes("//td[@class='Iknowthis_.*']");

Maybe its not possible? I'm not sure?

Upvotes: 3

Views: 1262

Answers (2)

MattH
MattH

Reputation: 38245

I've been over the w3schools xpath docs several times, I don't think you can use wildcards for substring matching (while testing for equality in predicates).

You could use some of the string functions string functions. E.g.:

"//td[starts-with(@class,'Iknowthis_')]"

You may need to prefix the starts-with function with a namespace, (e.g. fn:starts-with), it depends on the implementation.

There is also a matches string function which takes patterns.

Upvotes: 1

thezar
thezar

Reputation: 1298

may be you can use function 'contains()':

//td[contains(@class, 'Iknowthis')]

also you can use function 'not()' to exclude smth:

//td[not(contains(@class, 'Iknowthis'))]

Upvotes: 0

Related Questions