用php中的xpath搜索特定节点的兄弟?

时间:2012-09-24 07:10:41

标签: php xml xpath

我有两个不同的xml doxument,我从webservice获得。

通过提交第一个请求,我将获得包含酒店基本信息并具有此类结构的xml

<offers>
<offer>
    <city_id>5</city_id>
    <city>Barcelona</city>
    <name>EnGrande test hotel</name>
    <address>Calle Muntaner 262</address>
    <price_no_vat>80</price_no_vat>
    <currency>EUR</currency>
    <type>Hotel</type>
    <description>
        A fantastic hotel at a fantastic price in a fantastic location.
    </description>
    <image_url>http://www.somesite.com/photos/hotels/bcn30_accomodation_1220_1.jpg</image_url>
    <offer_url>http://www.somesite.com/eg_offer_detail.php?hotel_id=191&checkin=2009-05-01&checkout=2009-05-02&city_id=5&guests=2</offer_url>
    <distance>3.175</distance>
</offer>

和第二个xml文档,其中包含酒店的详细信息并具有此类结构

<hotels>
<hotel> 
    <hotel_id>191</hotel_id> 
    <name>Test hotel</name> 
    <type>hostel</type> 
    <category>2</category> 
    <street>C/ de la Victoria, 2 - 4th floor left door. Madrid 28012.</street> 
    <postcode>28012</postcode> 
    <city>Madrid</city> 
    <city_id>5</city_id> 
    <description>This is a fantastic hotel</description> 
    <url>http://www.somesite.com/madrid-hostel-191-test-hotel.html</url> 
    <pppn>27.10</pppn> 
    <currency>EUR</currency> 
    <longitude>-3.701687097549</longitude> 
    <latitude>40.416587829590</latitude> 
    <images>
        <image>http://www.somesite.com/photos/hotels/30mad_accomodation_5_1.jpg</image>
        <image>http://www.somesite.com/photos/hotels/30mad_accomodation_5_2.jpg</image>
        <image>http://www.somesite.com/photos/hotels/30mad_accomodation_5_3.jpg</image>
        <image>http://www.somesite.com/photos/hotels/30mad_accomodation_5_4.jpg</image>
        <image>http://www.somesite.com/photos/hotels/30mad_accomodation_5_5.jpg</image>
        <image>http://www.somesite.com/photos/hotels/30mad_accomodation_5_6.jpg</image>
        <image>http://www.somesite.com/photos/hotels/30mad_accomodation_5_7.jpg</image>
        <image>http://www.somesite.com/photos/hotels/30mad_accomodation_5_8.jpg</image>
        <image>http://www.somesite.com/photos/hotels/30mad_accomodation_5_9.jpg</image>
    </images>
    <amenities>
        <amenity>Elevator / lift</amenity>
        <amenity>Laundry / washing machine</amenity>
        <amenity>TV lounge</amenity>
        <amenity>Left-luggage office</amenity>
        <amenity>Reception available</amenity>
        <amenity>24h reception</amenity>
        <amenity>Credit cards accepted</amenity>
        <amenity>TV</amenity>
        <amenity>Telephone</amenity>
        <amenity>Fans</amenity>
        <amenity>Heating</amenity>
        <amenity>Hair dryer</amenity>
        <amenity>Towels & sheets</amenity>
    </amenities>
</hotel> 

现在第二个xml文档包含同一城市的许多酒店。我想在第二个xml文档中搜索酒店名称并显示其详细信息。我为此创建了一个php代码,看起来像这样

<?php 
$city_id = 6;

$request =  "http://www.somewebsite.com/feeds/phpfile.php?vendor_key=xxx&checkin=26-11-2012&checkout=30-11-2012&guests=3&city_id=".$city_id;

$same_city_hotels_request = "http://www.somewebsite.com/feeds/get-hotels.php?vendor_key=xxx&city_id=".$city_id; 


$offerxml = simplexml_load_file($request);
$same_city = simplexml_load_file($same_city_hotels_request);

$hotel = $offerxml->offer;

for($i=0;$i<=10;$i++){   

$hotel_info = $same_city->xpath("//hotel/name[contains(text(),'".$hotel[$i]->name."')]");

{
?>

<tr><td>name</td><td>category</td><td>city</td><td>Address</td><td>price</td><td>Image</td></tr>
<tr><td> <?php echo $hotel[$i]->name; ?></td>
<td><?php echo $hotel_info[$i]->category; ?></td>
<td> <?php echo $hotel[$i]->city; ?> </td>
<td><?php echo $hotel[$i]->address; ?></td><td><?php echo $hotel[$i]->price; ?></td>
<td><?php echo $hotel_info[$i]->image; ?></td></tr>

<?php } ?>

任何人都可以告诉我这里我做错了什么吗?

1 个答案:

答案 0 :(得分:0)

您的XPath返回name元素:

$hotel_info = $same_city->xpath(
    "//hotel/name[contains(text(),'".$hotel[$i]->name."')]"
);

但是,您的PHP代码会尝试使用该元素,就像它是hotel元素一样。

使XPath返回hotel元素,而不是name元素。以下任何一种形式都应该这样做。

"//hotel/name[contains(text(),'".$hotel[$i]->name."')]/parent::*"
"//hotel[name[contains(.," . $hotel[$i]->name . ")]]"
"//hotel[contains(string(name)," . $hotel[$i]->name . ")]"