Jump to content

Need help writing a SimpleXML Xpath expression correctly


Recommended Posts

I'm trying to select a node using a relative path and I cannot make it work although some say I have it right:

http://stackoverflow.com/questions/2315992/why-cant-i-select-the-desired-node-using-this-php-xpath-expression.

 

I want to select the title node that is immediately below the website node and whose lang attribute is equal to "fr".

Using Windows 2000 PC, Apache 2.2.14, PHP 5.0.5 with SimpleXML Revision: 1.139.2.4. Warning: newbie.

On my machine, the code below outputs: array(0) { }  Any ideas will be greatly appreciated. Cheers!

 

<?php
$string = <<<XML
<root>
<website>
	<title lang="en">My fancy website</title>
	<title lang="fr">Mon site web</title>
	<section>
		<title lang="en">Products section</title>
		<title lang="fr">Section Produits</title>
	</section>
</website>
</root>
XML;
$xmlObject = simplexml_load_string($string);
var_dump($xmlObject->website->xpath("title[@lang='fr']"));
?>

Here is something I did a while back for a client of mine who wished to have some RSS feeds from monster.com on there site. This will work for what I built it for. I'm in the middle of my own project at the moment to break this apart and comment heavily on it, but if you pop this into a php file, you could get an idea for how it works.

 

$feedURL = 'http://rss.jobsearch.monster.com/rssquery.ashx?WT.mc_n=RSS2005_jsr&q=Sales&cy=US&baseurl=jobsearch.monster.com';
$doc = new DOMDocument();
$doc->load($feedURL);
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array ( 
   'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
   'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
   'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
   'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
);
array_push($arrFeeds, $itemRSS);
//print_r($arrFeeds);
}
array_unique($arrFeeds);

foreach($arrFeeds as $arrItem){
   $XBurl = $arrItem['link'];
   $params = getUrlValues($XBurl);
   $jobzID = $params['JobID'];

if($classIt == "jobsRA"){$classIt = "jobsRB";}elseif($classIt == "jobsRB"){$classIt = "jobsRA";}else{$classIt = "jobsRA";}
$jobTitle = $arrItem['title'];
$jobDesc = $arrItem['desc'];
$jobURL = "http://jobview.monster.com/getjob.aspx?JobID=" . $jobzID;
echo "<div class=\"".$classIt."\"><strong><a href=\"".$jobURL."\" target=\"_blank\">".$jobTitle."</a></strong><br />".$jobDesc."...<br /></div>";
}

 

Live examples of this in action are:

 

http://originalglbtexpo.com/ first one is about 4 blocks down on the right side of the page "365 Gay Newswire" another example same site can be found at

http://originalglbtexpo.com/index.php?ref=jobop select any one of the links on that page. The code above is from one of the links found on the second link I gave you.

 

I know this isnt the best conduct for the forum here we are usually here to try and help one another learn rather than copy and past but.. Many of months ago when I did this project the first time I was stuck in a similar boat as you. Trying to make it work from all my searches on google, and posting like a mad man on forums every where unfortunately I didnt get much help and ended up spending a day or 2 piecing the above together. Its not the cleanest code, and I do intend on cleaning it up one day but todays not that day.. Again hope it helps you out.. enjoy.

Racoon, I replied to your SO question after compiling PHP 5.0.5 myself to check things out. SimpleXML XPath is a bit whacky in that old version and much, much improved in later versions of PHP. :shy:

Thanks salathe! You concur with other experimented users (on StackOverflow for example) who told me the same thing -- better to use an uppdated version of PHP. I have downloaded the latest version (5.3) for Windows. In your opinion, should I just run the installer, and let it work its magic? I've never updated PHP before.

Will keep you all posted! Thanks again.

SOLVED :)

Upgrading to the latest version of PHP solved the issue!

I completely uninstalled PHP and Apache from my system and instead used Uniform Server - a portable PHP/Apache environment (http://uniformserver.com/)

Thanks to all for your help.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.