#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    197
    Rep Power
    11

    Finding the parent node from the child namespace in RSS Feed


    I've been at this for days and I think I almost have it, but I'm missing a huge piece. Here's the sample RSS feed.

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:uoc="http://pathhere.com">
    <channel>
    <title>News Feed</title>
    <link>http://www.mylink.com</link>
    <description>News.</description>
    <language>en-us</language>
    <lastBuildDate>Wed, 12 Dec 2012 10:53:22 -0500</lastBuildDate>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <generator>Generator text</generator>
    
    <item>
    <title>Story 1</title>
    <link>http://www.mylink.com/story1.php</link>
    <description>Description Here</description>
    <author>Jane Doe</author>
    <pubDate>Fri, 7 Dec 2012 17:48:46 -0500</pubDate>
    <uoc:category>Academics,Campus,Current Students,Faculty,Scholarship</uoc:category>
        <uoc:tags>story, sample, find me</uoc:tags>
        <uoc:featured>no</uoc:featured>
    </item>
    
    <item>
    <title>Story 2</title>
    <link>http://www.mylink.com/story2.php</link>
    <description>Description Here</description>
    <author>John Doe</author>
    <pubDate>Fri, 7 Dec 2012 17:48:46 -0500</pubDate>
    <uoc:category>Academics,Campus,Current Students</uoc:category>
        <uoc:tags>story, sample, find me</uoc:tags>
        <uoc:featured>no</uoc:featured>
    </item>
    
    <item>
    <title>Story 3</title>
    <link>http://www.mylink.com/story3.php</link>
    <description>Description Here</description>
    <author>John Doe</author>
    <pubDate>Fri, 7 Dec 2012 17:48:46 -0500</pubDate>
    <uoc:category>Faculty,Music,Staff</uoc:category>
        <uoc:tags>story, sample, find me</uoc:tags>
        <uoc:featured>no</uoc:featured>
    </item>
    </channel>
    </rss>
    I need to find every story where the uoc:category node contains Faculty and/or Staff. So far I have

    Code:
    if (!isset($filePath)) $filePath = '/pathto/rss/news.xml';
    if (file_exists($filePath)) {
    	// read the file
    	$items = simplexml_load_file($filePath);
    	$stories = $items->xpath('//item/uoc:category/text()[contains(.,"Faculty")]');
    	var_dump($stories);
    }
    The successfully pulls the entries, but it only contains the uoc:category node and not the parent and sibling information. I suspect I'm approaching this in the completely wrong way or that there is one extra step I missed, but at this point I'm lost. Help?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    197
    Rep Power
    11
    I'm closer now. I figured out how to search using the xpath:

    Code:
    $items = $items->xpath('//item[uoc:category[contains(.,"Faculty")]]
    So now, the code is

    Code:
    if (file_exists($filePath)) {
    		// read the file
    		$items = simplexml_load_file($filePath);
    		$items = $items->xpath('//item[uoc:category[contains(.,"Faculty")]]|item[uoc:category[contains(.,"Staff")]]');
    		usort($items, " sortPubdate"); // to included function 
    		foreach ($items AS $story) {
    			echo '<h3>' . $story->title . '</h3>';
    			echo '<p>' . $story->description . '</p>';
    		}
    	}
    The last step I need to figure out is how to get the namespace to be part of the $items array created by xpath. Research so far shows that it's not possible, but that's in articles from 2007. Any updated articles that show otherwise?

IMN logo majestic logo threadwatch logo seochat tools logo