如何使用 Nokogiri 仅 select 不仅是空间的节点?

How to select only nodes that are not only spaces using Nokogiri?

我有以下 XML 文档:

<w:p w14:paraId="572705D7" w14:textId="77777777" w:rsidP="00CA0169" w:rsidR="00CA0169" w:rsidRDefault="00CA0169" w:rsidRPr="00777A35">
    <w:r>
        <w:t xml:space="preserve"/>
    </w:r>
    <w:r>
        <w:t>synthesized in cyanobacteria under unsuitable condition</w:t>
    </w:r>
</w:p>

我目前select所有以如下开头的节点:

text_nodes = p.xpath('w:r')

但是,我只想 select 那些包含文本且不仅仅是空格的文本节点,因为第一个节点如上面的 xml 示例所示。

我扩展了字符串 Class 以测试空格,如下所示:

class String
  def spaces?
    x = self =~ /^\s+$/
    x == 0
  end
end

所以我能做到:

element.text.spaces?

我只是不知道如何将它与 p.xpath('w:r') 到 select 不仅是空格的节点放在一起。

w:r[normalize-space(.) != '']

正如您的 XPath 表达式应该做的那样。