[英]PHP SimpleXML XPath get next parent node
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.
我只是一个讨厌的人,对PHP和XPath知之甚少。
I have an XML file like this: 我有一个像这样的XML文件:
<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: 我使用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: 然后我使用URL参数的引用来获取有关使用PHP的'item'的额外细节:
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 所以来自www.mysite.com/file.php?reference=A1这样的网址
I would get this HTML: 我会得到这个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'?
如果我从URL知道引用= A1,我如何找到下一个“项目”的引用,标题等? If I only have 'A1' and I know that's a reference node, how do I get HTML like this:
如果我只有'A1'并且我知道这是一个参考节点,我该如何得到这样的HTML:
<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. 任何帮助赞赏。
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. 含义: 从
catalogue
根元素的item
元素子元素,具有带有'A1'字符串值的reference
元素,navegate到第一个跟随sibling item
元素的title
子元素。
I´d probably use xpath to fetch the next/previous (and current) node. 我可能会使用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 />';
}
}
See the documentation of SimpleXMLElement::xpath
and XPath syntax documentation. 请参阅
SimpleXMLElement::xpath
和XPath语法文档的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.