【问题标题】:How can I print specific content of a child node with XPATH in PHP?如何在 PHP 中使用 XPATH 打印子节点的特定内容?
【发布时间】: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() 时,它会返回在描述中找到的子节点和内容。我是否错误地打印出数组?

标签: php xml xpath


【解决方案1】:

您的代码没有问题。通过对从字符串中读取的一些修改,我在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

【讨论】:

  • 有趣,我明白了。好吧,让我在 pastebin 中粘贴一个弱点节的完整示例,也许你可以告诉我我是否遗漏了什么。在这里找到:pastebin.com/pKZpw3qQ 作为一个附带问题,如果我只想打印在 SimpleXMLElement 对象中找到的字符串,我该怎么做?
  • 第二点很简单,只需将 Object 转换为字符串即可。我可以将其编辑为答案。
  • 前者:使用正确的 XPath 参数似乎仍然有效 - codepad.org/YlZRQDok 是否有可能给出不正确的 $searchString
  • 一定有一些简单的事情我搞砸了,因为我一直在硬编码属性的字符串“Test”以及将它放在 XPath 查询中。我想我将不得不再看一眼,因为您已经多次证明自己是正确的!不过再次感谢!
  • 显然我加载 XML 文件的方式不正确?一旦我将其更改为文件的完整路径而不是简单的文件名,它就开始工作了!很奇怪,嗯?感谢您的帮助!
【解决方案2】:

我的打印语句有什么问题。

看起来不太错,但有一些问题。首先,您可以将其简化为一个表达式:

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

但这只是装饰性的。看起来错误的另一件事是您在SimpleXMLElement 上使用print_rprint_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 扩展。它可以返回有限 xpath 支持。如您的示例所示,调用xpath() 方法不会失败,但是,在返回数组中,没有文本节点,因为a SimpleXMLElement can not be a text-node

因此,像您一样使用 XPath 表达式查询文本节点 (... text()) 有点错误。

【讨论】:

    猜你喜欢
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    • 1970-01-01
    相关资源
    最近更新 更多