我有一个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
(
)
)
我的print语句或XPATH查询有什么问题?谢谢!
答案 0 :(得分: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
。
答案 1 :(得分:0)
我的印刷声明有什么问题。
看起来没有太错,但有一些问题。首先,您可以将它减少为单个表达式:
echo "<pre>", print_r($description, true), "</pre>";
但这仅仅是装饰性的。另一个看起来不对的是您在print_r
上使用SimpleXMLElement
。两个print_r
and var_dump
do not work well with simplexml都是因为这些物体具有魔力。
相反,只需将它们作为XML回显,这应该是最具描述性的:
echo "<pre>", htmlspecialchars($description[0]->asXML()), "</pre>";
示例性输出(纯文本):
<pre><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></pre>
这已经表明你没有在这里查询任何文本节点而是查询元素。阅读原因。
我的XPath查询出了什么问题?
您正在使用simplexml扩展。它可以返回限制 xpath支持。如您的示例所示,调用xpath()
方法不会失败,但是,在返回数组中,没有文本节点,因为a SimpleXMLElement can not be a text-node。
因此,拥有一个像你一样查询文本节点的XPath表达式(... text()
)是错误的。