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>

Dateそれぞれの属性が必要で、次に各日の要素CalendarDayの配列が必要です。RoomTypeInventorysoapEnvelopeのすべての名前空間が登録されました。

これが私のコードの要点です(「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');

CalendarDayで宣言されたデフォルトの名前空間であるため、名前空間'x'を登録しました。

$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 だけの Rooms を取得したい場合は、相対 XPath を使用する必要があります。

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