簡體   English   中英

如何在PHP中使用XPATH打印子節點的特定內容?

[英]How can I print specific content of a child node with XPATH in PHP?

我有一個XPATH查詢,我正在PHP中針對來自CWE的以下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>

我已經使用XPATH編寫了以下PHP,以定位“ Description_Summary”子節點中的內容,但是,它只是返回Array()。 我有一個$ _GET,它從上一頁拉出$ searchString變量,指向“弱點”節點中的我的特定屬性。

<?php
$searchString = $_GET["searchString"];
echo "<b>CWE Name: </b>" . $searchString . "</br>";
$xml = simplexml_load_file("cwe.xml");

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

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

?>

當前返回的內容:

Array
(
    [0] => SimpleXMLElement Object
        (
        )

)

我的打印語句或XPATH查詢出了什么問題? 謝謝!

您的代碼沒有錯。 經過一些修改以讀取字符串,我在鍵盤上對此進行了測試, 並且效果很好

<?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

我的印刷聲明有什么問題。

看起來不太錯,但存在一些問題。 首先,您可以將其簡化為單個表達式:

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

但這僅僅是裝飾。 看起來不對的另一件事是,您在SimpleXMLElement上使用了print_r 由於這些對象具有魔力,因此print_rvar_dump都無法與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擴展。 它只能返回有限的 xpath支持。 如您的示例所示,調用xpath()方法不會失敗,但是,在return數組中,沒有文本節點,因為SimpleXMLElement不能是文本節點

因此,擁有一個像您一樣查詢文本節點的XPath表達式( ... text() )有點錯誤。

暫無
暫無

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

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