Racoon Posted February 24, 2010 Share Posted February 24, 2010 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']")); ?> Quote Link to comment https://forums.phpfreaks.com/topic/193163-need-help-writing-a-simplexml-xpath-expression-correctly/ Share on other sites More sharing options...
monkeytooth Posted February 24, 2010 Share Posted February 24, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193163-need-help-writing-a-simplexml-xpath-expression-correctly/#findComment-1017194 Share on other sites More sharing options...
Racoon Posted February 24, 2010 Author Share Posted February 24, 2010 Thanks monkeytooth, I appreciate your contribution. I'm afraid your code is too different from my scenario right now, but I'll keep it as a fragment for eventual future use. Quote Link to comment https://forums.phpfreaks.com/topic/193163-need-help-writing-a-simplexml-xpath-expression-correctly/#findComment-1017540 Share on other sites More sharing options...
salathe Posted February 24, 2010 Share Posted February 24, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193163-need-help-writing-a-simplexml-xpath-expression-correctly/#findComment-1017628 Share on other sites More sharing options...
Racoon Posted February 24, 2010 Author Share Posted February 24, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193163-need-help-writing-a-simplexml-xpath-expression-correctly/#findComment-1017667 Share on other sites More sharing options...
salathe Posted February 24, 2010 Share Posted February 24, 2010 Go for it and see what happens. I haven't installed PHP on Windows for 5 or 6 years so won't be of much use if it doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/193163-need-help-writing-a-simplexml-xpath-expression-correctly/#findComment-1017698 Share on other sites More sharing options...
Racoon Posted February 25, 2010 Author Share Posted February 25, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193163-need-help-writing-a-simplexml-xpath-expression-correctly/#findComment-1017815 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.