您有一个无效的 xpath 查询需要修复,因为无效的 xpath 查询总是会导致错误。您不能使用它的结果来从中获取节点。
不幸的是,xpath 查询无效,因为 XML 无效。因此,如果不先修复 XML,就不能使用 xpath 查询(或进一步测试/继续编写)。
从您在问题中提供的 XML 来看,它显然缺少 w 前缀的命名空间声明。
您需要启用最高级别的错误报告 ( E_ALL
)、在您的开发环境中显示错误以及通常的错误日志记录。然后,您可以按照错误日志:
Warning: DOMDocument::loadXML(): Namespace prefix w on document is not defined in Entity, line: 1 in /tmp/execpad-1d8a88cab4fd/source-1d8a88cab4fd on line 15
Warning: DOMDocument::loadXML(): Namespace prefix w on body is not defined in Entity, line: 2 in /tmp/execpad-1d8a88cab4fd/source-1d8a88cab4fd on line 15
Warning: DOMDocument::loadXML(): Namespace prefix w on p is not defined in Entity, line: 3 in /tmp/execpad-1d8a88cab4fd/source-1d8a88cab4fd on line 15
Warning: DOMDocument::loadXML(): Namespace prefix w on r is not defined in Entity, line: 4 in /tmp/execpad-1d8a88cab4fd/source-1d8a88cab4fd on line 15
Warning: DOMDocument::loadXML(): Namespace prefix w on t is not defined in Entity, line: 5 in /tmp/execpad-1d8a88cab4fd/source-1d8a88cab4fd on line 15
Warning: DOMXPath::query(): Undefined namespace prefix in /tmp/execpad-1d8a88cab4fd/source-1d8a88cab4fd on line 17
Warning: DOMXPath::query(): Invalid expression in /tmp/execpad-1d8a88cab4fd/source-1d8a88cab4fd on line 17
Fatal error: Call to a member function item() on a non-object in /tmp/execpad-1d8a88cab4fd/source-1d8a88cab4fd on line 18
正如这些所示,XML 存在许多问题,最终导致 xpath 查询无效并最终导致整个脚本停止。