XPath for elements with content inside?

c# html html-agility-pack xml xpath

Question

I'm using HtmlAgilityPack and I have the following situation:

<table class='table-main odds '>   
   <tbody>
      <tr>..</tr>
      <tr>..</tr>
      <tr>..</tr>
      <tr></tr>
      <tr></tr>
  </tbody>
</table>          

as you can see only three tr have content inside, so I want that in the final result should be available only the first three tr. Actually my code return all the tr:

 HtmlNode oddsTable = doc.DocumentNode
          .SelectSingleNode("//table[starts-with(@class, 'table-main')]");
 HtmlNodeCollection rows = oddsTable.SelectNodes("tbody//tr");

how can I achieve that using xpath?

Thanks for any help and explaination.

Accepted Answer

Depends on what you mean by content:

  • Any content: //tr[node()]
  • Element content: //tr[*]
  • Text content: //tr[text()]
  • Non-whitespace content: //tr[normalize-space()]


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why