PHP中的Xpath查询从具有特定属性值的元素进行属性

PHP中的Xpath查询从具有特定属性值的元素进行属性

问题描述:

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>noreply@blogger.com</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'];

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.

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