我有一个像这样的 XML 结构:
<layout>
<pattern> ... </pattern>
<row> ... </row>
</layout>
如何选择<layout>
的第一个节点通过其索引,使用 XPath?
w3schools 表示类似于 /bookstore/book[1]/title
的语法,但随后 says :
There is a problem with this. The example above shows different results in IE and other browsers.
IE5 and later has implemented that [0] should be the first node, but according to the W3C standard it should have been [1]!!
A Workaround!
To solve the [0] and [1] problem in IE5+, you can set the SelectionLanguage to XPath.
但这是特定于客户的。 PHP手册对此也不是很清楚。
最佳答案
正如菲利克斯所说:尝试一下怎么样?
$l = new SimpleXMLElement('<layout>
<pattern> ... </pattern>
<row> ... </row>
</layout>');
foreach($l->xpath('/layout/*[1]') as $n) {// yeah yeah, it's only one....
echo $n->getName();
}
打印模式
。 (php 5.3.1/win32/php.net 版本)
关于php - 在 PHP 上使用 xpath : how to select first child?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2180822/