PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
kc11
Forum Commoner
Posts: 73 Joined: Mon Sep 27, 2010 3:26 pm
Post
by kc11 » Thu Dec 15, 2011 1:41 pm
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
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Fri Dec 16, 2011 4:39 am
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 "
}
}
kc11
Forum Commoner
Posts: 73 Joined: Mon Sep 27, 2010 3:26 pm
Post
by kc11 » Fri Dec 16, 2011 12:38 pm
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