Search code examples
phpxpathsimplexmlparentsiblings

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.

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.


Solution

  • 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.