nokogiri xpath 属性 - 奇怪的结果 [英] nokogiri xpath attribute - strange results

查看:38
本文介绍了nokogiri xpath 属性 - 奇怪的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆字段,当我尝试运行时:

I have a bunch of fields and when I try to run:

src.xpath('//RECORD').each do |record|
tbegin = record.xpath('//FIELD/TOKEN')

tbegin 数组返回来自其他记录的字段.我已经检查过第一行是否为我提供了适当的记录"子树数组,但是对 tbegin 的下一次调用并没有将搜索限制为记录"子树.事实上,它始终返回record[0]的字段子树.

the tbegin array returns the fields from other records. I've checked that the first line is giving me the appropriate array of "record" subtrees, but the next call for tbegin doesn't limit the search to just the "record" subtree. In fact, it consistently returns the field subtree of record[0].

到目前为止,我已经通过使用:

Thus far, I've gotten around this by using:

tbegin = record.css('TOKEN')

但我想了解我做错了什么.

but I want to understand what I'm doing wrong.

推荐答案

问题是 xpath('//FIELD/TOKEN') 中的前导双斜线,它告诉 nokogiri 匹配节点不是相对于这个节点,而是跨越整个文档而不管位置.要相对于节点本身进行匹配,您必须删除双斜杠:

The problem is the leading double-slash in xpath('//FIELD/TOKEN'), which tells nokogiri to match nodes not relative to this node, but across the whole document regardless of location. To match relative to the node itself, you have to remove the double-slash:

tbegin = record.xpath('FIELD//TOKEN')

这篇关于nokogiri xpath 属性 - 奇怪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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