-2

I have a xml file like this :

<rss version="2.0" xmlns:atom="https://www.w3.org/2005/Atom">
<channel>
<item>
    <city>London</city>
    <description>Trip</description>
    <link>page.php</link>
    <img>img.jpg</img>
</item>
<item>
    <city>London</city>
    <description>Trip</description>
    <link>page.php</link>
    <img>img.jpg</img>
</item>
<item>
    <city>Paris</city>
    <description>Trip</description>
    <link>page.php</link>
    <img>img.jpg</img>
</item>
.
.
</channel>
</rss>

If I want to select TRIP in LONDON, I do that :

<?php
$xml   = simplexml_load_file('file.xml');
$items = $xml->xpath('//item[city[contains(.,"London")] and description[contains(.,"Trip")]]');
foreach($items as $item){
echo ' txt ';
}
?>

If I want to select ONLY the first TRIP in LONDON, I do that :

<?php
$xml   = simplexml_load_file('file.xml');
$items = $xml->xpath('//item[city[contains(.,"London")] and description[contains(.,"Trip")]]')[0];
foreach($items as $item){
echo ' txt ';
}
?>

I try also 1 instead of 0, and this

[position()=0]

it does not work.

What's wrong ?

I keep looking. I have made several tests only with the position filter, for example :

<?php  
$xml   = simplexml_load_file('file.xml');
$items = $xml->xpath('//(/item)[1]');
foreach($xml->channel->item as $item){
echo '<div>....</div>';
}
?>

And it doesn't work.

I think I have a problem with this part, but I don't see where.

Jm2t
  • 19
  • 4
  • What exactly is the question here? You are only selecting items that contain `London` to begin with, so how exactly is this not working as it should? – CBroe Feb 16 '23 at 08:02
  • Is there anything you struggle with ? If it's about basic XPath syntax you should just read about it like in [PHP documentation for SimpleXMLElement::xpath()](https://www.php.net/manual/en/simplexmlelement.xpath.php) or [cheatsheets](https://devhints.io/xpath). – AymDev Feb 16 '23 at 08:05
  • Thank you for your comments. Excuse me if it was not clear... In my example, I search "Trip" in "London". And it's working. I just want to write a title before the results A command like : If there is "Trip" in "London" so write '

    Trip in London

    '
    – Jm2t Feb 16 '23 at 08:27
  • I corrected my question :) – Jm2t Feb 16 '23 at 10:19
  • First, you sample xml isn't well formed, please fix it. Second, please edit the question (not in a comment) and show your **exact** expected output. – Jack Fleeting Feb 16 '23 at 13:02
  • Ok I think my problem was misrepresented. I corrected my question again. I'm no expert, I hope that was the right way to do it. Thank you for your indulgence. – Jm2t Feb 19 '23 at 17:48

3 Answers3

0
<?php
// Load the XML file
$xml = simplexml_load_file('your_xml_file.xml');

// Iterate through each "item" element
foreach ($xml->item as $item) {
    // Output the description and city
    echo $item->description . ' in ' . $item->city . '<br>';
}
?>
  • Thank you Sezer but I think it will write a title for each item. I search to write just one title. I am really sorry, I understand I was not clear. The best way is probably to show you my actual solution. I will edit my question. – Jm2t Feb 16 '23 at 09:54
0

Unlike php, xpath indexing start from "1".

So either of these should get you only the first trip:

#indexing is indicated inside the xpath expression so it starts with 1:
$items = $xml->xpath('//item[city[contains(.,"London")] and description[contains(.,"Trip")]][1]');

or

#indexing is indicated outside the xpath expression so it's handled by php and  starts with 0:
$items = $xml->xpath('//item[city[contains(.,"London")] and description[contains(.,"Trip")]]')[0];
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
  • Thank you Jack. But no unfortunately, it does not work, and displays all the items. For the second proposal, I also tried $items instead of $items3 (which I did not know) – Jm2t Feb 20 '23 at 08:00
  • First, apology: `$items3` was a typo; edited now. More importantly, not sure why it doesn't work. [Take a look here.](https://3v4l.org/16H7v). – Jack Fleeting Feb 20 '23 at 13:08
  • Thank you so much Jack. Unfortunately I don't think I have the level to understand the help provided by your link... – Jm2t Feb 20 '23 at 13:38
  • @Jm2t The link is just a demonstration that the code in the answer provides the expected output given the sample xml in the question. So I'm not sure where the problem is on your side. – Jack Fleeting Feb 20 '23 at 14:05
0

I found this way (to find only the first one) :

$xml   = simplexml_load_file('file.xml');
    $i = 0;
    $nb_affichage = 1;
    $items = $xml->xpath('//item[city[contains(.,"London")] and description[contains(.,"Trip")]]');
    foreach($xml->channel->item as $item){
    echo '<div> txt </div>';
    if(++$i>=$nb_affichage)
    break;
    }

It's definitely not the best, but it works. If anyone has a better idea, I'm still interested.

Jm2t
  • 19
  • 4