tags:

views:

261

answers:

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

i am getting invalid xpath expression with nokogiri

+1  A: 

normalize-space is a function.
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 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.

Cheeso