1

给定一个更大的 SOAP 响应的这个片段:

<Calendar>
    <CalendarDay Date="2013-10-01" xmlns="http://webservices.micros.com/og/4.3/Availability/">
        <Occupancy>
            <RoomTypeInventory roomTypeCode="2BS" totalRooms="26" overBookingLimit="0" soldDeductible="0" soldNonDeductible="0" totalAvailableRooms="26" xmlns="http://webservices.micros.com/og/4.3/HotelCommon/"/>
            <RoomTypeInventory roomTypeCode="3BS" totalRooms="4" overBookingLimit="0" soldDeductible="0" soldNonDeductible="0" totalAvailableRooms="4" xmlns="http://webservices.micros.com/og/4.3/HotelCommon/"/>
            <RoomTypeInventory roomTypeCode="BSV" totalRooms="3" overBookingLimit="0" soldDeductible="0" soldNonDeductible="0" totalAvailableRooms="3" xmlns="http://webservices.micros.com/og/4.3/HotelCommon/"/>
            <RoomTypeInventory roomTypeCode="CHV" totalRooms="1" overBookingLimit="0" soldDeductible="0" soldNonDeductible="0" totalAvailableRooms="1" xmlns="http://webservices.micros.com/og/4.3/HotelCommon/"/>
            <RoomTypeInventory roomTypeCode="D3B" totalRooms="6" overBookingLimit="0" soldDeductible="0" soldNonDeductible="0" totalAvailableRooms="6" xmlns="http://webservices.micros.com/og/4.3/HotelCommon/"/>
            <RoomTypeInventory roomTypeCode="DLDB" totalRooms="86" overBookingLimit="0" soldDeductible="0" soldNonDeductible="0" totalAvailableRooms="86" xmlns="http://webservices.micros.com/og/4.3/HotelCommon/"/>
            <RoomTypeInventory roomTypeCode="DLDC" totalRooms="81" overBookingLimit="0" soldDeductible="0" soldNonDeductible="0" totalAvailableRooms="81" xmlns="http://webservices.micros.com/og/4.3/HotelCommon/"/>
            <RoomTypeInventory roomTypeCode="DLKB" totalRooms="123" overBookingLimit="0" soldDeductible="0" soldNonDeductible="0" totalAvailableRooms="123" xmlns="http://webservices.micros.com/og/4.3/HotelCommon/"/>
            <RoomTypeInventory roomTypeCode="DLKC" totalRooms="117" overBookingLimit="0" soldDeductible="0" soldNonDeductible="0" totalAvailableRooms="117" xmlns="http://webservices.micros.com/og/4.3/HotelCommon/"/>
            <RoomTypeInventory roomTypeCode="GDDB" totalRooms="4" overBookingLimit="0" soldDeductible="0" soldNonDeductible="0" totalAvailableRooms="4" xmlns="http://webservices.micros.com/og/4.3/HotelCommon/"/>
            <RoomTypeInventory roomTypeCode="GDDC" totalRooms="17" overBookingLimit="0" soldDeductible="0" soldNonDeductible="0" totalAvailableRooms="17" xmlns="http://webservices.micros.com/og/4.3/HotelCommon/"/>
            <RoomTypeInventory roomTypeCode="GDKB" totalRooms="20" overBookingLimit="0" soldDeductible="0" soldNonDeductible="0" totalAvailableRooms="20" xmlns="http://webservices.micros.com/og/4.3/HotelCommon/"/>
            <RoomTypeInventory roomTypeCode="GMS" totalRooms="3" overBookingLimit="0" soldDeductible="0" soldNonDeductible="0" totalAvailableRooms="3" xmlns="http://webservices.micros.com/og/4.3/HotelCommon/"/>
        </Occupancy>
    </CalendarDay>
    <CalendarDay Date="2013-10-02" xmlns="http://webservices.micros.com/og/4.3/Availability/">
    ...
    </CalendarDay>
</Calendar>

我需要Dateeach 的属性,然后是每天CalendarDay的元素数组。RoomTypeInventory肥皂信封中的所有名称空间都已注册。

这是我的代码的要点('a' 是以前注册的默认命名空间):

$CalendarDays = $responseXML->xpath('//a:CalendarDay');

foreach($CalendarDays as $CalendarDay){
    $CalendarDate = ($CalendarDay->attributes()->Date);
    echo $CalendarDate . "&nbsp;";
    $CalendarDay->registerXPathNamespace('x', 'http://webservices.micros.com/og/4.3/Availability/');
    $RoomTypeInventory = $CalendarDay->xpath('//x:RoomTypeInventory');
}


$responseXML = simplexml_load_file('FetchAvailablePackages60Days.resp.xml');

我注册了命名空间“x”,因为它是使用 CalendarDay 声明的默认命名空间。

$CalendarDay->attributes()->Date有正确的值。但是$RoomTypeInventory是空的。我也试过

   $RoomTypeInventory = $CalendarDay->xpath('x:Occupancy/x:RoomTypeInventory');

这也失败了。但

   $Occupancy = $CalendarDay->xpath('x:Occupancy');

返回一个适当的值,所以我假设命名空间是正确的。

我究竟做错了什么?

4

1 回答 1

0

看起来问题是您注册了错误的命名空间。x当元素具有命名空间http://...../Availability/时,您正在注册:RoomTypeInventoryhttp://...../HotelCommon

$aNamespace = 'http://webservices.micros.com/og/4.3/Availability/';
$hNamespace = 'http://webservices.micros.com/og/4.3/HotelCommon/';

$CalendarDays = $responseXML->xpath('//a:CalendarDay');

foreach($CalendarDays as $CalendarDay){
    $CalendarDate = ($CalendarDay->attributes()->Date);
    echo $CalendarDate . "&nbsp;";
    $CalendarDay->registerXPathNamespace('h', $hNamespace);
    $RoomTypeInventory = $CalendarDay->xpath('//h:RoomTypeInventory');
}

顺便说一句,如果您想获取当前 CalendarDay 的房间,您需要使用相对 XPath:

    $RoomTypeInventory = $CalendarDay->xpath('.//h:RoomTypeInventory');
于 2013-03-05T04:24:51.063 回答