SimpleXML:使用三个条件的XPath

huangapple go评论66阅读模式
英文:

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>
.
.
&lt;/channel&gt;
&lt;/rss&gt;

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

&lt;?php
$xml   = simplexml_load_file(&#39;file.xml&#39;);
$items = $xml-&gt;xpath(&#39;//item[city[contains(.,&quot;London&quot;)] and description[contains(.,&quot;Trip&quot;)]]&#39;);
foreach($items as $item){
echo &#39; txt &#39;;
}
?&gt;

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

&lt;?php
$xml   = simplexml_load_file(&#39;file.xml&#39;);
$items = $xml-&gt;xpath(&#39;//item[city[contains(.,&quot;London&quot;)] and description[contains(.,&quot;Trip&quot;)]]&#39;)[0];
foreach($items as $item){
echo &#39; txt &#39;;
}
?&gt;

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 :

&lt;?php  
$xml   = simplexml_load_file(&#39;file.xml&#39;);
$items = $xml-&gt;xpath(&#39;//(/item)[1]&#39;);
foreach($xml-&gt;channel-&gt;item as $item){
echo &#39;&lt;div&gt;....&lt;/div&gt;&#39;;
}
?&gt;

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 :

&lt;rss version=&quot;2.0&quot; xmlns:atom=&quot;https://www.w3.org/2005/Atom&quot;&gt;
&lt;channel&gt;
&lt;item&gt;
	&lt;city&gt;London&lt;/city&gt;
	&lt;description&gt;Trip&lt;/description&gt;
	&lt;link&gt;page.php&lt;/link&gt;
	&lt;img&gt;img.jpg&lt;/img&gt;
&lt;/item&gt;
&lt;item&gt;
	&lt;city&gt;London&lt;/city&gt;
	&lt;description&gt;Trip&lt;/description&gt;
	&lt;link&gt;page.php&lt;/link&gt;
	&lt;img&gt;img.jpg&lt;/img&gt;
&lt;/item&gt;
&lt;item&gt;
	&lt;city&gt;Paris&lt;/city&gt;
	&lt;description&gt;Trip&lt;/description&gt;
	&lt;link&gt;page.php&lt;/link&gt;
	&lt;img&gt;img.jpg&lt;/img&gt;
&lt;/item&gt;
.
.
&lt;/channel&gt;
&lt;/rss&gt;

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

&lt;?php
$xml   = simplexml_load_file(&#39;file.xml&#39;);
$items = $xml-&gt;xpath(&#39;//item[city[contains(.,&quot;London&quot;)] and description[contains(.,&quot;Trip&quot;)]]&#39;);
foreach($items as $item){
echo &#39; txt &#39;;
}
?&gt;

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

&lt;?php
$xml   = simplexml_load_file(&#39;file.xml&#39;);
$items = $xml-&gt;xpath(&#39;//item[city[contains(.,&quot;London&quot;)] and description[contains(.,&quot;Trip&quot;)]]&#39;)[0];
foreach($items as $item){
echo &#39; txt &#39;;
}
?&gt;

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 :

&lt;?php  
$xml   = simplexml_load_file(&#39;file.xml&#39;);
$items = $xml-&gt;xpath(&#39;//(/item)[1]&#39;);
foreach($xml-&gt;channel-&gt;item as $item){
echo &#39;&lt;div&gt;....&lt;/div&gt;&#39;;
}
?&gt;

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>';
}
英文:
&lt;?php
// Load the XML file
$xml = simplexml_load_file(&#39;your_xml_file.xml&#39;);

// Iterate through each &quot;item&quot; element
foreach ($xml-&gt;item as $item) {
    // Output the description and city
    echo $item-&gt;description . &#39; in &#39; . $item-&gt;city . &#39;&lt;br&gt;&#39;;
}
?&gt;

答案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-&gt;xpath(&#39;//item[city[contains(.,&quot;London&quot;)] and description[contains(.,&quot;Trip&quot;)]][1]&#39;);

or

#indexing is indicated outside the xpath expression so it&#39;s handled by php and  starts with 0:
$items = $xml-&gt;xpath(&#39;//item[city[contains(.,&quot;London&quot;)] and description[contains(.,&quot;Trip&quot;)]]&#39;)[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(&#39;file.xml&#39;);
	$i = 0;
	$nb_affichage = 1;
	$items = $xml-&gt;xpath(&#39;//item[city[contains(.,&quot;London&quot;)] and description[contains(.,&quot;Trip&quot;)]]&#39;);
	foreach($xml-&gt;channel-&gt;item as $item){
	echo &#39;&lt;div&gt; txt &lt;/div&gt;&#39;;
	if(++$i&gt;=$nb_affichage)
	break;
	}

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

huangapple
  • 本文由 发表于 2023年2月16日 15:55:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定