MaylorTaylor
MaylorTaylor

Reputation: 5051

Xpath and tables

This is my first time using XPath queries. I get how it works but I just cant seem to obtain the correct information from this table. I am blaming it on a poorly designed table.

Xpath path for the table in question -- /table[1]/tbody[1]/tr[1]

That path gives me the following information.

  <tr>        
    <td style="vertical-align:top">          
    <table style="white-space:nowrap">            
    <tbody>
    <tr>              
      <td style="white-space:nowrap">               
          <span style="font-weight:bold">Norman White</span></td></tr>

 <tr>  
   <td style="white-space:nowrap"> 
     <span style="font-weight:bold">Is Married: </span>Yes</td></tr> <tr>             
   <td style="white-space:nowrap">  
     <span style="font-weight:bold">DOB: </span>birthdate</td></tr><tr> 
 <td style="white-space:nowrap"> 
 <div>     
     <span style="font-weight:bold">SSN: </span>XXX-XX-xxxx</div></td></tr</tbody>table></td>    

    <td style="width:20px">
    <td style="vertical-align:top">
        <table style="white-space:nowrap">
<tbody>
  <tr>  
     <td style="white-space:nowrap"> 
     <span style="font-weight:bold">Lynnea White</span></td></tr> 
 <tr>   
     <td style="white-space:nowrap">
<span style="font-weight:bold">Is Married: </span>Yes</td></tr>
 <tr> 
    <td style="white-space:nowrap">
  <span style="font-weight:bold">DOB: </span>birthdate</td></tr>
<tr> 
 <td style="white-space:nowrap"> 
 <div>  
 <span style="font-weight:bold">SSN: </span>XXX-XX-xxxx</div></td></tr></tbody></table>        </td>        
  <td style="width:20px">      </td></td></tr>  

I am trying to extract just the two names. If i use table.SelectSingleNode("//table/tbody/tr/td//span").InnerText, where table is an HTMLNODE containing the code above as InnerHTML, then i get the first name "Norman White", but i can't seem to get the second name in any way.

Upvotes: 0

Views: 261

Answers (1)

Robin
Robin

Reputation: 9644

Have you considered using SelectNodes instead of SelectSingleNode? :)
(SelectNodes vs SelectSingleNode)

Also, your Xpath may be clearer with something like //table/tbody/tr[1]//span. If you don't select the first tr, it will select all following ones.

Upvotes: 1

Related Questions