1

Given this fragment of a larger SOAP response:

<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>

I need the Date attribute of each CalendarDay, then an array of RoomTypeInventory elements for each day. All namespaces from the soap Envelope have been registered.

Here's the gist of my code ('a' is a previously registered default namespace):

$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');

I registered the namespace 'x' because it was the default namespace declared with CalendarDay.

$CalendarDay->attributes()->Date has the correct value. But $RoomTypeInventory is empty. I've also tried

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

which also fails. But

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

returns an appropriate value, so I assume the namespacing is correct.

What am I doing wrong?

1 Answers1

0

Looks like the issue is that you're registering the wrong namespace. You're registering x as http://...../Availability/ when the RoomTypeInventory elements have the namespace http://...../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');
}

Incidentally, if you want to get the Rooms for just the current CalendarDay, you need to use a relative XPath:

    $RoomTypeInventory = $CalendarDay->xpath('.//h:RoomTypeInventory');
JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • Wow. I didn't think there'd be a default namespace set on each element. And I didn't look for it out there. Thanks, that did it. – user1623459 Mar 05 '13 at 04:44