Soham
Soham

Reputation: 873

How does this XPATH query differentiate?

I am kind of repeating this question because mostly due to my own ignorance, I could not fully understand the innards.

Given this HTML snippet

<td valign=top class="tim_new">
  <a href="/stocks/company_info/pricechart.php?sc_did=MI42" class="tim_new">3M India</a>
</td>
<td class="tim_new" valign=top>
  <a href='/stocks/marketstats/indcomp.php?optex=NSE&indcode=Diversified' class=tim>Diversified</a>
</td>

How does this XPATH //a[@class='tim_new'] differentiate between line 1 and line 2.

Upvotes: 1

Views: 146

Answers (1)

Graham Clark
Graham Clark

Reputation: 12966

Break down your XPath:

// - This will search anywhere in the XML for a match, instead of looking for an explicit "path".

a - This will match all a elements. Therefore your other elements (td in this case) will be ignored.

[@class='tim_new'] - This will match an attribute called class with a vaule of tim_new.

So all together, your XPath will look everywhere in your input XML (HTML in this case) for an a element which has an attribute class with a value of tim_new.

If you wanted to match the td elements instead, you'd use //td[@class='tim_new'].

Upvotes: 2

Related Questions