这很可能是因为他们引入了默认名称空间(
xmlns="http://iptc.org/std/NewsML/2003-10-10/"
)在他们的文件中。简单地说,SimpleXML对默认名称空间的支持不是很好。
$xml->registerXPathNamespace("n", "http://iptc.org/std/NewsML/2003-10-10/");
$xml->xpath('/n:NewsML');
"n:"
前缀
每一个
http://people.ischool.berkeley.edu/~felix/xml/php-and-xmlns.html
.
the spec
:
The
registerXPathNamespace()
function
为下一个XPath查询创建前缀/ns上下文。
function simplexml_xpath_ns($element, $xpath, $xmlns)
{
foreach ($xmlns as $prefix_uri)
{
list($prefix, $uri) = explode("=", $prefix_uri, 2);
$element->registerXPathNamespace($prefix, $uri);
}
return $element->xpath($xpath);
}
用法:
$xmlns = ["n=http://iptc.org/std/NewsML/2003-10-10/"];
$result = simplexml_xpath_ns($xml, '/n:NewsML', $xmlns);