nokogiri: why is this an invalid xpath?

余生长醉 提交于 2019-12-08 19:46:25

问题


//br/preceding-sibling::normalize-space(text())

i am getting invalid xpath expression with nokogiri


回答1:


normalize-space is a function. You can't use it there.
You need a node-set.

maybe you mean

//br/preceding-sibling::*

or you could use normalize-space in a predicate, inside square brackets. Think of the predicate as a filter or selector on the node-set. So you can do this:

//br/preceding-sibling::*[normalize-space()='Fred']

In English that translates to "all elements preceding <br> in the document, and for which the (normalized) text is 'Fred' ". In this document:

<html>
  <p>
    <h2>Fred</h2>
    <br/>
  </p>
  <table>
    <tr>
      <td>
        <br/>
      </td>
    </tr>
  </table>
</html>

...the xpath expression selects the <h2> node.

I figured this out with the free XpathVisualizer tool available on codeplex.



来源:https://stackoverflow.com/questions/1551192/nokogiri-why-is-this-an-invalid-xpath

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