nokogiri xpath attribute - strange results

2019-05-23 07:57发布

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

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

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.

2条回答
叛逆
2楼-- · 2019-05-23 08:32

As additional clarification, the reason tbegin = record.css('TOKEN') works is record is providing the top node where the search will begin. 'TOKEN' doesn't force a search at the root of the document, unlike //FIELD/TOKEN, which would do that.

查看更多
孤傲高冷的网名
3楼-- · 2019-05-23 08:44

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')
查看更多
登录 后发表回答