XPath中的Tbody标签由Fire Bug产生 [英] Tbody tag in xpath produced by fire bug

查看:158
本文介绍了XPath中的Tbody标签由Fire Bug产生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ruby hpricot库从在线html中提取一些数据.我使用firefox扩展名fire bug来获取所选项目的xpath.

I'm trying to extract some data from online htmls using ruby hpricot library. I use the firefox extension fire bug to get the xpath of a selected item.

在产生的xpath表达式中总是存在额外的tbody标签.在某些情况下,我必须从表达式中删除tbody标签以获取结果,而在其他情况下,我必须保留标签以获取结果.

There's always the extra tbody tag present in the produced xpath expression. In some cases, I must remove the tbody tag from the expression to obtain the results while in other cases, I must keep the tag to get the results.

我只是不知道何时保留tbody标签,什么时候不保留.

I just can't figure out when to keep the tbody tag and when not to.

推荐答案

为了考虑并避免此问题,请使用以下类型的XPath表达式:

 /locStep1/locStep2/.../table/YourSubExpression
|
 /locStep1/locStep2/.../table/tbody/YourSubExpression

如果table没有子tbody ,则联合运算符的第二个参数(|)不选择节点,而联合运算符的第一个参数选择所需的节点.

If the table doesn't have a tbody child, then the second argument of the union operator (|) selects no nodes and the first argument of the union selects the wanted nodes.

或者,如果table 具有一个tbody,则并集运算符的第一个参数不选择节点,而并集第二个参数选择所需的节点.

Alternatively, if the table has a tbody child, then the first argument of the union operator selects no nodes and the second argument of the union selects the wanted nodes.

最终结果:在两种情况下都选择了所需的节点

The end result: in both cases the wanted nodes are selected

这篇关于XPath中的Tbody标签由Fire Bug产生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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