获取php和xml中包含特定值的xml节点数

获取php和xml中包含特定值的xml节点数,php,xml,count,nodes,Php,Xml,Count,Nodes,嗨,我正在尝试获取具有特定内容或值的所有节点的计数。在本例中,我有一个xml,如下所示: <hotel> <city>Cancun</city> <postalCode>77500</postalCode> <countryCode>MX</countryCode> <airportCode>CUN</airportCode> <supplierType>E</suppl

嗨,我正在尝试获取具有特定内容或值的所有节点的计数。在本例中,我有一个xml,如下所示:

<hotel>
<city>Cancun</city>
<postalCode>77500</postalCode>
<countryCode>MX</countryCode>
<airportCode>CUN</airportCode>
<supplierType>E</supplierType>
<propertyCategory>1</propertyCategory>
<hotelRating>4.0</hotelRating>
<confidenceRating>52</confidenceRating>
</hotel>

坎昆
77500
MX
寸
E
1.
4
52
我需要找到所有值为4的hotelRating,并计算我的xml列表中有多少。我不知道如何用php实现这一点。如果有人能帮我,我会非常感激

顺便说一句,我已经按如下方式连接了xml文件,并且能够获取数据和发送数据

$post_string= 'type=xml&cid=222&minorRev=14&apiKey=222&locale='.$userLocale.'&currencyCode='.$userCurr.'&customerIpAddress='.$userIp.'&customerUserAgent='.$userAgent.'&xml=<HotelListRequest><destinationId>'.$destinationId.'</destinationId><arrivalDate>'.$arriving.'</arrivalDate><departureDate>'.$departing.'</departureDate><RoomGroup>'.$xmlQuery.'</RoomGroup><supplierCacheTolerance>MED_ENHANCED</supplierCacheTolerance></HotelListRequest> ';
//Relative path to the file with $_POST parsing
$path = "http://api.ean.com/ean-services/rs/hotel/v3/list"; 
$ch = curl_init($path); 
$fp = fopen('xml/data.xml','w');
//Send the data to the file
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/xml')); 
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FILE, $fp);
$val = curl_exec($ch);
curl_close($ch);//Close curl session
fclose($fp); //Close file overwrite
$data = simplexml_load_file('xml/data.xml');
}
$post_string='type=xml&cid=222&minorRev=14&apiKey=222&locale='.$userLocale.¤cyCode='.$userCurr.&customerIpAddress='.$userIp.&customerUserAgent='.$userAgent.&xml='.$destinationId.'.$inriming.'.$deming.'.'.$definding.'.$definding.'.$xmlQuery.'MED_-ENHANCED'.';
//解析后使用$\u的文件的相对路径
$path=”http://api.ean.com/ean-services/rs/hotel/v3/list"; 
$ch=curl\u init($path);
$fp=fopen('xml/data.xml','w');
//将数据发送到文件
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Accept:application/xml'));
curl_setopt($ch,CURLOPT_头,0);
curl_setopt($ch,CURLOPT_文件,$fp);
$val=curl_exec($ch);
卷曲关闭($ch)//闭卷会话
fclose($fp)//关闭文件覆盖
$data=simplexml_load_文件('xml/data.xml');
}
提前感谢您的帮助

foreach ($data->hotel as $hotel)
{
    if ($hotel->hotelRating == '4.0')
    {
        //  hotel with 4.0 rating found
    }
}
将为您提供hotelrating=4.0的酒店数

$count = $data->xpath("count(\"//hotel[hotelRating= '4.0']\")");