Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath expression for containing element of a given text node

Tags:

xml

xpath

I am coding some Perl to use XPath to locate a particular td element within a table` that looks similar to this

  <table>
    <tr>
      <td>...</td>
      <td><font color="white" face="verdana, Helvetica, Arial" size="2">Showing <b>1</b>-<b>100</b> of <b>200</b> total</font></td>
      <td>...</td>
      <td>...</td>
    </tr>
  </table>

What I want is to find a td element that has a font/text() node that contains the string Showing.

matches contains(., "Showing")

A direct comparison works fine:

//td[font/text()="Showing "]

but I want to use the contains() XPath function so that the match is more flexible.

I have tried

//td[contains(font/text(), "Showing ")]

but this raises the error

XPath failed due to: A sequence of more than one item is not allowed as the first argument of contains()

and I have managed to achieve what I want with

//td[font/text()[contains(., "Showing")]]

but this is very ugly and I am hoping for something more concise. Please can someone improve on this for me, or perhaps confirm that this is the best and most concise way?

like image 963
Borodin Avatar asked Jun 15 '12 08:06

Borodin


1 Answers

Try this:

//td[contains(font/text()[1], 'Showing ')]
like image 103
Kirill Polishchuk Avatar answered Sep 20 '22 00:09

Kirill Polishchuk