Page 1 of 1

another xpath problem

Posted: Thu Dec 15, 2011 1:41 pm
by kc11
Hi everyone,

I am having trouble getting a specific <li> node using xpath . I want to get the value of the node whose text value cpntains or begins with " temp1: "

The HTML looks like this:

[text]
ul class="temperatures">
<li> temp1: string1 </li>
<li> temp2: string2 </li>
</ul>
[/text]

So far I have:

Code: Select all


$temp1= $simplexml->xpath("//li[contains(text(), 'temp1:')]";

This is not working.

Thank you in advance,

KC

Re: another xpath problem

Posted: Fri Dec 16, 2011 4:39 am
by Weirdan
works for me:

Code: Select all

    <?php
    $s = '<?xml version="1.0"?>
    <ul class="temperatures">
    <li> temp1: string1 </li>
    <li> temp2: string2 </li>
    </ul>
    ';
    
    $xml = simplexml_load_string($s);
    
    var_dump($xml->xpath("//li[contains(text(), 'temp1:')]"));

Code: Select all

array(1) {
  [0]=>
  object(SimpleXMLElement)#2 (1) {
    [0]=>
    string(16) " temp1: string1 "
  }
}

Re: another xpath problem

Posted: Fri Dec 16, 2011 12:38 pm
by kc11
Thanks Weirdan,

I am working with a snippet of HTML from a rss feed which is not utf-8.

So I have been using code like this:

Code: Select all

$doc = new DOMDocument();

  $doc->loadHTML($html);
  $xml = simplexml_import_dom($doc);
var_dump($xml->xpath("//li[contains(text(), 'temp1:')]"));
So I am still getting an 'empty' result.

Do you know of a workaround for less well formed html?

best regards,

KC