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'];
drbi19093
drbi19093 让我们在聊天中继续讨论
6 年多之前 回复
duanlang1531
duanlang1531 $href=$entry[0]->xpath('link[@rel=“alternate”]/@href');的print_r($HREF);返回一个空数组。
6 年多之前 回复
dongtuo3370
dongtuo3370 嗯,对不起,混淆了我的DOM和SimpleXML:我的意思是在$entry[0]上使用你->xpath()而不是$entry。
6 年多之前 回复
duanmei2805
duanmei2805 它返回对未定义方法SimpleXMLElement::query()的调用。当我print_r($entry[0])(或没有[0])时,它返回条目:wildupdates.com/test/test.php
6 年多之前 回复
douhu2525
douhu2525 嗯,$entry[0]->query()做什么?
6 年多之前 回复
dpauxqt1281
dpauxqt1281 我正在使用PHP5.3。link确实返回一个空数组。但我可以通过带有$entry->title的simpleXML获得标题。所以我必须在入境权利?
6 年多之前 回复
dongshanni1611
dongshanni1611 顺便说一句:如果链接找不到任何东西......你确定你在一个入口节点吗?
6 年多之前 回复
dongrenshi0889
dongrenshi0889 属性不是text()节点,因此(string)$entry->xpath('link[@rel=“alternate”]/@href')[0]适合我。[0]需要php>=5.4BTW。
6 年多之前 回复

2个回答



因为它来自聊天对话来自我原来的问题我必须注册命名空间。 最后我使用了这个 网站和代码结果如下:</ p>

  $ feed = new DOMDocument(); 
$ feed-&gt; load(“http:/ /www.wildstarfans.net/feeds/posts/default");
nnxpath = new DOMXPath($ feed);
$ xpath-&gt; registerNamespace('atom','http://www.w3 .org / 2005 / Atom');

foreach($ xpath-&gt; evaluate('// atom:entry')as $ entry){

$ href = $ xpath-&gt; evaluate('string (atom:link [@ rel =“alternate”] / @ href)',$ entry);

}
</ code> </ pre>

Credits转到 ThW Wrikken 。 希望我能为你们提供SO点数。</ p>
</ div>

展开原文

原文

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-&gt; xpath('link [@ rel =“alternate”]'); 
$ href =(string)$ href [0] - &gt; attributes() - &gt; href;
</ code> </ pre>
</ div>

展开原文

原文

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

douwulu2576
douwulu2576 不确定。 此代码适用于您提供的XML。
6 年多之前 回复
dongqiao1964
dongqiao1964 第一行返回一个空数组,导致第2行出错:在非对象上调用成员函数attributes()。 * [@ rel =“alternate”]似乎确实有用,但这并不是整齐的编码。 我在这里错过了什么?
6 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐