0

我不知道我是否正确,我有一个 cURL 脚本,它从提要中提取一个 xml,然后将其包含在一个 php 字符串中 $rawdata 数据看起来像......

<search>
    <response status="1">
        <errors>
            <number_of_hotels>1 of 1</number_of_hotels>
        </errors>
    </response>
    <lr_rates>
        <hotel>
            <hotel_ref>142680</hotel_ref>
            <hotel_currency>GBP</hotel_currency>
            <hotel_rooms>
                <room>
                    <ref>4380316</ref>
                    <type>10</type>
                    <type_description>Apartment</type_description>
                    <sleeps>2</sleeps>
                    <rooms_available>0</rooms_available>
                    <adults>2</adults>
                    <children>0</children>
                    <breakfast>false</breakfast>
                    <dinner>false</dinner>
                    <description>The apartment has seperate kitchen area, lounge/dining area, bedroom with double bed and bathroom with bath &amp; shower.</description>
                    <alternate_description/>
                    <rack_rate>140.00</rack_rate>
                    <rate>
                        <date>31/07/2012</date>
                        <formatted_date>31 July 2012</formatted_date>
                        <price>Full</price>
                        <hotelcurrencyprice>Full</hotelcurrencyprice>
                        <numeric_price>Full</numeric_price>
                        <numeric_hotelcurrencyprice>Full</numeric_hotelcurrencyprice>
                        <requested_currency>GBP</requested_currency>
                    </rate>
                    <rate>
                        <date>01/08/2012</date>
                        <formatted_date>01 August 2012</formatted_date>
                        <price>Full</price>
                        <hotelcurrencyprice>Full</hotelcurrencyprice>
                        <numeric_price>Full</numeric_price>
                        <numeric_hotelcurrencyprice>Full</numeric_hotelcurrencyprice>
                        <requested_currency>GBP</requested_currency>
                    </rate>
                    <available_online>false</available_online>
                    <minimum_nights>1</minimum_nights>
                    <bed_type>Double</bed_type>
                    <cancellation_policy/>
                    <cancellation_days/>
                    <cancellation_hours/>
                    <room_terms/>
                </room>
                <room>
                    <ref>4383781</ref>
                    <type>10</type>
                    <type_description>Apartment</type_description>
                    <sleeps>4</sleeps>
                    <rooms_available>0</rooms_available>
                    <adults>4</adults>
                    <children>0</children>
                    <breakfast>false</breakfast>
                    <dinner>false</dinner>
                    <description>The apartment has seperate kitchen area, lounge with dining area, two double bedrooms, ensuite &amp; main bathroom.</description>
                    <alternate_description/>
                    <rack_rate>185.00</rack_rate>
                    <rate>
                        <date>31/07/2012</date>
                        <formatted_date>31 July 2012</formatted_date>
                        <price>Full</price>
                        <hotelcurrencyprice>Full</hotelcurrencyprice>
                        <numeric_price>Full</numeric_price>
                        <numeric_hotelcurrencyprice>Full</numeric_hotelcurrencyprice>
                        <requested_currency>GBP</requested_currency>
                    </rate>
                    <rate>
                        <date>01/08/2012</date>
                        <formatted_date>01 August 2012</formatted_date>
                        <price>Full</price>
                        <hotelcurrencyprice>Full</hotelcurrencyprice>
                        <numeric_price>Full</numeric_price>
                        <numeric_hotelcurrencyprice>Full</numeric_hotelcurrencyprice>
                        <requested_currency>GBP</requested_currency>
                    </rate>
                    <available_online>false</available_online>
                    <minimum_nights>1</minimum_nights>
                    <bed_type/>
                    <cancellation_policy/>
                    <cancellation_days/>
                    <cancellation_hours/>
                    <room_terms/>
                </room>
            </hotel_rooms>
            <cancellation_type>First Night Stay Chargeable</cancellation_type>
            <cancellation_policy>1 Days Prior to Arrival</cancellation_policy>
            <citytax>
                <typename/>
                <value/>
                <optedin/>
                <iscitytaxarea/>
            </citytax>
        </hotel>
    </lr_rates>
</search>

有多个价格实例,如果可能的话,我需要将其他信息传递回 php............ ........ 好的,所以答案是的,这是可能的,但现在我坚持尝试让 xsl 输出房间数据,所以这就是我如何让它工作到获得输出的方式,首先我必须重建我的 php 安装以包含 xsl,然后使用这个 php总而言之:

  $ch = curl_init();
  $timeout = 0;
  curl_setopt ($ch, CURLOPT_URL, $url1);
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $rawdata = curl_exec($ch);
  curl_close($ch);
    $xml = new DOMDocument();
    $xml->$rawdata;
   $xsl = new DOMDocument;
   $xsl->load('path/to/file.xsl');
   $proc = new XSLTProcessor();
   $proc->importStyleSheet($xsl);
  $lrdata = $proc->transformToXML($xml);

现在一切正常,但我无法让我的 xslt 从每个房间节点提取数据我尝试更改匹配和选择值但没有运气我在这里做错了什么假设 xml 与上面相同这是 xsl:

 <?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/">
      <h2> Availability Search:</h2>
       <table border="1">
       <tr bgcolor="#FFFFFF">
        <th align="left">Room Type</th>
        <th align="left">Description</th>
        <th align="left">Availability</th>
        <th align="left">Price</th>
        </tr>
      <xsl:for-each select="/room">
       <tr>
        <td><xsl:value-of select="type_description" /></td>
        <td><xsl:value-of select="description" /></td>
        <td><xsl:value-of select="rooms_available" /></td>
        <td><xsl:value-of select="rack_rate" /></td>
      </tr>
     </xsl:for-each>
  </table>
  </xsl:template>
  </xsl:stylesheet>

很抱歉造成混乱,所以现在我需要知道这个 xsl 有什么问题以及是否可以将信息作为 $string 传递回 php

4

2 回答 2

1

您的 xsl:for-each 选择需要阅读:

<xsl:for-each select='//room'>

原因是 /room 会在主层寻找房间(在 XML 中你只能有 1 个),但 //room 会在 XML 树的任何层寻找所有房间元素。

完整的 XSL:(您还缺少 xsl:output 指令)

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method='html' />
       <xsl:template match="/">
          <h2> Availability Search:</h2>
           <table border="1">
           <tr bgcolor="#FFFFFF">
            <th align="left">Room Type</th>
            <th align="left">Description</th>
            <th align="left">Availability</th>
            <th align="left">Price</th>
            </tr>
          <xsl:for-each select="//room">
           <tr>
            <td><xsl:value-of select="type_description" /></td>
            <td><xsl:value-of select="description" /></td>
            <td><xsl:value-of select="rooms_available" /></td>
            <td><xsl:value-of select="rack_rate" /></td>
          </tr>
         </xsl:for-each>
      </table>
      </xsl:template>
    </xsl:stylesheet>

从您的示例中生成 2 行

于 2012-08-01T14:21:06.030 回答
1

我最终设法让这个工作正确的答案是,我正在使用 dom 我应该一直使用 simplexmlelement 在通过 xml 进入的 curl 之后创建一个 XML 感谢您的帮助

curl_setopt ($ch, CURLOPT_URL, $url1);
 curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $urlstring);
    $rawdata = curl_exec($ch);
    curl_close($ch);
  $lrxml = new SimpleXMLElement($rawdata);
 $xsl = new DOMDocument;
$xsl->load('path/to/file.xsl');
$proc = new XSLTProcessor();
$proc->importStyleSheet($xsl);
$lrdata = $proc->transformToXML($lrxml);
于 2012-08-01T19:29:46.970 回答