英文:
Simplexml : xpath with three conditions
问题
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.
英文:
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.
答案1
得分: 0
// 加载 XML 文件
$xml = simplexml_load_file('your_xml_file.xml');
// 遍历每个 "item" 元素
foreach ($xml->item as $item) {
// 输出描述和城市
echo $item->description . ' 在 ' . $item->city . '<br>';
}
英文:
<?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>';
}
?>
答案2
得分: 0
与php不同,xpath索引从“1”开始。
所以以下任何一个都可以让你获取到第一个旅行:
#indexing is indicated inside the xpath expression so it starts with 1:
$items = $xml->xpath('//itemcity[contains(.,"London")] and description[contains(.,"Trip")]');
或
#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];
英文:
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];
答案3
得分: 0
我找到了这种方法(只找第一个):
$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;
}
这肯定不是最佳方法,但它可以工作。如果有更好的想法,我仍然感兴趣。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论