2013-07-16 29 views
0

我有一個XPath查詢我在PHP運行鍼對下面的XML從CWE:如何在PHP中使用XPATH打印子節點的特定內容?

<Weaknesses> 
<Weakness ID="211" Name="Test" Weakness_Abstraction="Base" Status="Incomplete"> 
<Description> 
<Description_Summary> 
The software performs an operation that triggers an external diagnostic or error message that is not directly generated by the software, such as an error generated by the programming language interpreter that the software uses. The error can contain sensitive system information. 
</Description_Summary> 
</Description> 
</Weakness> 
</Weaknesses> 

我寫了下面的PHP使用XPath以目標的「Description_Summary」子節點中的內容,但是,它只是返回Array()。我有一個$ _GET,它從前一頁拉出$ searchString變量,指向我在「弱點」節點中找到的特定屬性。

什麼它目前返回:

Array 
(
    [0] => SimpleXMLElement Object 
     (
     ) 

) 

什麼是錯我的print語句或XPath查詢?謝謝!

+0

這是一個錯字:'Description/Description_Text/text()'?你是否想要使用'Description/Description_Summary/text()'? – Joe

+0

是的,謝謝喬,這是一個錯字。我會糾正這一點。 – user1995565

+0

你的xpath查詢看起來很好,你確定你的$ searchString是'Test'。 – Desu

回答

0

你的代碼沒有問題。通過一些修改來從字符串讀取,我在codepad and it worked fine上測試了這個。

<?php 
$xml = '<Weaknesses> 
<Weakness ID="211" Name="Test" Weakness_Abstraction="Base" Status="Incomplete"> 
<Description> 
<Description_Summary> 
The software performs an operation that triggers an external diagnostic or error message that is not directly generated by the software, such as an error generated by the programming language interpreter that the software uses. The error can contain sensitive system information. 
</Description_Summary> 
</Description> 
</Weakness> 
</Weaknesses>'; 

$searchString = 'Test'; 
echo "<b>CWE Name: </b>" . $searchString . "</br>"; 
$xml = simplexml_load_string($xml); 

$description = $xml->xpath('//Weakness[@Name="'. $searchString .'"]/Description/Description_Summary/text()'); 

echo "<pre>"; print_r((string) $description[0]); echo "</pre>"; 

?> 

並返回此:

<b>CWE Name: </b>Test</br><pre> 
The software performs an operation that triggers an external diagnostic or error message that is not directly generated by the software, such as an error generated by the programming language interpreter that the software uses. The error can contain sensitive system information. 
</pre> 

這可能是該文件不包含你所想象的那樣,或者$searchString不等於Test的情況。

+0

有趣,我明白了。那麼,讓我在pastebin中粘貼一個Weakness節的完整示例,也許你可以告訴我我是否缺少一些東西。在這裏找到:http://pastebin.com/pKZpw3qQ 作爲一個側面的問題,如果我只想打印在SimpleXMLElement對象中找到的字符串,我該怎麼做呢? – user1995565

+0

第二位很簡單,只需將Object轉換爲字符串即可。我可以將其編輯爲答案。 – 2013-07-16 03:42:30

+0

前者:XPath的正確參數似乎仍然有效 - http://codepad.org/YlZRQDok有沒有可能出現錯誤的'$ searchString'? – 2013-07-16 03:45:59

0

什麼是錯我的print語句。

看起來不是太錯,但也存在一些問題。首先,您可以將其縮減爲單個表達式:

echo "<pre>", print_r($description, true), "</pre>"; 

但這只是表面化妝。另一件看起來不正確的事是您在SimpleXMLElement上使用print_r。因爲這些對象具有魔力,所以print_r and var_dump do not work well with simplexml

而不只是附和他們,如同XML,這應該是最描述:

echo "<pre>", htmlspecialchars($description[0]->asXML()), "</pre>"; 

輸出例(純文本):

<pre>&lt;Description_Summary&gt; 
The software performs an operation that triggers an external diagnostic or error message that is not directly generated by the software, such as an error generated by the programming language interpreter that the software uses. The error can contain sensitive system information. 
&lt;/Description_Summary&gt;</pre> 

這已經表明你沒有查詢任何文本 - 節點在這裏,但一個元素。請閱讀原因。

這有什麼錯我的XPath查詢?

您正在使用的SimpleXML extenstion。它有有限的 xpath支持它可以返回的內容。如你的例子所示,調用xpath()方法不會失敗,但是,在返回數組中,沒有文本節點,因爲a SimpleXMLElement can not be a text-node

因此有像你這樣(... text())是有點不對的查詢文本節點XPath表達式。

相關問題