Search code examples
c#htmlxmlxpathhtml-agility-pack

XPath for elements with content inside?


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.


Solution

  • Depends on what you mean by content:

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