Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nokogiri: why is this an invalid xpath?

Tags:

xpath

nokogiri

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

i am getting invalid xpath expression with nokogiri

like image 992
Jaaaaabbb Avatar asked Oct 11 '09 17:10

Jaaaaabbb


Video Answer


1 Answers

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.

enter image description here

like image 187
Cheeso Avatar answered Jan 03 '23 00:01

Cheeso