使用xpath从SOAP响应返回元素数组

时间:2013-03-05 03:24:53

标签: php xml soap xpath simplexml

鉴于更大的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的{​​{1}}属性,然后是每天CalendarDay元素的数组。 soap Envelope中的所有命名空间都已注册。

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

RoomTypeInventory

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

$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->attributes()->Date是空的。 我也试过了

$RoomTypeInventory

也失败了。但

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

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

我做错了什么?

1 个答案:

答案 0 :(得分:0)

看起来问题是您正在注册错误的命名空间。当x元素具有命名空间http://...../Availability/时,您将RoomTypeInventory注册为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');
}

顺便说一下,如果你想获得当前CalendarDay的房间,你需要使用相对XPath:

    $RoomTypeInventory = $CalendarDay->xpath('.//h:RoomTypeInventory');