开发者

HtmlAgilityPack with XPath - retrieve nodes that doesn't contain  

I'm trying to retrieve a select amount of elements that doesn't contain the value   (a space) using the HtmlAgilityPack in C#. Here's my XPath expression:

"(td)[(position() >= 10 and position() <= last()) and not(.='&nbsp;')]"

but it is still giving me these nodes, I've tried using a literal space, &#160; ALT + 1060 - nothing seems to work. Here is what I'm parsing:

 <tr height=20 style='mso-height-source:userset;height:15.0pt'>
  <td height=20 class=xl96 style='height:15.0pt'>&nbsp;</td>
  <td class=xl97>&nbsp;</td>
  <td class=xl106 style='border-top:none'>JIM COCKS</td>
  <td class=xl107 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl107 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl107 style='border-top:none;border-left:none'>HOL</td>
  <td class=xl76>&nbsp;</td>
  <td class=xl103 style='border-left:none'>&nbsp;</td>
  <td class=xl97>&nbsp;</td>
  <td class=xl104 style='border-top:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&n开发者_JAVA技巧bsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>09:30</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td> 
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>17:00</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl104 style='border-top:none;border-left:none'>&nbsp;</td>
  <td class=xl76>&nbsp;</td>
 </tr>

The items with the class 'xl104' is what I want to grab (I've done this with position statements as their classes change) but I only want nodes that contain something other than &nbsp;, e.g. the 09:30 AND 17:00 you see above.


"(td)[(position() >= 10 and position() <= last()) and not(.='&nbsp;')]" 

not(.='&nbsp;')

tests that the whole text() node is not the string '&nbsp;'.

You want to use the XPath contains() function:

not(contains(., '&#xA0;'))


I'm trying to retrieve a select amount of elements that doesn't contain the value &nbsp;

I believe @Dimitre has answered for that specification of the task.

I only want nodes that contain something other than &nbsp;

A slightly different specification. Does this work? (Edited; thanks to Alejandro.)

"td[position() >= 10 and translate(., '&#xA0;', '') != '']" 

This is equivalent and shorter, but less readable:

"td[position() >= 10 and translate(., '&#xA0;', '')]" 

Anyway, you found the problem so we won't go farther with this.

Do note, though, that using &nbsp; literally in XPath won't normally work unless you define it. This character entity is predefined in HTML but not in XML. That's why &#160; or &#xA0; is more reliable. However, it's possible that the HtmlAgilityPack defines   for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜