使用XSL格式化SOAP响应

时间:2012-01-20 16:34:54

标签: php xml xslt soap

这是我的肥皂请求和xml打印输出的代码:

header("Content-Type:text/xml");

$client = new SoapClient("http://---.---.-----------.--/EMSAPI/Service.asmx?wsdl", array('trace'=>1));

date_default_timezone_set("America/Chicago");

$result = $client->GetBookings(array('UserName' => "api", "Password" => "apitest", "StartDate" => date("H:i:s", strtotime("Today")), "EndDate"=> date("H:i:s", strtotime("Today")), "Buildings" =>array("int" => "1"), "Statuses" =>array("int"=>"1001"), "ViewComboRoomComponents" => true ));

$resString = html_entity_decode($client->__getLastResponse());

echo str_replace('<?xml version="1.0" encoding="utf-8"?>', '<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet href="bookings.xsl" type="text/xsl"?>', $resString);

我希望能够格式化这个XML布局并按时间排序:

<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet href="bookings.xsl" type="text/xsl"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetBookingsResponse xmlns="http://DEA.EMS.API.Web.Service/"><GetBookingsResult><Bookings>
  <Data>
    <BookingDate>2012-01-20T00:00:00-06:00</BookingDate>
    <StartBookingDate>2012-01-20T00:00:00-06:00</StartBookingDate>
    <RoomDescription>Memorial N103 Stotler</RoomDescription>
    <TimeEventStart>2012-01-20T10:00:00-06:00</TimeEventStart>
    <TimeEventEnd>2012-01-20T12:00:00-06:00</TimeEventEnd>
    <GroupName>Asian Affairs Center</GroupName>
    <EventName>TaLK</EventName>
    <SetupTypeDescription>Chairs In Rows</SetupTypeDescription>
    <SetupCount>15</SetupCount>
    <ReservationID>60750</ReservationID>
    <EventCoordinator>CJ</EventCoordinator>
    <GroupID>4036</GroupID>
    <VIP xml:space="preserve"> </VIP>
    <VIPEvent>false</VIPEvent>
    <ClosedAllDay>false</ClosedAllDay>
    <OpenTime>1900-01-01T07:00:00-06:00</OpenTime>
    <CloseTime>1900-01-01T00:00:00-06:00</CloseTime>
    <GroupTypeDescription>Campus Use</GroupTypeDescription>
    <EventTypeDescription>Lecture/Seminar</EventTypeDescription>
    <Contact>Jessica Fitzgerald 882-6902</Contact>
    <AltContact />
    <BookingID>322710</BookingID>
    <TimeBookingStart>2012-01-20T08:42:00-06:00</TimeBookingStart>
    <TimeBookingEnd>2012-01-20T12:15:00-06:00</TimeBookingEnd>
    <GMTStartTime>2012-01-20T14:42:00-06:00</GMTStartTime>
    <GMTEndTime>2012-01-20T18:15:00-06:00</GMTEndTime>
    <TimeZone>CT</TimeZone>
    <BuildingCode>Memorial</BuildingCode>
    <Building>Memorial Union Meeting Rooms</Building>
    <RoomCode>N103 Stotler</RoomCode>
    <Room>N103 Stotler Lounge All</Room>
    <RoomID>2409</RoomID>
    <BuildingID>1</BuildingID>
    <StatusID>1001</StatusID>
    <StatusTypeID>-14</StatusTypeID>
    <EventTypeID>2420</EventTypeID>
    <GroupTypeID>2305</GroupTypeID>
    <DateAdded>2011-12-30T11:13:47.81-06:00</DateAdded>
    <AddedBy>Chareba Johnson</AddedBy>
    <DateChanged>2012-01-17T11:37:22.38-06:00</DateChanged>
    <ChangedBy>Kaitlin Hinkle</ChangedBy>
  </Data>

</Bookings></GetBookingsResult></GetBookingsResponse></soap:Body></soap:Envelope>

现在,我只是想通过应用样式表来获得一些东西。所以这就是我到目前为止所做的:          

<xsl:template match="//soap:Envelope/GetBookingsResponse/GetBookingsResult/Bookings/Data">
    <xsl:apply-templates select="*/*/EventName"/>
</xsl:template>

</xsl:stylesheet>

非常感谢帮助!

1 个答案:

答案 0 :(得分:0)

如果您对XSL一无所知,请不要这样做并使用SimpleXML创建您知道如何处理的数据结构。

ZVON曾经在XSLT上有很好的教程,从那时起他们搞砸了导航但你仍然可以使用它们:http://zvon.org/comp/r/tut-XSLT_1.html#Pages~Contents

修改

在下面找到一个有效的版本(使用xsltproc在我的机器上测试)。注意命名空间声明,这主要是它失败的地方(我猜)。

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                xmlns:ws="http://DEA.EMS.API.Web.Service/">
    <xsl:template match="/soap:Envelope//ws:Data">
        <Name><xsl:value-of select="ws:EventName"/></Name>
    </xsl:template>
</xsl:stylesheet>