给定一个较大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>
我需要每个CalendarDay
的Date
属性,然后是每天的RoomTypeInventory
元素数组。soap信封中的所有名称空间都已注册。
下面是我代码的要点('a'是之前注册的默认名称空间):
$CalendarDays = $responseXML->xpath('//a:CalendarDay');
foreach($CalendarDays as $CalendarDay){
$CalendarDate = ($CalendarDay->attributes()->Date);
echo $CalendarDate . " ";
$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');
返回一个适当的值,所以我假设命名空间是正确的。
我做错了什么?
看起来问题是您注册了错误的名称空间。当RoomTypeInventory
元素具有名称空间http://...../HotelCommon
:
x
注册为http://...../Availability/
。$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 . " ";
$CalendarDay->registerXPathNamespace('h', $hNamespace);
$RoomTypeInventory = $CalendarDay->xpath('//h:RoomTypeInventory');
}
顺便提一下,如果只想获取当前CalendarDay的Rooms,则需要使用相对XPath:
$RoomTypeInventory = $CalendarDay->xpath('.//h:RoomTypeInventory');