0

I'm truly bending my head over something that should be way to simple. I have an XML feed with 25 entries in the root. I'm already iterating them as $entry in PHP.

Here is an example of one entry in the xml feed:

<entry>
      <id>tag:blogger.com,1999:blog-7691515427771054332.post-4593968385603307594</id>
      <published>2014-02-10T06:33:00.000-05:00</published>
      <updated>2014-02-10T06:40:34.678-05:00</updated>
      <category scheme="http://www.blogger.com/atom/ns#" term="Aurin" />
      <category scheme="http://www.blogger.com/atom/ns#" term="fan art" />
      <category scheme="http://www.blogger.com/atom/ns#" term="Fred-H" />
      <category scheme="http://www.blogger.com/atom/ns#" term="spellslinger" />
      <category scheme="http://www.blogger.com/atom/ns#" term="wildstar" />
      <title type="text">Fan Art Showcase: She's gunnin' for trouble!</title>
      <content type="html">Some random content</content>
      <link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7691515427771054332/posts/default/4593968385603307594" />
      <link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7691515427771054332/posts/default/4593968385603307594" />
      <link rel="alternate" type="text/html" href="http://www.wildstarfans.net/2014/02/fan-art-showcase-shes-gunnin-for-trouble.html" title="Fan Art Showcase: She's gunnin' for trouble!" />
      <author>
         <name>Name Removed</name>
         <uri>URL removed</uri>
         <email>[email protected]</email>
         <gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-ow-dvUDbNxI/AAAAAAAAAAI/AAAAAAAABTY/MhrybgagMv0/s512-c/photo.jpg" />
      </author>
      <media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-Ifp6awhDJuU/UWQEUl8nhUI/AAAAAAAABss/BSZ_YYM1U38/s72-c/fan-art-header.png" height="72" width="72" />
</entry>

I want to get the href of the third link with rel set to alternate. The alternate link isn't always the third one. I know how to do this through SimpleXML, but I want to get to know xpath for this, because through simpleXML it's more complicated and with this I hope I'm one step closer to understanding complex xpath queries.

The PHP I got that makes the most sense to me is:

$href = $entry->xpath('link[@rel="alternate"]/@href');

I tried multiple queries based on the information I found, but they all resulted in nothing. Here is a list of the queries I tried:

  • $href = $entry->xpath('link[@rel="alternate"]/@href/text()');
  • $href = $entry->xpath('link[@rel="alternate"]')->getAttributes()->href;
  • $href = $entry->xpath('*[@rel="alternate"]'); $href = $href['href'];
8
  • An attribute is not a text() node, so (string)$entry->xpath('link[@rel="alternate"]/@href')[0] works for me. The [0] requires php >= 5.4 BTW. Commented Feb 11, 2014 at 19:31
  • BTW: if link does not find anything... are you sure you are in an entry node? Commented Feb 11, 2014 at 19:35
  • I'm working with PHP 5.3. link returns an empty array indeed. But I can get the title through simpleXML with $entry->title. So I must be in the entry right? Commented Feb 11, 2014 at 19:40
  • Hm, what does $entry[0]->query() do? Commented Feb 11, 2014 at 19:55
  • It returns Call to undefined method SimpleXMLElement::query(). When I print_r($entry[0]) (or without the [0]) it returns the entry: wildupdates.com/test/test.php Commented Feb 11, 2014 at 20:06

2 Answers 2

2

As it turns out from the chat conversation from my original question I had to register the namespace. In the end I used this website and the code turned out to be like this:

$feed = new DOMDocument();
$feed->load("http://www.wildstarfans.net/feeds/posts/default");

$xpath = new DOMXPath($feed);
$xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');

foreach ($xpath->evaluate('//atom:entry') as $entry) {

    $href = $xpath->evaluate('string(atom:link[@rel="alternate"]/@href)', $entry);

}

Credits go to ThW and Wrikken. Wish I could give you guys SO points for this.

Sign up to request clarification or add additional context in comments.

Comments

0
$href = $entry->xpath('link[@rel="alternate"]');
$href = (string) $href[0]->attributes()->href;

1 Comment

The first line returns an empty array, resulting in an error for line 2: Call to a member function attributes() on a non-object. *[@rel="alternate"] does seem to work, but that's not tidy coding. What am I missing here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.