xpath that exclude some specific elements

随声附和 提交于 2020-01-24 22:10:26

问题


This is a simple version of the HTML of the page that I want analyse:

<table class="class_1">
  <tbody>
    <tr class="class_2">
      <td class="class_3">&nbsp;</td>
      <td class="class_4">&nbsp;</td>
      <td class="class_5">&nbsp;</td>
    </tr>
    <tr class="class_2">
      <td class="class_3">&nbsp;</td>
      <td class="class_4">&nbsp;</td>
      <td class="class_5"><span class="class_6"></span>square</td>
    </tr>
    <tr class="class_2">
      <td class="class_3">&nbsp;</td>
      <td class="class_4">&nbsp;</td>
      <td class="class_5"><span class="class_7"></span>circle</td>
    </tr>
    <tr class="class_2">
      <td class="class_3">&nbsp;</td>
      <td class="class_4">&nbsp;</td>
      <td class="class_5"><span class="class_6"></span>triangle</td>
    </tr>
  </tbody>
</table>

You can find the page at https://sabbiobet.netsons.org/test.html

If you try in a google sheets the function:

=IMPORTXML("https://sabbiobet.netsons.org/test.html";"//td[@class='class_5']")

i'll obtain:

    • square
  • circle
  • triangle

I need to obtain all the <td> with class="class_5" minus the ones that have &nbsp; or <span class=class_7>.

In other words I want to obtain only these values:

  • Square
  • Triangle

can somebody help me?


回答1:


The following XPath expression

//td[@class='class_5' and span and not(span[@class='class_7'])]

selects all td elements having an attribute class with value class_5, having a child element span and not having a child element span where its class attribute has the value class_7.

Note that you could also use

//td[@class='class_5' and span[@class='class_6']]

to get the same result in this case.




回答2:


This should work:

//td[@class='class_5'][not(text()=' ')][not(./span[@class='class_7'])]

where [not(text()=' ')] is not testing for a reqular space but rather for a symbol with Unicode code U+00A0 that you can input from keyboard in windows using alt+0160 where numbers are to be input from numpad.



来源:https://stackoverflow.com/questions/58874244/xpath-that-exclude-some-specific-elements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!