php - 在 PHP 上使用 xpath : how to select first child?

标签 php xml xpath

我有一个像这样的 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/

相关文章:

php - 将日期从 PDO 传递到 MySQL 存储过程

PHP foreach 循环返回额外值

php - MySQL 可以处理大量数据吗?

PHP -> 如何检查 XML 是否为空

Google Docs ImportXML 的 XPath 查询

selenium - 如何在 selenium 中搜索带有符号的元素

php 没有获取 JSON 对象

java - Struts 2.5 更新操作 validator 问题(条件验证)

python - <element> 的 Pyxb 实例没有开始标记的绑定(bind)元素

html - 如何在 xpath 中获取选项的值而不是显示值?