繁体   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