试图让我了解 Nokogiri 和 XPath,并希望您能帮助解释这种行为。这段代码:
data = Nokogiri::XML(%{
<veg>
<peas>
<color>"green"</color>
</peas>
<peas>
<color>"yellow"</color>
</peas>
</veg>
})
data.xpath('//peas').each do |p|
puts p
puts p.xpath('color/text()')
puts p.xpath('//color/text()') # output not as expected
end
给出这个输出:
<peas>
<color>"green"</color>
</peas>
"green"
"green"
"yellow"
<peas>
<color>"yellow"</color>
</peas>
"yellow"
"green"
"yellow"
puts 如何p.xpath('//color/text()')
最终检索到绿色和黄色,而 p 只包含其中一个?