Xpath 选择多个标签 [英] Xpath select multiple tags

查看:31
本文介绍了Xpath 选择多个标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要使用 PHP DOMXPath 查询的多个标签(td 和 th).

I want so multiple tags (td and th) using a PHP DOMXPath query.

我该怎么做?

推荐答案

您可以使用 |(联合)运算符.下面是一个例子:

You can use the | (Union) operator. Here is an example:

$doc = new DOMDocument();
$doc->loadHTML('<table>
<tr>
<th>table header</th>
<td>table cell</td>
</tr>
</table>');

$xpath = new DOMXPath($doc);

$rows = $xpath->query('//tr');                        // select all <tr> elements anywhere in the document
$cols = $xpath->query('./th | ./td', $rows->item(0)); // select all <th>/<td> from context
                                                      // where context = first row
echo $cols->length;             // 2
echo $cols->item(0)->nodeValue; // table header
echo $cols->item(1)->nodeValue; // table cell

这篇关于Xpath 选择多个标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆