我根据某些条件选择了一个节点集。生成的节点集是正确的。但是,如果我在其上执行xpath,我将从文档中获取所有节点。我必须在这里遗漏一些东西。解释和解决方案将不胜感激。
require 'nokogiri'
doc = Nokogiri::XML(DATA)
selection = doc.xpath("//listing[code[contains(text(), '34')]]")
p selection.length ## 2
p selection.xpath("//id").inner_text ##34567 (ids of all nodes), I'm trying to get 35 instead
__END__
<?xml version="1.0" encoding="UTF-8"?>
<listings>
<listing>
<id>3</id>
<code>3,4,55,34</code>
</listing>
<listing>
<id>4</id>
<code>3,4,55,33</code>
</listing>
<listing>
<id>5</id>
<code>3,4,55,34</code>
</listing>
<listing>
<id>6</id>
<code>3,4,55</code>
</listing>
<listing>
<id>7</id>
<code>3,14</code>
</listing>
</listings>