nokogiri xpath attribute - strange results

只愿长相守 提交于 2019-12-07 23:02:05

问题


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.



来源:https://stackoverflow.com/questions/12301611/nokogiri-xpath-attribute-strange-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!