PHP SimpleXML XPath 获取下一个父节点

发布于 2024-09-16 16:21:32 字数 1182 浏览 4 评论 0原文

我以前从未在这里问过问题,所以如果我的问题格式不好或不够具体,请原谅我的问题。我只是一个浅尝辄止的人,对 PHP 和 XPath 知之甚少。

我有一个像这样的 XML 文件:

<catalogue>
 <item>
  <reference>A1</reference>
  <title>My title1</title>
 </item>
 <item>
  <reference>A2</reference>
  <title>My title2</title>
 </item>
</catalogue>

我使用 SimpleXML 提取该文件:

$file = "products.xml";
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");

然后我使用 URL 参数中的引用来使用 PHP 获取有关“项目”的额外详细信息:

foreach ($xml->item as $item) {
if ($item->reference == $_GET['reference']) {
 echo '<p>' . $item->title . '</p>';
}

所以从像 www.mysite.com/file 这样的 URL .php?reference=A1

我会得到这个 HTML:

<p>My title1</p>

我意识到我可能做得不对,欢迎任何改进这一点的指示。

我的问题是,我想找到下一个和上一个“项目”的详细信息。如果我从 URL 知道引用=A1,我如何找到下一个“项目”的引用、标题等?如果我只有“A1”并且我知道这是一个引用节点,我如何获得这样的 HTML:

<p>Next item is My title2</p>

我已经阅读了有关 follow-sibling 的内容,但我不知道如何使用它。我只能找到参考节点的以下兄弟节点,这不是我需要的。

任何帮助表示赞赏。

I've never asked a question here before so please forgive my question if its formatted badly or not specific enough. I am just a dabbler and know very little about PHP and XPath.

I have an XML file like this:

<catalogue>
 <item>
  <reference>A1</reference>
  <title>My title1</title>
 </item>
 <item>
  <reference>A2</reference>
  <title>My title2</title>
 </item>
</catalogue>

I am pulling this file using SimpleXML:

$file = "products.xml";
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");

Then I am using the reference from a URL parameter to get extra details about the 'item' using PHP:

foreach ($xml->item as $item) {
if ($item->reference == $_GET['reference']) {
 echo '<p>' . $item->title . '</p>';
}

So from a URL like www.mysite.com/file.php?reference=A1

I would get this HTML:

<p>My title1</p>

I realise I might not be doing this right and any pointers to improving this are welcome.

My question is, I want to find the next and previous 'item' details. If I know from the URL that reference=A1, how do I find the reference, title etc of the next 'item'? If I only have 'A1' and I know that's a reference node, how do I get HTML like this:

<p>Next item is My title2</p>

I have read about following-sibling but I don't know how to use it. I can only find the following-sibling of the reference node, which isn't what I need.

Any help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぃ弥猫深巷。 2024-09-23 16:21:33

您可以使用:

/catalogue/item[reference='A1']/following-sibling::item[1]/title

含义:来自 catalogue 根元素的 item 元素子元素,具有带有“A1”字符串值的 reference 元素,导航到第一个同级 item 元素的 title 子元素。

You could use:

/catalogue/item[reference='A1']/following-sibling::item[1]/title

Meaning: from an item element child of catalogue root element, having a reference element with 'A1' string value, navegate to first following sibling item element's title child.

初相遇 2024-09-23 16:21:33

我可能会使用 xpath 来获取下一个/上一个(和当前)节点。

<?php

error_reporting(E_ALL ^ E_NOTICE);

$s = '
<catalogue>
 <item>
  <reference>A1</reference>
  <title>My title1</title>
 </item>
 <item>
  <reference>A2</reference>
  <title>My title2</title>
 </item>
 <item>
  <reference>A3</reference>
  <title>My title3</title>
 </item>
</catalogue>
';

$xml = simplexml_load_string($s);
$reference = 'A3';

list($current) = $xml->xpath('/catalogue/item[reference="' . $reference . '"]');

if($current) {
    print 'current: ' . $current->title . '<br />';

    list($prev) = $current->xpath('preceding-sibling::*[1]');

    if($prev) {
        print 'prev: ' . $prev->title . '<br />';   
    }

    list($next) = $current->xpath('following-sibling::*[1]');

    if($next) {
        print 'next: ' . $next->title . '<br />';   
    }
}

请参阅 SimpleXMLElement::xpath 的文档XPath 语法 文档。

I´d probably use xpath to fetch the next/previous (and current) node.

<?php

error_reporting(E_ALL ^ E_NOTICE);

$s = '
<catalogue>
 <item>
  <reference>A1</reference>
  <title>My title1</title>
 </item>
 <item>
  <reference>A2</reference>
  <title>My title2</title>
 </item>
 <item>
  <reference>A3</reference>
  <title>My title3</title>
 </item>
</catalogue>
';

$xml = simplexml_load_string($s);
$reference = 'A3';

list($current) = $xml->xpath('/catalogue/item[reference="' . $reference . '"]');

if($current) {
    print 'current: ' . $current->title . '<br />';

    list($prev) = $current->xpath('preceding-sibling::*[1]');

    if($prev) {
        print 'prev: ' . $prev->title . '<br />';   
    }

    list($next) = $current->xpath('following-sibling::*[1]');

    if($next) {
        print 'next: ' . $next->title . '<br />';   
    }
}

See the documentation of SimpleXMLElement::xpath and XPath syntax documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文