nokogiri xpath attribute - strange results

2019-05-23 08:24发布

问题:

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.

回答1:

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')


回答2:

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.