簡體   English   中英

有可能xslt轉換動態的PHP字符串

[英]is it possible xslt transformation on the fly of php string

我不知道我是否正確,我有一個cURL腳本從一個feed中提取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

你的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行

我設法得到這個工作到底正確的答案是,我正在使用dom我應該使用simplexmlelement來創建一個XML后,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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM