Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何使用Nokogiri仅选择不仅仅是空间的节点?_Ruby_Xpath_Nokogiri - Fatal编程技术网

Ruby 如何使用Nokogiri仅选择不仅仅是空间的节点?

Ruby 如何使用Nokogiri仅选择不仅仅是空间的节点?,ruby,xpath,nokogiri,Ruby,Xpath,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>

我有以下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>
但是,我只想选择那些包含文本的文本节点,而不是唯一的空格,因为第一个节点如上面的xml示例所示

我扩展了String类以测试空格,如下所示:

text_nodes = p.xpath('w:r')
class String
  def spaces?
    x = self =~ /^\s+$/
    x == 0
  end
end
所以我可以做到:

element.text.spaces?
我只是不知道如何将它与
p.xpath('w:r')
结合起来,只选择不仅仅是空间的节点

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

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

谢谢您。这适用于任意数量的空格还是仅适用于一个空格?
规范化空格
删除所有前导空格和尾随空格,如果不存在其他字符,则这些空格等于空字符串。