2014-01-05 118 views
-1

我寫了下面的XPath:良好的XPath,但沒有結果PHP

//datas[child::node()//DataInfo/DataInfoItem[contains(@Value,'EA22')]] 

應用於以下XML:

<?xml version="1.0" encoding="utf-8"?> 
<root> 
<Customer Modified="false"> 
    <datas> 
     <FireData> 
      <DataInfo> 
       <DataInfoItem Name="place" Value="g5688"/> 
       <DataInfoItem Name="number" Value="EA22"/> 
       <DataInfoItem Name="use" Value="ice"/> 
      </DataInfo> 
      <DataHistorics> 
       <DataHistoric HistoDate="2013-11-25T07:54:31" Type="0" visual="691.77"/> 
       <DataHistoric HistoDate="2013-12-02T07:23:29" Type="0" visual="618.65"/> 
      </DataHistorics> 
      <FireDataData Previous="106256.71"/> 
     </FireData> 
    </datas> 
</Customer> 
<Customer Modified="false"> 
    <datas> 
     <FireData> 
      <DataInfo> 
       <DataInfoItem Name="place" Value="g5678"/> 
       <DataInfoItem Name="number" Value="GH44"/> 
       <DataInfoItem Name="use" Value="ice"/> 
      </DataInfo> 
      <DataHistorics> 
       <DataHistoric HistoDate="2013-11-25T07:54:31" Type="0" visual="691.77"/> 
       <DataHistoric HistoDate="2013-12-02T07:23:29" Type="0" visual="618.65"/> 
      </DataHistorics> 
      <FireDataData Previous="106256.71"/> 
     </FireData> 
    </datas> 
</Customer> 

我試了XPath的測試儀在線(http://www.xpathtester.com/http://xpath.online-toolz.com/tools/xpath-editor.php )和Oxygen也是, 和xpath一直返回我正在尋找的東西:

<datas> 
     <FireData> 
      <DataInfo> 
       <DataInfoItem Name="place" Value="g5688"/> 
       <DataInfoItem Name="number" Value="EA22"/> 
       <DataInfoItem Name="use" Value="ice"/> 
      </DataInfo> 
      <DataHistorics> 
       <DataHistoric HistoDate="2013-11-25T07:54:31" Type="0" visual="691.77"/> 
       <DataHistoric HistoDate="2013-12-02T07:23:29" Type="0" visual="618.65"/> 
      </DataHistorics> 
      <FireDataData Previous="106256.71"/> 
     </FireData> 
    </datas> 

但如果我這樣做在PHP中,我得到什麼:

$xml = simplexml_load_file(test.xml); 
$data = $xml->xpath("//datas[child::node()//DataInfo/DataInfoItem[contains(@Value,'EA22')]]"); 
var_dump($data) 

我真不明白,我在其他的討論閱讀的var_dump不好打印的XPath結果查詢,但我嘗試了很多東西,我不認爲它是從那裏來的。

Anymone可以幫助我嗎?

非常感謝!

+0

似乎可以在PHP中找到純DOM:https://eval.in/86074 – ThW

+0

您的代碼可以正常工作:https://eval.in/86076 - 錯誤可能不在xpath表達式中。 – michi

回答

0

首先,在您的示例XML中,最後缺少</root>,但我想這只是複製/粘貼錯誤。

在你的代碼的問題是,你沒有用引號把文件名:

$xml = simplexml_load_file(test.xml); 

如果你這樣做echo hello;,PHP試圖通過解釋「你好」作爲字符串進行補償。在你的情況下,它所做的是將其解釋爲兩個字符串:「test」和「xml」,因爲.是字符串連接運算符。所以它會嘗試打開「testxml」(如果你把「test」和「xml」放在一起)而不是「test.xml」,你會得到它。

的修復將是:

$xml = simplexml_load_file('test.xml'); 

然後它工作得很好,對我來說。

相關問題