【发布时间】:2013-07-16 02:05:10
【问题描述】:
我有一个 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 查询有什么问题?谢谢!
【问题讨论】:
-
这是一个错字:
Description/Description_Text/text()?你的意思是使用Description/Description_Summary/text()? -
是的,谢谢乔,这是一个错字。我会改正的。
-
你的 xpath 查询看起来不错,你确定你的 $searchString 是 'Test'。
-
我确定,当我删除 /Description_Summary/text() 时,它会返回在描述中找到的子节点和内容。我是否错误地打印出数组?