我有一个应用程序,用户在其中写入XPath查询以用作给定文档的源数据。有时他们只需要一个元素的内容,有时他们需要整个元素本身。据我的理解,他们应该能够在查询结束时指定text()或node()来选择哪种行为。PHP SimpleXMLElement:我可以用saveXML()或outerHTML(string)获得innertext吗?
但是,看起来像我从SimpleXMLElement获取字符串的方式决定了行为,而不管查询。
当我将查询转换为(字符串)时,它总是只返回内部XML。
(string) $xml->xpath('//document/head/Keywords')[0] ===
(string) $xml->xpath('//document/head/Keywords/node()')[0] ===
(string) $xml->xpath('//document/head/Keywords/text()')[0] ===
'17';
如果我使用 - > saveXML(),它总是返回整个标签。
$xml->xpath('//document/head/Keywords')[0]->asXML() ===
$xml->xpath('//document/head/Keywords/node()')[0]->asXML() ===
$xml->xpath('//document/head/Keywords/text()')[0]->asXML() ===
'<Keywords topic="611x27keqj">17</Keywords>';
有没有办法,我可以得到一个字符串,它可以让我的用户指定内VS外部XML作为自己XPath查询的一部分的单一方式?
[我怎样才能从XML数据提取所有文本使用PHP(即SimpleXmlElement)?](https://stackoverflow.com/questions/31766136/how-can-i-extract-all-text-from -xml - 数据 - 使用 - PHP-IE-的SimpleXMLElement) – ThW