抓取上一个/下一个节点的特定部分 [英] Grabbing a Specific Part of the Previous / Next Nodes

查看:29
本文介绍了抓取上一个/下一个节点的特定部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SimpleXML 遍历 XML 文件并尝试从上一个和下一个中提取特定的数据(特别是 series->displayname 属性)每次都有节点.

以下是 XML 结构的示例:

<漫画><id>117</id><index>1</index><主要部分><pagecount>33</pagecount><系列><displayname>惊人的故事</displayname><sortname>惊人的故事</sortname></系列></mainsection></漫画><漫画><id>102</id><index>1</index><主要部分><pagecount>33</pagecount><系列><displayname>神奇的故事</displayname><sortname>神奇的故事</sortname></系列></mainsection></漫画><漫画><id>85</id><index>1</index><主要部分><pagecount>33</pagecount><系列><displayname>伟大的故事</displayname><sortname>伟大的故事</sortname></系列></mainsection></漫画>

这是我在循环中使用的结构示例:

$prevIssue = $comic->xpath('preceding-sibling::*')->mainsection->series->displayname;$nextIssue = $comic->xpath('following-sibling::*')->mainsection->series->displayname;回声'<li class="issue" id="' . $seriesCat . '"><h3>'.$comicSeriresName .'<div id="详细信息"><span id="publisher">'.$comicPublisher .'</span><br>最后一个系列被称为' . $prevIssue . '<br>下一个系列称为' . $nextIssue . '<br>

';

但是,nextIssue"和prevIssue"变量不起作用.他们返回一个空白结果.我做错了什么?

非常感谢任何指导.

解决方案

试试下面的代码

$prevIssue_arr = $comic->xpath('preceding-sibling::*[1]');$prevIssue = (isset($prevIssue_arr[0])) ?$prevIssue_arr[0]->mainsection->series->displayname : '';$nextIssue_arr = $comic->xpath('following-sibling::*[1]');$nextIssue = (isset($nextIssue_arr[0])) ?$nextIssue_arr[0]->mainsection->series->displayname : '';回声...

[已编辑]

其他人已经解释了为什么代码不起作用

一个是 xpath() 方法返回一个 SimpleXMLElement 对象数组.如果没有找到匹配的节点,则返回一个空数组.

另一个是位置路径中没有谓词[1][position()=1],所有前面或后面的comic 节点将被返回.

或者,您可以使用位置路径 'following-sibling::comic[1]/mainsection/series' 从紧接的后续节点中获取所需的 series 节点兄弟.如果有多个 series 节点,则必须给出位置或返回所有节点.

所以使用这个位置路径

$prevIssue_arr = $comic->xpath('preceding-sibling::comic[1]/mainsection/series');$prevIssue = (isset($prevIssue_arr[0])) ?$prevIssue_arr[0]->显示名称:'';$nextIssue_arr = $comic->xpath('following-sibling::comic[1]/mainsection/series');$nextIssue = (isset($nextIssue_arr[0])) ?$nextIssue_arr[0]->显示名称:'';

I am looping through a XML file with SimpleXML and trying to pull a specific piece of data (specifically, the series->displayname attribute) from the Previous and Next nodes each time.

Here is a sample of the XML structure:

<comic>
      <id>117</id>
      <index>1</index>
      <mainsection>
        <pagecount>33</pagecount>
        <series>
          <displayname>Amazing Story</displayname>
          <sortname>Amazing Story</sortname>
        </series>
      </mainsection>
</comic>
<comic>
      <id>102</id>
      <index>1</index>
      <mainsection>
        <pagecount>33</pagecount>
        <series>
          <displayname>Fantastic Tale</displayname>
          <sortname>Fantastic Tale</sortname>
        </series>
      </mainsection>
</comic>
<comic>
      <id>85</id>
      <index>1</index>
      <mainsection>
        <pagecount>33</pagecount>
        <series>
          <displayname>The Great Tale</displayname>
          <sortname>The Great Tale</sortname>
        </series>
      </mainsection>
</comic>

And here is a sample of the structure I am using in the loop:

$prevIssue = $comic->xpath('preceding-sibling::*')->mainsection->series->displayname;
$nextIssue = $comic->xpath('following-sibling::*')->mainsection->series->displayname;

echo '
<li class="issue" id="' . $seriesCat . '">
<h3>' . $comicSeriresName . '</h3>
   <div id="details">
   <span id="publisher">' . $comicPublisher . '</span><br>
    The last series was called "' . $prevIssue . '<br>
    The next series is called "' . $nextIssue . '<br>
   </div>
</li>'; 

However, the 'nextIssue' and 'prevIssue' variables are not working. They are returning a blank result. What am I doing wrong?

Any guidance is greatly appreciated.

解决方案

Try the below code

$prevIssue_arr = $comic->xpath('preceding-sibling::*[1]');
$prevIssue = (isset($prevIssue_arr[0])) ? $prevIssue_arr[0]->mainsection->series->displayname : '';
$nextIssue_arr = $comic->xpath('following-sibling::*[1]');
$nextIssue = (isset($nextIssue_arr[0])) ? $nextIssue_arr[0]->mainsection->series->displayname : '';

echo ...

[Edited]

The others have already explained why the code did not work

One is that the xpath() method returns an array of SimpleXMLElement objects. If no matching node is found, an empty array is returned.

The other is that without the predicate [1] or [position()=1] in the location path, all the preceding or succeeding comic nodes would be returned.

Alternatively you can use the location path 'following-sibling::comic[1]/mainsection/series' to get just the required series node from the immediate succeeding sibling. If there were multiple series nodes, the position would have to be given or all of them would be returned.

So using this location path

$prevIssue_arr = $comic->xpath('preceding-sibling::comic[1]/mainsection/series');
$prevIssue = (isset($prevIssue_arr[0])) ? $prevIssue_arr[0]->displayname : '';
$nextIssue_arr = $comic->xpath('following-sibling::comic[1]/mainsection/series');
$nextIssue = (isset($nextIssue_arr[0])) ? $nextIssue_arr[0]->displayname : '';

这篇关于抓取上一个/下一个节点的特定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆