简体   繁体   中英

PHP & XML: Can xpath include namespaces in the results, not just on the search?

I have an RSS feed that contains namespaces. I need to display the namespace information in the results along with the regular nodes. I'm trying xpath, but am now stuck and can't seem to find an answer that does what I need (or at least one that I understand--they may be answering my question and I don't get it.)

Here's my RSS feed (with a few edits to remove excess nodes):

<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
   <item>
      <title>...</title>
      <link>...</link>
      <description>...</description>
      <author>...</author>
      <pubDate>05/14/2008</pubDate>
         <media:content url="http://www.url.com">
            <media:title>...</media:title>
         </media:content>
   </item>
   <item>
      <title>...</title>
      <link>...</link>
      <description>...</description>
      <author>...</author>
      <pubDate>06/17/2008</pubDate>
         <media:content url="http://www.url.com">
            <media:title>...</media:title>
         </media:content>
   </item>
</channel>

Here's the code thus far:

if (file_exists($filePath)) {
    $items = simplexml_load_file($filePath);
    $items->registerXPathNamespace("media","http://search.yahoo.com/mrss/");
    $items = $items->xpath('//item');
    usort($items, "toSort"); // sorts using an included function

    // output the file
    foreach($items as $item) {
        echo '<div class="grayBx">';
        echo $item->content->attributes()->url;
        echo '<h2>' . $item->title . '</h2>';
        echo '<p>' . $item->description . '</p>';
        echo '<p><a href="' . $item->link . '">read more &gt;&gt;</a></p>';
        echo '</div>';
        echo '<div class="clearBoth">&nbsp;</div>';
    }
}

Right now, as I understand it, xpath changes the simplexml_load_file to an array type and also makes it easier/faster to search in most cases. The problem is, when I use xPath it strips the media: namespace out of the results and the media result is where thumbnails are stored and I need to display the images on the page.

I'm stuck and I'm not sure if this is even the right path anymore. Can anyone help?

You need to fetch the namespaced elements with SimpleXMLElement::children which allows you to pass the namespace:

foreach($items as $item) {
    // media is an array with the media:* children of the item (ie media:content)
    $media = $item->children('http://search.yahoo.com/mrss/');
    echo '<div class="grayBx">';
    echo $media[0]->attributes()->url; // media:content->attrinutes()->url
    echo '<h2>' . $item->title . '</h2>';
    echo '<p>' . $item->description . '</p>';
    echo '<p><a href="' . $item->link . '">read more &gt;&gt;</a></p>';
    echo '</div>';
    echo '<div class="clearBoth">&nbsp;</div>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM